我有一个模型用户,其中有id和其他属性。
public class User{
int id;
.....
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id")
private UserLocation userLocationObj;
}UserLocation是模型类,和UserLocation一样,有更多的5-6个带有相同注释的属性来与User类关联。
在UserLocation模型类和其他模型类中,我也与user类进行了一对一的映射。
以及其他属性以及它们的getter和setter。当我试图使用id获取User类的对象时,它执行了n次查询,然后显示了stackoverflow错误。为什么呢??
我的用户表结构是: User
+----------------------+-----
| Field | Key|
+----------------------+-----
| id | PRI|
| userName | |
| first_name | |
| middle_name | |
| last_name | |
| dateOfBirth | |
| gender | |
| city | |
| email | |
+----------------------+----+
My UserLocation table Structure is :
UserLocation
+-----------------++-----
| Field | Key |
+-----------------+------
| id | PRI |
| user_id | |
| current_city | |
| current_country | |
| latitude | |
| longitude | |
| iso_location | |
+-----------------+-----+UserLocation中的UserId是Users表的id。
谢谢
发布于 2018-07-31 15:09:00
这里的问题与您的列名有关:您告诉hibernate @JoinColumn(name="id"):这意味着在表User中有一个名为id的字段,该字段与表UserLocation连接,这不是您的数据库结构。
相反,在UserLocation表中有对象User,因此它由
public class UserLocation{
int id;
.....
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="user_id")
private User user;
}然后,您可以让User类知道它已经用下面的代码连接了UserLocation类:
public class User{
int id;
.....
@OneToOne(fetch=FetchType.LAZY, mappedBy = "user_id")
private UserLocation userLocationObj;
}https://stackoverflow.com/questions/51593779
复制相似问题