[英]get all hibernate anotations/entity from another maven repository artifact
Can I get all entity or hibernate annotations from another maven repository artifact to my project? 我可以从另一个Maven存储库工件到我的项目中获取所有实体或休眠注释吗?
In this case I want to add all entities from a package inside an artifact that I create. 在这种情况下,我想从我创建的工件内的包中添加所有实体。 all i found is give setting like
<class>
or <jar-file>
in the persistence.xml file. 我发现的全部是给persistence.xml文件中的
<class>
或<jar-file>
的设置。 like: 喜欢:
<class>id.co.test.domain.Entity1</class>
<class>id.co.test.domain.Entity2</class>
<class>id.co.test.domain.Entity3</class>
So that class will be included in my project, while the artifact may modified(cant use <class>
) or get the version changed(cant use <jar-file>
) 这样该类将包含在我的项目中,而工件可以进行修改(请使用
<class>
)或更改版本(请使用<jar-file>
)
Is there any way to import any entities inside that one package id.co.test.domain
to my project? 有什么办法可以将一个包
id.co.test.domain
任何实体导入到我的项目中?
You could define a filter like this (Spring, but you can also do it with other libraries, or you can program the class scanning yourself): 您可以定义一个这样的过滤器(Spring,但是您也可以使用其他库来做,或者您可以自己编写扫描类的程序):
Then use a class scanner to retrieve you all classes that have that annotations and add them to your hibernate configuration: 然后使用类扫描器检索具有该批注的所有类,并将它们添加到您的休眠配置中:
hibernate: create Entity(with annotations) in runtime I have also attached some code, which, while not perfect, can get your creative juices flowing. 休眠:在运行时创建Entity(带有批注)我还附加了一些代码,这些代码虽然不完美,但可以使您的创意源源不断。 It is Spring based, but below, i will summarize the idea as well in pseudo code.
它是基于Spring的,但是在下面,我还将用伪代码来总结这个思想。
private static final TypeFilter[] ENTITY_TYPE_FILTERS = new TypeFilter[] {
new AnnotationTypeFilter(Entity.class, false),
new AnnotationTypeFilter(Embeddable.class, false),
new AnnotationTypeFilter(MappedSuperclass.class, false)};
private void scanPackages(Configuration conf, String... packagesToScan) throws HibernateException {
try {
for (String pkg : packagesToScan) {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
Resource[] resources = resourcePatternResolver.getResources(pattern);
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
for (Resource resource : resources) {
if (resource.isReadable()) {
MetadataReader reader = readerFactory.getMetadataReader(resource);
String className = reader.getClassMetadata().getClassName();
if (matchesFilter(reader, readerFactory)) {
conf.addAnnotatedClass(resourcePatternResolver.getClassLoader().loadClass(className));
}
}
}
}
}
catch (IOException | ClassNotFouncException ex) {
throw new MappingException("Failed to scan classpath for unlisted classes", ex);
}
}
Basically you should follow the following steps: 基本上,您应该遵循以下步骤:
Find the place where you bootstrap hibernate. 找到您引导休眠的位置。
Find a way to scan all classes in your external jar that you need to be put in hibernate, that would most commonly be classes annnotated with @Entity, @MappedSuperClass and @Embedded. 找到一种方法来扫描您需要置于休眠状态的外部jar中的所有类,这些方法通常是用@ Entity,@ MappedSuperClass和@Embedded注释的类。
Use Hibernate's configuration.addAnnotatedClass
method ot add those classes to your hibernate config 使用Hibernate的
configuration.addAnnotatedClass
方法将这些类添加到您的hibernate配置中
http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/cfg/Configuration.html#addAnnotatedClass(java.lang.Class) http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/cfg/Configuration.html#addAnnotatedClass(java.lang.Class)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.