mysql - Cascading not working in oneToMany, with eclipseLink -
i working eclipselink , jpa 2.0.
those 2 entities:
feeder entity:
@entity @table(name = "t_feeder") public class feeder implements serializable { private static final long serialversionuid = 1l; //staff @onetomany(cascade = cascadetype.all, mappedby = "idattachedfeederfk") private collection<port> portcollection; //staff }
port entity:
@entity @table(name = "t_port") public class port implements serializable { //staff @joincolumn(name = "id_attached_feeder_fk", referencedcolumnname = "id") @manytoone private feeder idattachedfeederfk; //staff }
and code:
feeder f = new feeder(); //staff port p = new port(); p.setfeeder(f); save(feeder); //this function calls persist.
the probleme that, feeder persisted , not port. missing something? , specially, in side should mention cascading exactly. given in database, port table referencing feeder 1 foreign key.
edit
this simple piece of code worked fine me:
public static void main(string[] args) { address a1 = new address(); a1.setaddress("madinah 0"); employee e1 = new employee(); e1.setname("houssem 0"); e1.setaddressfk(a1); saveemplyee(e1); }
i not sure why expect work: attempting save new instance of feeder has no connection whatsoever newly created port.
by adding cascade @onetomany
, calling save(feeder)
eclipse link if there association:
- insert record feeder.
- iterate port collection , insert relevant records.
as have noted however, new feeder instance has no ports associated it.
with regard simple example assume when works both new address , employee have been written database. expected because have told employee address (e1.setaddressfk(a1);
) , saved employee. given presence of relevant cascade option both entities should written database expected.
given should obvious calling save(port)
work if necessary cascade option added @manytoone
side of relationship.
however if want call save(feeder)
need fix data model. should always ensure in-memory data model correct @ given point in time, viz. if first condition below true follows second condition must true.
port p = new port(); feeder feeder = new feeder(); p.setfeeder(f(); if(p.getfeeder().equals(f){ //true } if(f.isassociatedwithport(p)){ //bad --> returns false }
this best practice anyway ensuring correctnes of in-memory model should mean not experience type of issue seeing in jpa environment.
to ensure correctness of in-memory data model should encapsulate set/add operations.
Comments
Post a Comment