通过(一对一)暂停休眠:org.hibernate.AnnotationException:未知mappingBy在:引用的属性未知:

时间:2018-07-23 06:01:02

标签: java hibernate one-to-one hibernate-annotations

我试图在休眠状态下创建一对一关系,当我实现该关系时,我会得到 org.hibernate.AnnotationException 。请找出错误并建议我。

蔬菜课

@SuppressWarnings("serial")
@Entity
@Table(name = "Vegetables")
@Access(value = AccessType.FIELD)
public class Vegetable implements Serializable  {

    @Id
    @Column(name = "P_ID")
    private String productID;

    @Column(name = "Product_Name")
    private String productName;

    @Column(name = "Description")
    private String description;

    @Column(name = "In_Item")
    private LocalDate localDateIn;

    @Column(name = "Out_Item")
    private LocalDate localDateOut;

    @OneToOne(mappedBy = "vegetable", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        private Stock stock;
//getter & setter
}

股票类别

@SuppressWarnings("serial")
@Entity
@Table(name="STOCK")
@Access(value=AccessType.FIELD)
public class Stock implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer sId;

    @Column(name="stockLevel")
    private Integer stockLevel;

    @Column(name="StockUSed")
    private Integer stockUsed;

    @Column(name="Availablity")
    private Integer availablity;

    @Column(name="OutOfStock")
    private Integer outOfStock;

    //getter & setter
}

例外

Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: com.javabootstar.grofers.domain.Vegetable.stock, referenced property unknown: com.javabootstar.grofers.domain.Stock.vegetable

1 个答案:

答案 0 :(得分:3)

您没有正确使用mappedBy中的@OneToOne属性。 MappedBy属性表明该关系的键在另一侧。

在您的情况下,您期望您的Stock类中有一个蔬菜田(不可用),这就是您遇到此异常的原因。

您应该使用@JoinColumn注释在菜类中映射外键