原谅我,因为我是菜鸟进入休眠状态,其他类似问题的答案没有完全解决这个问题(我相信我的问题很基本)。我正在尝试在持久性类中使用OneToMany和ManyToOne映射。 我有以下表格:使用@OneToMany或@ManyToMany针对未映射的类在休眠中发生异常
city (
id INT AUTO_INCREMENT,
name VARCHAR(255),
)
pincode (
id INT AUTO_INCREMENT,
pincode VARCHAR(6),
city_id INT FOREIGN KEY fk_city_id ON city(id)
)
area (
id INT AUTO_INCREMENT,
name VARCHAR(255),
pincode_id INT FOREIGN KEY fk_pincode_id ON pincode(id)
)
下持续I类写道: 城市:
@Table(name="city")
@Entity
City {
@Id
private Integer id;
@Column(name="name")
private String name;
}
对于PIN码:
@Table(name="pincode")
@Entity
PinCode {
@Id
private Integer id;
@Column(name = "pincode")
private String pinCode;
@ManyToOne
@JoinColumn(name = "city_id")
private City city; //Multiple pincodes may be mapped to one city
//One pincode may have multiple areas
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pincode_mapping")
@Fetch(value = FetchMode.SELECT)
private List<Area> area;
}
适用面积:
@Entity
Area {
@Id
private Integer id;
@Column(name = "name")
private String name;
@ManyToOne(fetch = FetchType.EAGER, targetEntity = PinCode.class)
@Column(name = "pincode_id")
private PinCode pinCode; //Multiple areas may be mapped to one pincode
}
面对以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'sessionFactory' defined in class path resource [applicationContext-resources.xml]: Invocation of init method failed;
nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.mycompany.model.PinCode.area[com.mycompany.model.Area]
谢谢。这解决了这个问题。 –