jpa - Entity Session bean - Persist Data into table and use the created Id to persist another table -


am new jpa , have learned lot forum. have problem.

i have 2 tables (members , member_next_of_kin). memberid auto incremental column in members table

the member_next_of_kin has referenced column memberid members tabke mandatory

i have method create new member.

public void addmember(members member) {     try {                   createnewmember(member);         nok.setmemberid(getmemberid());         addnextofkin(nok);     } catch (exception e) {         context.setrollbackonly();     } }  public members createnewmember(members member) {     memberfacade.create(member);     return memberid; } 

my aim persist create member , return created memberid , use insert in nextofkin table.

please show me how within transaction. memberid returning null.

in jpa model required behavior relationship - in case @onetomany instead of plain reference identifier of other entity.

jpa object-relational mapping, think in terms of oo design. lets try model possible based on information provided:

@entity public class member { @id @generatedvalue private long id;  @onetomany(mappedby="member") private set<nextofkin> kindred; } 

and

@entity public class nextofkin {     @id     @generatedvalue     private long id;      @manytoone     private member member;     } 

now of jpa complexity comes place, , best if first try basic grasp of of important concepts in jpa.

what have modeled here bidirectional one-to-many/many-to-one relationship in jpa terms. in sql, nextofkin table foreign key poiting member table. every bidirectional relationship has 1 owning side , 1 inverse side. owning side may or may not correspond natural relationship between 2 entities. in jpa, owner entity, responsible management of relationship(foreign keys) in database. according jpa spec, in case of bidirectional one-to-many relation, many side should owning side- in case nextofkin. designate inverse side using mappedby attribute of @onetomany performing db operations on owner can correctly manage relationship in db. there multiple ways how achieve functionality require:

lets start empty db, no members , no nextofkins. example implemented this.

member newmember = new member(); nextofkin nok = new nextofkin(); createnewmember(newmember);//here em.persist() should happen //after save, member.getid() return generated long identifier  //because annotated id attribute @generatedvalue //nextofkin owner, therefore need correct in  memory relationship    point of view  nok.setmember(newmember); createnewnok(nok);//again persist entity //end of transaction 

after this, relationship in db should correct - nextofkin table has 1 entry, generated long id , fk pointing entry in member table. information in db correct, though in java, member did not have elements in kindred collection. can verify loading member db e.g.

member fromdb = entitymanager.find(member.class, newmember.getid()); asserttrue("member should have 1 next of kin now",      fromdb.getkindred().size() == 1); //and other side nextofkin nokfromdb = entitymanager.find(nextofkin.class, nok.getid()); asserttrue("next of kin should have member",      nokfromdb.getmember().getid() == newmember.getid()) 

there other options, example, add cascade behaviour relationship, db operation performed on member cascaded kindred:

//...in member @onetomany(cascade=cascadetype.all, mappedby="member") //... saving member save nextofkin well, //but remember, have set correct information in memory // point of view of owner member newmember = new member(); nextofkin nok = new nextofkin(); nok.setmember(newmember); createnewmember(newmember); 

another option make relationship unidirectional - nextofkin not have relation member. therefore operations performed on member table.

for more information on jpa owning/inverse side have @ popular questions here @ , e.g. in bidirectional jpa onetomany/manytoone association, meant “the inverse side of association”?

jpa , orm in general complex subject, , highly recommend spending time documentation or book. minimum knowledge is, in opinion, required productively use jpa basic entity mapping, relationship mappings, fetching strategies, understanding of owner/inverse concept , cascading.

you can find many sources online , free, varying levels of complexity.
useful resources:

good luck , not forget, there more 1 of how achieve goals. if find jpa complex, can try other java db frameworks jooq or mybatis.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -