原谅我,因为我是新手,要冬眠,而其他类似问题的答案也不能完全解决这个问题(我确信我的问题很基础)。我试图在我的持久性类中有一个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;
}


对于PinCode:

@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]

最佳答案

您必须在Area实体的多对一注释中定义连接列:

@ManyToOne(fetch = FetchType.EAGER, targetEntity = PinCode.class)
@JoinColumn(name = "pincode_id", nullable = false)


另外,在您的Pincode实体中,您应该在mappedBy属性中指示正确的字段:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "pincode")

10-01 14:35