Hibernate jpa" mappedBy"例外

时间:2018-04-10 14:51:38

标签: java spring hibernate jpa

当我想使用persist方法将新的UserDetails对象添加到我的数据库时,我的JPA数据库出现问题。然后我得到了例外:"重复hibernate的条目2"此例外与Product类有关。我找到了基于将mappedBy参数添加到@OneToMany(上面的Product class list)的解决方案,但后来我得到了#34; mappedBy引用了一个未知的目标实体属性" 有没有解决方案?

@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

1 个答案:

答案 0 :(得分:1)

在课程Product中,您需要声明字段detailsOffer并将其映射为@ManyToOne@JoinColumn,并使用引用DetailsOffer的列名称。< / p>

例如:

@ManyToOne
@JoinColumn(name = 'id_details_offer')
private DetailsOffer detailsOffer;

我希望它可以帮到你。