java - JPA OneToOne join issue -
here simplecost entity
@entity @table(name="simplecost") public class simplecost { @id @generatedvalue(strategy = generationtype.identity) private long id; private long userid; private long cost; @onetoone(cascade=cascadetype.all,fetch = fetchtype.eager) @joincolumn(name = "userid") private userbare user; }
userbare:
@entity @table(name="user") public class userbare{ @id @generatedvalue(strategy = generationtype.identity) private long id; private long userid; private string fname; private string lname; }
so i'm having trouble populating simplecost.user attribute. need use simplecost.userid retreive user table data , fill simplecost.user object. here database structure.
database tables: user
create table `user` ( `id` bigint(20) not null auto_increment, `fname` varchar(100) default null, `lname` varchar(100) default null, `mname` varchar(100) default null, `title` varchar(100) default null, `email` varchar(100) default null, primary key (`id`), unique key `id_unique` (`id`) ) engine=innodb auto_increment=9 default charset=latin1; /*!40101 set character_set_client = @saved_cs_client */;
simplecost
create table `simplecost` ( `id` bigint(20) not null auto_increment, `userid` bigint(20) default null, `liablecost` decimal(10,2) default null, `isactive` tinyint(1) default null, primary key (`id`), unique key `id_unique` (`id`), key `fk_simplecost_user1_idx` (`userid`), constraint `fk_simplecost_user` foreign key (`userid`) references `user` (`id`) on delete no action on update no action ) engine=innodb auto_increment=13 default charset=latin1; /*!40101 set character_set_client = @saved_cs_client */;
you don't need 'userid' columns defined in entity (java class). 1 in userbare
useless , 1 in simplecost
created/used because of @joincolumn(name = "userid")
added 'user' field'
remove private long userid;
both entity declaration.
other that, need define "having trouble populating simplecost.user attribute".
Comments
Post a Comment