java-在persistence.xml之外定义jpa实体类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-在persistence.xml之外定义jpa实体类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3051字,纯文字阅读大概需要5分钟。
内容图文
![java-在persistence.xml之外定义jpa实体类](/upload/InfoBanner/zyjiaocheng/671/6861d0bacba54ddfb41113898635925f.jpg)
有没有办法在persistence.xml之外(即在单独的文件中)定义jpa实体类?
能够没有persistence.xml作为外部文件也将足够.
提前致谢,
史蒂文
编辑:
对不起,我不清楚.这是在Java SE环境中.
另外,我不想在persistence.xml文件中列出some.class.AClass.
这是因为我想动态创建此类列表并引用包含此列表的文件.
编辑2:
通过在打包之前在构建时编写persistence.xml文件来解决此问题.如果有人感兴趣,我会使用scannotations查找所有@Entity注释的类,并将它们写到target / classes / META-INF中的persistence.xml文件中(使用maven).
解决方法:
在Java SE环境中,可移植应用程序必须在persistence.xml中显式列出类.根据JPA 1.0规范:
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.
如果不考虑可移植性,则某些提供程序会在Java SE环境中支持实体发现(例如,EclipseLink,Hibernate).
如果需要考虑可移植性,那么使用像Spring这样的第三方容器会有所帮助.
内容总结
以上是互联网集市为您收集整理的java-在persistence.xml之外定义jpa实体类全部内容,希望文章能够帮你解决java-在persistence.xml之外定义jpa实体类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。