JPA OneToOne not working -
i followed tutorial : http://www.codejava.net/frameworks/hibernate/hibernate-one-to-one-mapping-with-foreign-key-annotations-example
i have following code:
@entity @table(name = domainconstant.table_user) public class user{ @id @column(name = domainconstant.domain_user_id) @generatedvalue private long userid; private useractivationcode useractivationcode; ///////////////////// constructor.... /// standard , set.... @onetoone(cascade = cascadetype.all) @joincolumn(name = domainconstant.domain_activation_link_id) public useractivationcode getuseractivationcode() { return useractivationcode; } } @entity @table(name = domainconstant.table_user_activaton_link) public class useractivationcode { @id @column(name = domainconstant.domain_activation_link_id) @generatedvalue private long useractivationcodeid; @column(name = domainconstant.domain_activation_date) @temporal(javax.persistence.temporaltype.date) private date date; @column(name = domainconstant.domain_activation_code) private string code; ///////////////////// constructor.... /// standard , set.... }
when save user
object not make record in useractivationcode
, why?
like this:
user newuser = new user(); newuser.setuseractivationcode(new useractivationcode("this example")); userdao.save(newuser);
i have record in user table.
can tell me why?
your problem mixing access types. in user entity have specified @id on field (private long userid) whereas have defined join mapping on property (the getter useractivationcode). if specify join mapping on field, should work is.
@entity @table(name = domainconstant.table_user) public class user{ @id @column(name = domainconstant.domain_user_id) @generatedvalue private long userid; @onetoone(cascade = cascadetype.all) @joincolumn(name = domainconstant.domain_activation_link_id) private useractivationcode useractivationcode; ///////////////////// constructor.... /// standard , set.... public useractivationcode getuseractivationcode() { return useractivationcode; } }
for more information on access , access types, see access, java ee 7
Comments
Post a Comment