[英]hibernate multiple queries executing
I am having a model User and having id and other properties in it. 我有一个模型用户,并且具有id和其他属性。
public class User{
int id;
.....
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id")
private UserLocation userLocationObj;
}
UserLocation is the model class and like the UserLocation there are more 5-6 properties with same annotations to make the association with the User class. UserLocation是模型类,像UserLocation一样,还有更多5-6个具有相同批注的属性,以与User类建立关联。
In the UserLocation model class and other model classes also I did the one to one mapping with the user class . 在UserLocation模型类和其他模型类中,我也与user类进行了一对一的映射。
And also other properties along with their getter and setter. 以及其他属性以及其getter和setter。 And when I am trying to getting the object of User class by using id it is executing n number of queries and then showing stackoverflow error . 当我尝试通过使用id获取User类的对象时,它正在执行n个查询,然后显示stackoverflow错误。 Why so ?? 为什么这样 ??
My User table Structure is : 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 | |
+-----------------+-----+
UserId in the UserLocation is the id of the Users table . UserLocation中的UserId是Users表的ID。
Thanks 谢谢
The problem here is connected to the name of your column: You are telling hibernate that @JoinColumn(name="id")
: this means that in the table User
there is a field named id
, that joins in the table UserLocation
, which is not what is your database structure. 这里的问题与您的列的名称有关:您正在告诉hibernate @JoinColumn(name="id")
:这意味着表User
有一个名为id
的字段,该字段连接到表UserLocation
,即不是您的数据库结构是什么。
You have instead the object User
in the UserLocation
table, so this is represented by 相反,您在UserLocation
表中有对象User
,因此由表示
public class UserLocation{
int id;
.....
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="user_id")
private User user;
}
Then, you can let the User
class know that it has connected the UserLocation
class with this: 然后,您可以让User
类知道它已经使用以下方法连接了UserLocation
类:
public class User{
int id;
.....
@OneToOne(fetch=FetchType.LAZY, mappedBy = "user_id")
private UserLocation userLocationObj;
}
The JoinColumn
annotation indicates that the class is the owner of the relation, so in this case the owner is UserLocation
, whereas the mappedBy
represent the other side of the linking. 所述JoinColumn
注释指示该类是关系的所有者 ,所以在这种情况下,所有者是UserLocation
,而mappedBy
表示链接的另一侧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.