hibernate 扫描包可以自动创建 SessionFactory 吗?
Can hibernate scan packages to create SessionFactory automatically?
我可以将 Hibernate 配置为自动扫描包以从 @Entity
注释 bean 创建 SessionFactory
吗?
目前我正在使用
Configuration config = new Configuration() ;
config.addAnnotatedClass(MyEntity.class);
我不想使用 hibernate.cfg.xml
配置映射。
请注意,我想在不使用任何 Spring 或此类框架的普通 Java 项目中实现此目的。
之前使用 Spring 已经回答了类似的问题,但我想在不使用 Spring 或其他框架的情况下实现它。我对一些执行此操作的简单库持开放态度。
没有。即使使用最新的 Hibernate 5 版本,您也不能说 Hibernate 扫描包以获取持久性 类。 Configuration
有方法 addPackage()
,但它用于读取 "package-level metadata"(.package-info
- 文件)。
你不想使用Spring,你能做什么:
使用 fluent-hibernate
你可以使用
EntityScanner from fluent-hibernate 库(你不需要其他 jar,除了库)
对于 Hibernate 4 和 Hibernate 5:
Configuration configuration = new Configuration();
EntityScanner.scanPackages("my.com.entities", "my.com.other.entities")
.addTo(configuration);
SessionFactory sessionFactory = configuration.buildSessionFactory();
使用新的 Hibernate 5 引导 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();
使用其他库
如果您已经使用了可用于扫描的库,例如Reflections, there is a test project with examples of using various libraries for entity scanning: hibernate-scanners-test。
public static ArrayList listfiles(String pckgname) {
ArrayList classes=new ArrayList();
try{
// Get a File object for the package
File directory=null;
try {
directory=new File(Thread.currentThread().getContextClassLoader().getResource(pckgname.replace('.', '/')).getFile());
} catch(NullPointerException x) {
System.out.println("Nullpointer");
throw new ClassNotFoundException(pckgname+" does not appear to be a valid package");
}
if(directory.exists()) {
// Get the list of the files contained in the package
String[] files=directory.list();
for(int i=0; i<files.length; i++) {
// we are only interested in .class files
if(files[i].endsWith(".class")) {
// removes the .class extension
classes.add(Class.forName(pckgname+'.'+files[i].substring(0, files[i].length()-6)));
}
}
} else {
System.out.println("Directory does not exist");
throw new ClassNotFoundException(pckgname+" does not appear to be a valid package");
}
Class[] classesA=new Class[classes.size()];
classes.toArray(classesA);
// return classesA;
} catch (Exception e) {
e.printStackTrace();
}
return classes;
}
我可以将 Hibernate 配置为自动扫描包以从 @Entity
注释 bean 创建 SessionFactory
吗?
目前我正在使用
Configuration config = new Configuration() ;
config.addAnnotatedClass(MyEntity.class);
我不想使用 hibernate.cfg.xml
配置映射。
请注意,我想在不使用任何 Spring 或此类框架的普通 Java 项目中实现此目的。
之前使用 Spring 已经回答了类似的问题,但我想在不使用 Spring 或其他框架的情况下实现它。我对一些执行此操作的简单库持开放态度。
没有。即使使用最新的 Hibernate 5 版本,您也不能说 Hibernate 扫描包以获取持久性 类。 Configuration
有方法 addPackage()
,但它用于读取 "package-level metadata"(.package-info
- 文件)。
你不想使用Spring,你能做什么:
使用 fluent-hibernate
你可以使用 EntityScanner from fluent-hibernate 库(你不需要其他 jar,除了库)
对于 Hibernate 4 和 Hibernate 5:
Configuration configuration = new Configuration();
EntityScanner.scanPackages("my.com.entities", "my.com.other.entities")
.addTo(configuration);
SessionFactory sessionFactory = configuration.buildSessionFactory();
使用新的 Hibernate 5 引导 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();
使用其他库
如果您已经使用了可用于扫描的库,例如Reflections, there is a test project with examples of using various libraries for entity scanning: hibernate-scanners-test。
public static ArrayList listfiles(String pckgname) {
ArrayList classes=new ArrayList();
try{
// Get a File object for the package
File directory=null;
try {
directory=new File(Thread.currentThread().getContextClassLoader().getResource(pckgname.replace('.', '/')).getFile());
} catch(NullPointerException x) {
System.out.println("Nullpointer");
throw new ClassNotFoundException(pckgname+" does not appear to be a valid package");
}
if(directory.exists()) {
// Get the list of the files contained in the package
String[] files=directory.list();
for(int i=0; i<files.length; i++) {
// we are only interested in .class files
if(files[i].endsWith(".class")) {
// removes the .class extension
classes.add(Class.forName(pckgname+'.'+files[i].substring(0, files[i].length()-6)));
}
}
} else {
System.out.println("Directory does not exist");
throw new ClassNotFoundException(pckgname+" does not appear to be a valid package");
}
Class[] classesA=new Class[classes.size()];
classes.toArray(classesA);
// return classesA;
} catch (Exception e) {
e.printStackTrace();
}
return classes;
}