我在JPA数据库中遇到了问题,当我想使用持久化方法将新的用户详细信息对象添加到我的数据库中时。然后我得到了异常:“键Hibernate的重复条目2”这个异常与产品类有关。我找到了解决方案,该解决方案是基于将mappdBy参数添加到@OneTo很多(在产品类列表上方),但后来我得到了“mappdBy引用未知的目标实体属性”有什么解决方案吗?
@Entity
public class UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
....
@OneToOne(cascade = CascadeType.ALL)
private UserOffer userOffer;
.....
//getters and setters
下一个
@Entity
public class UserOffer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
.....
@OneToOne(cascade= CascadeType.ALL)
private DetailsOffer detailsOffer;
.....
下一个
@Entity
public class DetailsOffer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy="detailsOffer")
private List<Product> products;
nexr@实体公共类产品{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private PetType petType;
private int petNumber;
@ElementCollection
private List<String> goodies;
private String petDescribtion;
private BigDecimal price;
错误:
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.knr.domain_ver2.Product.detailsOffer in com.knr.domain_ver2.DetailsOffer.products
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:769) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:719) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:382) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 16 common frames omitted
在类Products
中,您需要声明字段详细信息
并将其映射为@ManyToOne
和@JoinCol的
,列名引用DetailsOffer
。
例如:
@ManyToOne
@JoinColumn(name = 'id_details_offer')
private DetailsOffer detailsOffer;
希望对你有帮助。