java - JPA creating a trimmed down entity version of an existing Entity -
i have angular client , rest backend through spring rest/mvc. database mysql , i'm using jpa hibernate orm.
some of front end views require me de-normalize domain-data on java side. have typical user domain object id,fname,lname etc.. user object points 'user' table in database. nothing unusual.
the problem have object "simplecost" has following structure
public class simplecost { private long id; private long userid; private long cost; }
a collection of above objects utilized frontend generate view this
userid cost 1 5.5 2 7.5 3 10.00
but view supposed
name cost john doe 5.5 jane doe 7.5 rusty shackleford 10.00
so instead of sending userid simplecost object, need send fname , lname.
to solve problem tried (failing miserably) following approach. created new domain object called "userbare". trimmed down version of "user". mean "userbare" has fname , lname.
i added "userbare" new member of simplecost object
new proposed structure of simplecost
public class simplecost { private long id; private long userid; private long cost; private userbare user; }
but i'm unable populate user object using jpa. tried onetoone join didn't work. tried adding string fname, string lname fields simplecost instead , @secondarytable annotation populate values @column(table="user", name="fname"). didn't work well.
here database structure
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 */;
if want join work have this:
public class simplecost { private long id; private user user; private long cost; } public class user{ private long id; private string fname; private string lname; priavte string mname; priavate string title; private string email; }
also if rename columns "id"
"userid"
, "simplecostid"
Comments
Post a Comment