我正在使用Hibernate Annotations。
在我的所有模型类中,我都这样注释:
@Entity
@Table
public class SomeModelClass {
//
}
我的hibernate.cfg.xml是
<hibernate-configuration>
<session-factory>
<!-- some properties -->
<mapping package="com.fooPackage" />
<mapping class="com.fooPackage.SomeModelClass" />
</session-factory>
</hibernate-configuration>
对于我添加到com.fooPackage的每个类,我必须在hibernate.cfg.xml中添加一行,如下所示:
<mapping class="com.fooPackage.AnotherModelClass" />
有没有办法可以添加新的模型类,但是不需要将这行添加到hibernate.cfg.xml中?
答案 0 :(得分:18)
开箱即用 - 没有。但是,您可以编写自己的代码来检测/注册带注释的类。如果您正在使用Spring,则可以扩展AnnotationSessionFactoryBean
并执行以下操作:
@Override
protected SessionFactory buildSessionFactory() throws Exception {
ArrayList<Class> classes = new ArrayList<Class>();
// the following will detect all classes that are annotated as @Entity
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
// only register classes within "com.fooPackage" package
for (BeanDefinition bd : scanner.findCandidateComponents("com.fooPackage")) {
String name = bd.getBeanClassName();
try {
classes.add(Class.forName(name));
} catch (Exception E) {
// TODO: handle exception - couldn't load class in question
}
} // for
// register detected classes with AnnotationSessionFactoryBean
setAnnotatedClasses(classes.toArray(new Class[classes.size()]));
return super.buildSessionFactory();
}
如果您不使用Spring(并且您应该:-)),您可以编写自己的代码来检测相应的类,并使用AnnotationConfiguration
通过addAnnotatedClass()
方法注册它们。
顺便说一句,没有必要映射包,除非你实际上已经在包级别声明了什么。
答案 1 :(得分:11)
我刚刚遇到这个问题,并发现似乎有一个开箱即用的解决方案。我的集成尚未完成,稍后会更新。
来自Javadoc,尤其是packagesToScan
部分:
org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
Spring的标准LocalSessionFactoryBean的子类,用于Hibernate, 支持映射的JDK 1.5+注释元数据。
注意:此类需要使用Java的Hibernate 3.2或更高版本 存在持久性API和Hibernate Annotations附加组件。
AnnotationSessionFactoryBean
bean定义的示例:<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>test.package.Foo</value> <value>test.package.Bar</value> </list> </property> </bean>
或者当使用类路径扫描来实现实体类的自动检测时:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="test.package"/> </bean>
自:1.2.2
作者:Juergen Hoeller
答案 2 :(得分:11)
这里有一点线索坏死......
看起来仍然不是一个很好的方法来做你想做的事情。如果你不想使用Spring,这里有一个使用Reflections的类似方法:
// Create your SessionFactory with mappings for every `Entity` in a specific package
Configuration configuration = new Configuration();
configuration.configure("your_hibernate.cfg.xml");
Reflections reflections = new Reflections("your_package");
Set<Class<?>> classes = reflections.getTypesAnnotatedWith(javax.persistence.Entity.class);
for(Class<?> clazz : classes)
{
configuration.addAnnotatedClass(clazz);
}
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
对于其他Java反射库,同样的方法也许是可行的。
答案 3 :(得分:1)
如果您不想使用spring
或任何其他库,您可以像这样实现。与luke's相同但没有Reflections
库
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import javax.persistence.Entity;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class SessionFactoryWrapper {
private final SessionFactory sessionFactory;
public SessionFactoryWrapper(final String...packagesToScan) {
this.sessionFactory = this.createSessionFactory(packagesToScan);
}
private SessionFactory createSessionFactory(final String[] packagesToScan) {
final Configuration configuration = new Configuration();
configuration.configure(); // Reads hibernate.cfg.xml from classpath
for (String packageToScan : packagesToScan) {
this.getEntityClasses(packageToScan).stream().forEach( configuration::addAnnotatedClass);
}
final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
}
private Collection<Class> getEntityClasses(final String pack) {
final StandardJavaFileManager fileManager = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
try {
return StreamSupport.stream(fileManager.list(StandardLocation.CLASS_PATH, pack, Collections.singleton(JavaFileObject.Kind.CLASS), false).spliterator(), false)
.map(FileObject::getName)
.map(name -> {
try {
final String[] split = name
.replace(".class", "")
.replace(")", "")
.split(Pattern.quote(File.separator));
final String fullClassName = pack + "." + split[split.length - 1];
return Class.forName(fullClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
})
.filter(aClass -> aClass.isAnnotationPresent(Entity.class))
.collect(Collectors.toCollection(ArrayList::new));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
}
答案 4 :(得分:1)
我使用StackOverflow的答案对类扫描方法进行了一些调查。所以我在一个测试项目中使用Hibernate实体扫描作为测试收集所有内容:hibernate-scanners-test。
使用fluent-hibernate
如果您正在寻找没有其他依赖关系的快速扫描方法,您可以尝试fluent-hibernate库(除了库之外,您不需要其他jar)。 除此之外,它还为Hibernate 5和Hibernate 4提供了一些有用的功能,包括实体扫描,Hibernate 5隐式命名策略,嵌套转换器等。
只需从项目页面下载该库:fluent-hibernate并使用EntityScanner:
对于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();
答案 5 :(得分:-2)
1. XML
<mapping class="com.concretepage.Person"/>
2. Class
Configuration configuration = new Configuration().configure().addAnnotatedClass(Person.class);
//Configure all the classes.
Set<Class> entityClasses = new HashSet<>(Arrays.asList(Person.class,PersonDetails.class));
//Iterate all the Classes.
for (Class cls : entityClasses ) {
configuration.addAnnotatedClass(cls);
}
3. Add package
//Or you can add package
configuration.addPackage("com.domain.entity");