java AnnotationException:使用 @OneToMany 或 @ManyToMany 定位未映射的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41225746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class
提问by emoleumassi
My User super class:
我的用户超类:
@MappedSuperclass
public class User implements Serializable {
@ManyToOne(targetEntity = Role.class, fetch = FetchType.EAGER)
@JoinColumn(name = "roleId", referencedColumnName = "id")
private Role role;
other attributes ...
Here my role entity:
这是我的角色实体:
@Entity
public class Role implements GrantedAuthority {
@JsonIgnore
@OneToMany(mappedBy = "role", targetEntity = User.class, fetch = FetchType.LAZY)
private Set<User> users = new HashSet<>();
another attributes ...
i have this subclass of the user class:
我有这个用户类的子类:
@Entity
@Table
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class AUser extends User {
...
}
another subclass:
另一个子类:
@Entity
@Table
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class BUser extends User {
....
}
Here is my exception:
这是我的例外:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.x.y.model.Role.users[com.x.y.model.User]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) ~[spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) ~[spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at com.x.y.XApplication.main(XApplication.java:15) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.4.2.RELEASE.jar:1.4.2.RELEASE]
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.x.y.model.Role.users[com.x.y.model.User]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1191) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:794) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.cfg.annotations.CollectionBinder.secondPass(CollectionBinder.java:719) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
... 20 common frames omitted
Can someone tells me what is wrong in my code?
有人可以告诉我我的代码有什么问题吗?
回答by Klaus Groenbaek
You should make User an entity instead of MappedSuperClass. If you make User an abstract class there will still not be any table in the database.
您应该将 User 设为实体而不是 MappedSuperClass。如果您将 User 设为抽象类,则数据库中仍然不会有任何表。
However I would like to warn you about using TABLE_PER_CLASS inheritance in this particular case. TABLE_PER_CLASS gives better performance (when selecting a specific use of a known type), but since you only authenticate a user ones per session, performance should not matter.
但是,我想警告您在这种特殊情况下使用 TABLE_PER_CLASS 继承。table_per_class提供更好的性能(选择特定使用已知类型时),但由于您只对每个会话进行身份验证用户,但性能无关紧要。
I'm working on a system with 3 types of user identity and TABLE_PER_CLASS inheritance (Retrospectively it should have been JOINED inheritance). The problem is that if User has a userName property and you want to do type-ahead you need to search 3 tables instead of one (if each different User type has references to different entities, it can be much worse). In the end we ended up having to add a view to the database, because it was close to impossible to write the query we needed to make a fast searchable UI, because we use the wrong inheritance strategy.
我正在研究具有 3 种类型的用户身份和 TABLE_PER_CLASS 继承的系统(回顾起来应该是 JOINED 继承)。问题是,如果 User 有一个 userName 属性并且您想要预先输入,您需要搜索 3 个表而不是一个(如果每个不同的 User 类型都有对不同实体的引用,则情况可能会更糟)。最后,我们不得不向数据库添加一个视图,因为编写快速搜索 UI 所需的查询几乎是不可能的,因为我们使用了错误的继承策略。
回答by Mik jagger
The problem is stated there. Use of @OneToMany or @ManyToMany targeting an unmapped class: com.x.y.model.Role.users
This is because you are using mapping in a @MapppedSuperClass
. Such classes are not mapped in to a table by the Java persistence provider.
问题在那里说明。Use of @OneToMany or @ManyToMany targeting an unmapped class: com.x.y.model.Role.users
这是因为您在@MapppedSuperClass
. Java 持久性提供程序不会将此类类映射到表中。