我有几个实体,它们具有相同的属性,因此提取了像下面的公共参数。
@MappedSuperclass()
public class BaseCustomerDemographicDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
@ManyToOne
@JoinColumn(name = "GROUP")
protected Group group;
}
实体片段如下。
@Entity
@Table(name = "INDIVIDUAL_CUSTOMER")
@Access(AccessType.FIELD)
public class IndividualCustomer extends BaseCustomerDemographicDetails {
@NotNull
@Embedded
private PersonalInformation personalInformation;
@Column(precision = 16, scale = 2)
private BigDecimal annualIncome;
private Boolean vip;
private Boolean isStaff;
...
}
@Entity
@Access(AccessType.FIELD)
@Table(name = "GROUP")
public class Group {
protected Group(){}
@Id
@NotNull
@Column(name = "GROUPID")
private String groupId;
@NotNull
@Column(name="GROUPNAME")
private String groupName;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "group")
private List<BaseCustomerDemographicDetails> customerDemographicDetails;
}
使用Hibernate进行OneToMany映射会在启动时导致异常,如下所示。
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:860)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:319)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 16 common frames omitted
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: xx.xxx.xxxxxxx.xxxxxxxxx.domain.model.Group.customerDemographicDetails[xx.xxx.xxxxx.xxxxxxx.domain.model.BaseCustomerDemographicDetails]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1135)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:794)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:729)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
... 24 common frames omitted
这不是 @MappedSuperclass
应该使用的方式吗?
@OneToMany(fetch = FetchType.LAZY, mappedBy = "group") private List<IndividualCustomer> customerDemographicDetails;
- Mohamed Nabli