Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1096101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:13:53+00:00 2026-05-17T00:13:53+00:00

Is there a way to define jpa entity classes outside of persistence.xml (i.e. in

  • 0

Is there a way to define jpa entity classes outside of persistence.xml (i.e. in a separate file)?

Being able to not have persistence.xml as an external file would also suffice.

Thanks in advance,
Steven

Edit:
Sorry I was not clear. This is in a Java SE environment.
Also, I would like to not have a listing of some.class.AClass in my persistence.xml file.
This is because I would like to create this list of classes dynamically and reference a file containing this list.

Edit2:
Managed to solve this by writing a persistence.xml file at build time before it is packaged. If anyone is interested, I used scannotations to discover all the classes annotated with @Entity and wrote them to the persistence.xml file in target/classes/META-INF (using maven).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:13:54+00:00Added an answer on May 17, 2026 at 12:13 am

    In a Java SE environment, portable applications must list classes explicitly in the persistence.xml. From the JPA 1.0 specification:

    6.2.1.6 mapping-file, jar-file, class, exclude-unlisted-classes

    The following classes must be
    implicitly or explicitly denoted as
    managed persistence classes to be
    included within a persistence unit:
    entity classes; embeddable classes;
    mapped superclasses.

    The set of managed persistence classes
    that are managed by a persistence unit
    is defined by using one or more of the
    following:

    • One or more object/relational mapping XML files
    • One or more jar files that will be searched for classes
    • An explicit list of the classes
    • The annotated managed persistence classes contained in the root of the
      persistence unit (unless the
      exclude-unlisted-classes element is
      specified)

    (…)

    A list of named managed persistence
    classes may also be specified instead
    of, or in addition to, the JAR files
    and mapping files. Any mapping
    metadata annotations found on these
    classes will be processed, or they
    will be mapped using the mapping
    annotation defaults. The class element
    is used to list a managed persistence
    class. A list of all named managed
    persistence classes must be specified
    in Java SE environments to insure
    portability. Portable Java SE
    applications should not rely on the
    other mechanisms described here to
    specify the managed persistence
    classes of a persistence unit.
    Persistence providers may also require
    that the set of entity classes and
    classes that are to be managed must be
    fully enumerated in each of the
    persistence.xml files in Java SE
    environments.

    All classes contained in the root of
    the persistence unit are also searched
    for annotated managed persistence
    classes and any mapping metadata
    annotations found on them will be
    processed, or they will be mapped
    using the mapping annotation defaults.
    If it is not intended that the
    annotated persistence classes
    contained in the root of the
    persistence unit be included in the
    persistence unit, the
    exclude-unlisted-classes element
    should be used. The
    exclude-unlisted-classes element is
    not intended for use in Java SE
    environments.

    The resulting set of entities managed
    by the persistence unit is the union
    of these sources, with the mapping
    metadata annotations (or annotation
    defaults) for any given class being
    overridden by the XML mapping
    information file if there are both
    annotations as well as XML mappings
    for that class. The minimum portable
    level of overriding is at the level of
    the persistent field or property.

    If portability is not a concern, some provider do support entity discovery in a Java SE environment (for example, EclipseLink, Hibernate).

    If portability is a concern, using a third party container like Spring would help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to define an XML schema that allows elements not defined
Is there a way to define styles for a combination of classes? For example,
I have a question - is there any way to define a few buttons
Is there a way to define a CSS class as being equal to another?
Is there any way to force the persistence order of objects in JPA 2
Is there a way to define regions in CSS file just like regions in
Is there a way to define a configuration XML for Checkstyle in Eclipse that
Is there a way to define the timezone for an application in ASP.NET such
Is there a way to define the weekly period I would like to use
Is there any way to define an IDE Layout in visual studio 2008? Lets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.