I want to create an API with users where I can make referals to other user and store the users that refearal me.
#Entity
#Table(name="requirement")
#Data
#NoArgsConstructor
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id", nullable = false)
private Long id;
#Column(name = "name")
private String name;
private User parentUser;
}
CREATE TABLE user(
id serial PRIMARY KEY,
name VARCHAR,
user_id INT,
what is the best way to handle this case
I want to create an API with users where I can make referals to other user and store the users that refearal me.
You can use #OneToOne Relation between parent entity if you have a related column like 'parent_user_id'.
#OneToOne
#JoinColumn(name = "parent_user_id")
private User parentUser;
Related
I have 3 tables which are Person Login and Account.
Person and Login is OneToOne relation and Login has one FK which is connected Person's id column called PERSON_ID.
Person(one) and Account(many) is OneToMany relation and Account has one FK which is connected Person's id column called PERSON_ID as well .
what i want to do is when i delete one data from Account , nothing happen to Person and Login.
if i delete one data from Person which id=1, Login's PERSON_ID=1 data will be deleted , and all of the data PERSON_ID=1 from Account will be deleted as well.
if i delete one data from Login which PERSON_ID=1, Person 's id=1 data will be deleted , and all of the data PERSON_ID=1 from Account will be deleted as well.
how should i set the cascade ?
i've tried dozens of times and still can't find the logic in there, thanks!!
here's my code of all 3 tables without setting cascade:
`
#Entity
#Table(name = "PERSON")
public class Person {
#Id
#Column(name = "ID", nullable = false, unique = true)
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "NAME")
private String name;
#Column(name = "SEX")
private String sex;
#OneToMany(mappedBy = "person",fetch = FetchType.LAZY)
private List<Account> account;
#OneToOne(mappedBy = "person")
private Login login;
#get..
#set..
}
`
#Entity
#Table(name = "ACCOUNT")
public class Account {
#Id
#Column(name = "ID")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "ACCOUNT")
private String account;
#Column(name = "AMOUNT")
private String amount;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "PERSON_ID",referencedColumnName = "ID")
public Person person;
#get..
#set..
}
`
#Entity
#Table(name = "LOGIN")
public class Login {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
private long id;
#Column(name = "USERNAME")
private String userName;
#Column(name = "PASSWORD")
private String password;
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "PERSON_ID", referencedColumnName = "ID")
private Person person;
#get..
#set..
}
It's been a while, but if I'm not mistaken you need to use the cascade=REMOVE option on the OneToMany and OneToOne relationships. In the OneToOne I think you need to specify cascade=REMOVE on the side that does NOT own the relationship, that is, the side that also contains the "mappedBy" property.
Finally, I believe JPA will NOT automatically load lazy relationships and then cascade them. I'm thinking you may need to fetch the relationship before you delete the parent entity (otherwise JPA will not know what to delete).
I have an entity called User with these fields :
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "user_id")
private Long id;
#Column(name = "name")
private String name;
#Column(name = "last_name")
private String lastName;
#OneToMany(mappedBy="userId")
private List<Survey> survey= new ArrayList<>();
And the Survey entity which has :
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "SURVEY_ID")
private Long Id;
#ManyToOne
#JoinColumn(name = "user_id",referencedColumnName="user_id")
private User userId;
.....
I want one User to be have many Surveys, and each survey is related to one user.
But there is something wrong with the way I've mapped it, cuz as JSON file, when I access allUsers I get this :
[{"id":1,"name":"User","lastName":"user","email":"user#user.com","surveyData":[{"userId":{"id":1,"name":"User","lastName":"user","email":"user#user.com","surveyData": ,...... and repeats itself
So instead of getting as list the values of the survey data, I get the values of the Users information ?
Can someone help me with this ?
Your mapping is correct.
Just use #JsonManagedReference in your User class and #JsonBackReference in your Survey Class. #JsonManagedReference is the forward part of reference – the one that gets serialized normally. #JsonBackReference is the back part of reference – it will be omitted from serialization.
In the User Class:
#OneToMany(mappedBy="userId")
#JsonManagedReference
private List<Survey> survey;
In the Survey Class:
#ManyToOne
#JoinColumn(name = "user_id",referencedColumnName="user_id")
#JsonBackReference
private User userId;
I have 2 remarks:
If the surveys are not ordered, you can consider to use a Set instead of a List.
I would also recommend to rename the class variable userId in the Survey class to user, since it is a User object and no identifier.
I have User and Role classes with ManyToMany relationship. When I'm adding Role object to List roles in User object the 'user_roles' table columns 'username' and 'role_name' gets populated with User and Role objects primary keys' which are their ids. I wanted to know whether its possible to reference not primary keys(ids) and get username and Role.name in those columns instead?
#Entity
#Table(name = "users")
#SecondaryTable(name = "user_info", pkJoinColumns = {
#PrimaryKeyJoinColumn(name = "info_id", referencedColumnName = "user_id") })
public class User {
#Id
#Column(name = "user_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "username")
private String username;
#Column(name = "password")
private String password;
#ManyToMany
#JoinTable(name="user_roles",
joinColumns= {#JoinColumn(name="username"/*, referencedColumnName="username"*/)},
inverseJoinColumns= {#JoinColumn( name="role_name"/*, referencedColumnName="role_name"*/)}
)
private List<Role> roles;
#Entity
#Table(name = "roles")
public class Role {
#Id
#Column(name = "role_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "role_name")
private String name;
#ManyToMany
private List<User> users;
Also if I remove the comments around referencedColumnName I get error when trying to fetch all users from the database. I haven't added any data to database so even when querying empty database I'm getting this error:
Caused by: org.hibernate.HibernateException: Found shared references
to a collection: com.recipee.model.User.roles
The reason I'm using such database schema is because I'm trying to use Tomcat realm authentication where I need username and role_name in one database table.
I am very new to hibernate and I am working with JPA and Hibernate4. Trying to insert parent object in child as onetoone relationship.
I went through some tutorials but All the example in the web shows, inserting both parent and child tables.
I want to insert data in child table only.
I have two tables called user and department.
User table consists of user details with department as onetoone relationship, as follows,
#Entity
#Table(name = "User")
public class UserEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "_id")
private String id;
#Column(name = "firstName")
private String firstName;
#Column(name = "lastName")
private String lastName;
#OneToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "departmentId")
private Department departmentId;
// getters and setters...
}
Below is my Department entity,
#Entity
#Table(name = "Department")
public class Department {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "_id")
private String id;
#Column(name = "name")
private String name;
// getters and setters...
}
In department table there is only 4 data. I want to insert data only in user data while insert into it and don't want to insert in Department.
How can I do that.Please assist.
You have to use mappedBy for this, as mentoned below in child Table, Department in your case
#OneToOne(mappedBy="department")
private UserEntity user;
These posts explain you better this,
JPA JoinColumn vs mappedBy
Understanding mappedBy annotation in Hibernate
You need to specify the relationship owner using mappedBy property in the OneToOne mapping in the owner side, here in your case in the Department class, you should add:
#OneToOne(mappedBy="department")
private UserEntity user;
I updated your code, to included the stated annotation and also renamed the Department property in your UserEntity class from departmentId to department to avoid confusion between relationship owner and its id:
#Entity
#Table(name = "User")
public class UserEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "_id")
private String id;
#Column(name = "firstName")
private String firstName;
#Column(name = "lastName")
private String lastName;
#OneToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "departmentId")
private Department department;
// getters and setters...
}
Below is the Department entity,
#Entity
#Table(name = "Department")
public class Department {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "_id")
private String id;
#Column(name = "name")
private String name;
#OneToOne(mappedBy="department")
private UserEntity user;
// getters and setters...
}
This will give you the right mapping with the expected behaviour.
In the #OneToOne annotation, the default value for parameter optional is true. So your annotation is the same as #OneToOne(fetch = FetchType.EAGER, optional = true). This means you can simply leave the Department in a UserEntity instance empty. In that case, persisting it results in persisting only a user entity and no department.
Even if you created a Department instance and assigned it to a UserEntity instance, persisting the UserEntity would not automatically persist the Department, since you don't have any cascade parameter in your annotation. If you don't automatically cascade persists, you would have to persist the Department first and then persist the corresponding user entity.
Maybe you're asking about using existing departments for your user entities. In that case, you first need to get the department via Hibernate (or the JPA API) from an entity manager. The entity instance you get is managed by Hibernate, and you can then set it in a UserEntity and persist that, to have it refer to the department.
Finally, I think one department will probably have more than one user. It might make more sense to have a #ManyToOne annotation instead of #OneToOne, indicating multiple users can refer to the same department, but that depends on your domain model.
I have a datamodel, which has:
a User entity which has a few fields specific to 2 users in the application
another entity UserDetails, which contains details specific to one particular type of user in the application besides the fields in User entity
Both entities share the same primary key. I am new to JPA. What kind of mappings should be there between the two?
#Entity
class User{
#Id
#Column(name="USER_ID")
private int id;
}
#Entity
class UserDetails{
#Id
#OneToOne(optional = false, fetch = FetchType.EAGER)
#JoinColumn(name = "USER_ID")
private User user;
...
}
The above mapping gives issues on fetching UserDetails for a particular User.
It requires that both Entities share the same primary key USER_ID.
You didn't mention the issues with the above mapping. It looks OK, but I would use a separate primary key for UserDetails table.
#Entity
class UserDetails{
#Id
private int id;
#OneToOne(optional = false, fetch = FetchType.EAGER)
#JoinColumn(name = "USER_ID")
private User user;
...
}
Also, it is a good practise to use bidirectional relationships, for eaiser navigation i.e. getting user details from User, you would just use user.getUserDetails(); so in User class:
#Entity
class User{
#Id
#Column(name="USER_ID")
private int id;
#OneToOne(mappedBy = "user")
private UserDetails userDetails;
}
In this case use OneToOne relationship. But make sure your database table UserDetailshas foreign key relationship to User table. Use below code to implement it using JPA and Hibernate.
#Entity
class User{
#Id
#Column(name="USER_ID")
private int id;
// getters and setters
}
#Entity
class UserDetails{
#Id
#Column(name="USER_DETAILS_ID")
private int userDetailsId;
#OneToOne(optional = false, fetch = FetchType.LAZY)
#JoinColumn(name = "USER_ID")
private User user;
// getters and setters
}