java - cascade not working while saving the data -
i have 1 one mapping relationship.
user.java
public class user { @id @sequencegenerator(name = "userid", sequencename = "userid") @generatedvalue(strategy = generationtype.auto, generator = "userid") @column(name = "id") private long id; private string firstname; private string lastname; @column(unique = true) private string username; private string password; @onetoone(mappedby = "user", cascade = cascadetype.all, fetch = fetchtype.lazy) @joincolumn(name = "id") private role role; public long getid() { return id; } public void setid(long id) { this.id = id; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public role getrole() { return role; } public void setrole(role role) { this.role = role; } @override public string tostring() { return "user [id=" + id + ", firstname=" + firstname + ", lastname=" + lastname + ", username=" + username + ", password=" + password + ", role=" + role + "]"; } }
roles.java
public class role { @id @sequencegenerator(name = "userid", sequencename = "userid") @generatedvalue(strategy = generationtype.auto, generator = "userid") @column(name = "roleid") private long roleid; @onetoone private user user; private integer role; public long getid() { return roleid; } public void setid(long id) { this.roleid = id; } public user getuser() { return user; } public void setuser(user user) { this.user = user; } public integer getrole() { return role; } public void setrole(integer role) { this.role = role; } }
i tring save data
public long create(user user) { long userid = super.save(user); return userid; }
when try save user entity seeing data saved below.
user table:
id firstname lastname usrename password 1 hello hello hello hello
role table:
roleid role user_id 1 1 null
can tell me user table not getting related each other.
i need user_id 1 instead of null.
here id of user table not getting mapped user_id of table role.
the role owner of association, user's @onetoone role doesn't need @joincolumn @ all:
@id @sequencegenerator(name = "userid", sequencename = "userid") @generatedvalue(strategy = generationtype.auto, generator = "userid") @column(name = "id") private long id; @onetoone(mappedby = "user", cascade = cascadetype.all, fetch = fetchtype.lazy) private role role;
when saving user need set both side of association:
user.setrole(role); role.setuser(user); userdao.create(user);
Comments
Post a Comment