java 使用 @OneToMany 或 @ManyToMany 在休眠中针对未映射的类异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36761816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of @OneToMany or @ManyToMany targeting an unmapped class Exception in hibernate
提问by Yo Yo Money Singh
Forgive me since I am rookie to hibernate and other answers on similar questions didn't quite fix this issue(I am sure my issue is quite basic). I am trying to have a OneToMany and ManyToOne mapping in my persistence classes. I have following tables:
原谅我,因为我是休眠的新手,类似问题的其他答案并没有完全解决这个问题(我确信我的问题很基本)。我试图在我的持久性类中有一个 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)
)
The following persistence class I wrote: For City:
我写的以下持久性类:对于城市:
@Table(name="city")
@Entity
City {
@Id
private Integer id;
@Column(name="name")
private String name;
}
For 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;
}
For 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
}
Facing the following exception:
面临以下异常:
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]
回答by Matteo Baldi
You have to define your join column in the many to one annotation in your Area entity:
您必须在 Area 实体的多对一注释中定义连接列:
@ManyToOne(fetch = FetchType.EAGER, targetEntity = PinCode.class)
@JoinColumn(name = "pincode_id", nullable = false)
plus, in your Pincode entity you should indicate the correct field in the mappedBy
property:
另外,在您的 Pincode 实体中,您应该在mappedBy
属性中指明正确的字段:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pincode")
回答by GHajba
The problem is that you use mappedBy
wrong: you add there the column name in the database however it has to be the property name in the Area
entity by the PinCode
is mapped:
问题是你用mappedBy
错了:你在数据库中添加了列名,但是它必须是Area
实体中的属性名PinCode
被映射:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pinCode")
@Fetch(value = FetchMode.SELECT)
private List<Area> area;
And the JavaDoc of mappedBy
tells you the same:
JavaDoc ofmappedBy
告诉你同样的事情:
The fieldthat owns the relationship. Required unless the relationship is unidirectional.
拥有关系的字段。除非关系是单向的,否则是必需的。
回答by Sergii Bishyr
I suppose you have something like this in your hibernate configuration
我想你的休眠配置中有这样的东西
<mapping class="com.mycompany.model.PinCode" />
Or, if you are using java config
或者,如果您使用的是 java 配置
.addAnnotatedClass(PinCode.class);
Make sure taht all your @Entity
classes are declared as mapping classes, or configure it to auto-scan
确保所有@Entity
类都声明为映射类,或将其配置为自动扫描