Hibernate配置不会列出XML中的所有实体 [英] Hibernate config not list all Entities in XML

查看:126
本文介绍了Hibernate配置不会列出XML中的所有实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我的 hibernate.cfg.xml 文件中,我必须列出每个单独的实体作为映射类,以便我的Hibernate选取实体,否则我得到一个错误,如引用一个未知实体



所以我有大约20个这样的行:

 < mapping class =my.com.entity.User>< / mapping> 
< mapping class =my.com.entity.Address>< / mapping>
...

不必每次都在XML文件中添加新行一个新的实体被创建,有没有办法告诉Hibernate嘿,只需从my.com.entity包中取出所有的东西作为一个实体?

解决方案

没有。即使使用最后一个Hibernate 5版本,你也不能说Hibernate会扫描持久化类的包。



使用Spring



使用Spring的常见方式如@Srini所示。

 < bean id =sessionFactoryclass =org.springframework.orm.hibernate5.LocalSessionFactoryBean> 
< property name =packagesToScan>
< list>
<值> my.com.entities< /值>
<值> my.com.other.entities< /值>
< / list>
< / property>
< / bean>

请注意,取决于Hibernate版本,您需要使用包: org.springframework .orm.hibernate5 org.springframework.orm.hibernate4



使用流利hibernate



如果您不想使用Spring ,你可以使用
流利的hibernate 库(你不需要有其他的jar,除了库)。除此之外,它还具有Hibernate 5和Hibernate 4的一些有用功能,包括实体扫描,Hibernate 5隐式命名策略,嵌套转换器等。

对于Hibernate 4和Hibernate 5:

  Configuration configuration = new Configuration(); 
EntityScanner.scanPackages(my.com.entities,my.com.other.entities)
.addTo(配置);
SessionFactory sessionFactory = configuration.buildSessionFactory();

使用新的Hibernate 5引导API:

  List< Class<?>> classes = EntityScanner 
.scanPackages(my.com.entities,my.com.other.entities)。result();

MetadataSources metadataSources = new MetadataSources(); (Class<> annotatedClass:classes){
metadataSources.addAnnotatedClass(annotatedClass);
;


SessionFactory sessionFactory = metadataSources.buildMetadata()
.buildSessionFactory();

使用其他库



如果您已经使用可用于扫描的库,例如思考,那里是一个测试项目,其中包含使用各种库进行实体扫描的示例: hibernate-scanners-test 一>。

Currently in my hibernate.cfg.xml file, I have to list each individual entity as a mapping class in order for my Hibernate to pick up the Entity or else I get an error like references an unknown entity.

So I have about 20 of these lines:

<mapping class="my.com.entity.User"></mapping>
<mapping class="my.com.entity.Address"></mapping>
...

Instead of having to put a new line in the XML file each time a new Entity is created, is there a way to tell Hibernate "Hey, just pull in everything from the my.com.entity package as a Entity"?

解决方案

No. You can't say Hibernate to scan packages for persistent classes even with the last Hibernate 5 version.

Using Spring

The common way to use Spring for that, as @Srini suggested.

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  <property name="packagesToScan">
    <list>
      <value>my.com.entities</value>
      <value>my.com.other.entities</value>
    </list>
  </property>
</bean>

Note that depends of Hibernate version you need to use package: org.springframework.orm.hibernate5, org.springframework.orm.hibernate4.

Using fluent-hibernate

If you don't want to use Spring, you can use EntityScanner from fluent-hibernate library (you will not need to have other jars, except the library). Apart this, it has some useful features for Hibernate 5 and Hibernate 4, including entities scanning, a Hibernate 5 implicit naming strategy, a nested transformer and others.

For Hibernate 4 and Hibernate 5:

Configuration configuration = new Configuration();
EntityScanner.scanPackages("my.com.entities", "my.com.other.entities")
    .addTo(configuration);
SessionFactory sessionFactory = configuration.buildSessionFactory();

Using a new Hibernate 5 bootstrapping API:

List<Class<?>> classes = EntityScanner
        .scanPackages("my.com.entities", "my.com.other.entities").result();

MetadataSources metadataSources = new MetadataSources();
for (Class<?> annotatedClass : classes) {
    metadataSources.addAnnotatedClass(annotatedClass);
}

SessionFactory sessionFactory = metadataSources.buildMetadata()
    .buildSessionFactory();

Using other libraries

If you already use a library that can be used for scanning, for an example Reflections, there is a test project with examples of using various libraries for entity scanning: hibernate-scanners-test.

这篇关于Hibernate配置不会列出XML中的所有实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆