我试图在休眠状态下创建一对一关系,当我实现该关系时,我会得到 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
答案 0 :(得分:3)
您没有正确使用mappedBy
中的@OneToOne
属性。 MappedBy属性表明该关系的键在另一侧。
在您的情况下,您期望您的Stock类中有一个蔬菜田(不可用),这就是您遇到此异常的原因。
您应该使用@JoinColumn
注释在菜类中映射外键