【发布时间】:2016-04-21 15:11:33
【问题描述】:
请原谅我,因为我是休眠的菜鸟,类似问题的其他答案并没有完全解决这个问题(我相信我的问题很基本)。我试图在我的持久性类中有一个 OneToMany 和 ManyToOne 映射。 我有以下表格:
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)
)
我编写的以下持久性类: 对于城市:
@Table(name="city")
@Entity
City {
@Id
private Integer id;
@Column(name="name")
private String name;
}
对于密码:
@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]
【问题讨论】: