I am having a model User and having id and other properties in it.
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.
In the UserLocation model class and other model classes also I did the one to one mapping with the user class .
And also other properties along with their getter and 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 . 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 .
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.
You have instead the object User
in the UserLocation
table, so this is represented by
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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.