可以休眠扫描包来自动创建SessionFactory? [英] Can hibernate scan packages to create SessionFactory automatically?

查看:119
本文介绍了可以休眠扫描包来自动创建SessionFactory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以配置Hibernate自动扫描包以从 @Entity 带注释的bean中创建 SessionFactory 吗?



目前我正在使用

  Configuration config = new Configuration() ; 
config.addAnnotatedClass(MyEntity.class);

我不想使用 hibernate.cfg.xml 来配置映射。

请注意我想在一个普通的Java项目中实现这个功能,而不使用任何Spring或这样的框架。

以前用Spring回答过类似的问题,但我想在不使用Spring或其他框架的情况下实现它。我向一些简单的图书馆开放,可以做到这一点。

解决方案

没有。即使使用最后一个Hibernate 5版本,你也不能说Hibernate为持久化类扫描软件包。 配置有方法 addPackage(),但它是用于读取包级别元数据( .package-info - files)。



你不想使用Spring,那么你该怎么做:



使用流利hibernate



您可以使用
EntityScanner from fluent-hibernate 库(您不需要除了库之外,还有其他的jar)

对于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 一>。

Can I configure Hibernate to scan packages automatically to create a SessionFactory from @Entity annotated beans ?

Currently I am using

Configuration config = new Configuration() ;
config.addAnnotatedClass(MyEntity.class);

I do not want to use hibernate.cfg.xml to configure mappings.

Please note I want to achieve this in a plain Java project without using any Spring or such frameworks.

Similar question have been answered using Spring before but I want to achieve it without using Spring or other frameworks. I am open to some simple library that does this.

解决方案

No. You can't say Hibernate to scan packages for persistent classes even with the last Hibernate 5 version. Configuration has method addPackage(), but it is for reading "package-level metadata" (.package-info- files).

You don't want to use Spring, so what can you do:

Using fluent-hibernate

You can use EntityScanner from fluent-hibernate library (you will not need to have other jars, except the library)

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.

这篇关于可以休眠扫描包来自动创建SessionFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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