Java Hibernate配置未列出XML中的所有实体
当前,在我的hibernate.cfg.xml文件中,我必须将每个实体列为映射类,以便我的hibernate拾取该实体,否则会出现类似Java Hibernate配置未列出XML中的所有实体,java,hibernate,Java,Hibernate,当前,在我的hibernate.cfg.xml文件中,我必须将每个实体列为映射类,以便我的hibernate拾取该实体,否则会出现类似引用未知实体的错误 所以我有大约20行: <mapping class="my.com.entity.User"></mapping> <mapping class="my.com.entity.Address"></mapping> ... ... 有没有办法告诉Hibernate“嘿,把my.com.Ent
引用未知实体的错误
所以我有大约20行:
<mapping class="my.com.entity.User"></mapping>
<mapping class="my.com.entity.Address"></mapping>
...
...
有没有办法告诉Hibernate“嘿,把my.com.Entity包中的所有东西都作为一个实体拉进来”而不是每次创建一个新实体时都在XML文件中放一行呢?没有。你不能说Hibernate扫描包中的持久类,即使是Hibernate 5的最新版本
使用弹簧
正如@Srini所建议的那样,使用Spring的常用方法是
<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>
使用新的Hibernate 5引导API:
Listv.ladynev提出的两种方法都很好。但是,如果您不想自己控制创建配置
和会话
,可以执行以下操作
在hibernate cfg.xml
中,您需要添加自定义扫描仪
而CustomEntityScanner
的实现如下所示。您只需将自定义包添加到非root
URL,其他一切都是从AbstractScannerImpl复制的
public class CustomEntityScanner extends AbstractScannerImpl {
private final ArchiveDescriptorFactory archiveDescriptorFactory;
public CustomEntityScanner() {
this(StandardArchiveDescriptorFactory.INSTANCE);
}
protected CustomEntityScanner(ArchiveDescriptorFactory archiveDescriptorFactory) {
this.archiveDescriptorFactory = archiveDescriptorFactory;
}
@Override
public ScanResult scan(ScanEnvironment environment, ScanOptions options, ScanParameters parameters) {
final ScanResultCollector collector = new ScanResultCollector( environment, options, parameters );
//this is specific to your implemenation
List<URL> paths = Lists.newArrayList();
// ClasspathHelper is from Reflections library.
paths.addAll(ClasspathHelper.forPackage("your.custom.package"));
environment.getNonRootUrls().addAll(paths);
inal ArchiveContext context = new ArchiveContextImpl( false, collector );
for ( URL url : environment.getNonRootUrls() ) {
final ArchiveDescriptor descriptor = buildArchiveDescriptor( url, false );
descriptor.visitArchive( context );
}
if ( environment.getRootUrl() != null ) {
final ArchiveContext context = new ArchiveContextImpl( true, collector );
final ArchiveDescriptor descriptor = buildArchiveDescriptor( environment.getRootUrl(), true );
descriptor.visitArchive( context );
}
return collector.toScanResult();
}
}
公共类CustomEntityScanner扩展了AbstractScannerImpl{
私人最终归档描述工厂归档描述工厂;
公共CustomEntityScanner(){
这(StandardArchiveDescriptorFactory.INSTANCE);
}
受保护的CustomEntityScanner(ArchiveDescriptor工厂ArchiveDescriptor工厂){
this.archiveDescriptorFactory=archiveDescriptorFactory;
}
@凌驾
公共扫描结果扫描(扫描环境、扫描选项、扫描参数){
最终ScanResultCollector收集器=新的ScanResultCollector(环境、选项、参数);
//这是特定于您的实现的
列表路径=Lists.newArrayList();
//ClasspathHelper来自反射库。
addAll(ClasspathHelper.forPackage(“your.custom.package”);
environment.getNonRootUrls().addAll(路径);
inal ArchiveContext context=new ArchiveContextImpl(false,收集器);
for(URL:environment.getNonRootUrls()){
最终ArchiveDescriptor描述符=buildArchiveDescriptor(url,false);
描述符visitArchive(上下文);
}
if(environment.getRootUrl()!=null){
最终ArchiveContext上下文=新的ArchiveContextImpl(true,收集器);
最终ArchiveDescriptor描述符=buildArchiveDescriptor(environment.getRootUrl(),true);
描述符visitArchive(上下文);
}
返回收集器.toScanResult();
}
}
可能与@ScottSosna重复-我没有使用persistence.xml
文件。我可以在hibernate.cfg.xml文件中使用
吗?不知道,有一段时间没有使用hibernate配置文件了,很明显,试一下看看它是否有效。
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();
public class CustomEntityScanner extends AbstractScannerImpl {
private final ArchiveDescriptorFactory archiveDescriptorFactory;
public CustomEntityScanner() {
this(StandardArchiveDescriptorFactory.INSTANCE);
}
protected CustomEntityScanner(ArchiveDescriptorFactory archiveDescriptorFactory) {
this.archiveDescriptorFactory = archiveDescriptorFactory;
}
@Override
public ScanResult scan(ScanEnvironment environment, ScanOptions options, ScanParameters parameters) {
final ScanResultCollector collector = new ScanResultCollector( environment, options, parameters );
//this is specific to your implemenation
List<URL> paths = Lists.newArrayList();
// ClasspathHelper is from Reflections library.
paths.addAll(ClasspathHelper.forPackage("your.custom.package"));
environment.getNonRootUrls().addAll(paths);
inal ArchiveContext context = new ArchiveContextImpl( false, collector );
for ( URL url : environment.getNonRootUrls() ) {
final ArchiveDescriptor descriptor = buildArchiveDescriptor( url, false );
descriptor.visitArchive( context );
}
if ( environment.getRootUrl() != null ) {
final ArchiveContext context = new ArchiveContextImpl( true, collector );
final ArchiveDescriptor descriptor = buildArchiveDescriptor( environment.getRootUrl(), true );
descriptor.visitArchive( context );
}
return collector.toScanResult();
}
}