java - Adding a newly persisted entity to a list of entities held by the inverse side of a relationship so as to maintaining a bidirectional relationship -
given 2 entities department , employee forming one-to-many relationship department employee.
since relationship quite intuitive, leaving out entity classes.
the following segment of code, persists entity employee.
public void insert() { employee employee = new employee(); employee.setemployeename("k"); department department = entitymanager.find(department.class, 1l); employee.setdepartment(department); entitymanager.persist(employee); entitymanager.flush(); list<employee> employeelist = department.getemployeelist(); employeelist.add(employee); } and following method returns list of employees associated particular department.
public list<employee> getlist() { return entitymanager.find(department.class, 1l).getemployeelist(); } both methods written in stateless ejb using cmt (hereby not bmt) named let's employeeservice.
a client application invokes these methods in sequence so,
employeeservice.insert(); list<employee> employeelist = employeeservice.getlist(); (employee e : employeelist) { system.out.println(e.getemployeeid() + " : " + e.getemployeename()); } the sout statement in foreach loop above displays newly added employee entity list<employee> in department null employeeid in given line entitymanager.flush(); not present in first code snippet.
entitymanager#persist(object entity) not guaranteed generate id. id guaranteed generated @ flush time.
what happens is, if entitymanager.flush(); removed/commented, entity employee added list of employees (list<employee> employeelist) null identifier in (the primary key column in underlying database table).
what usual way maintain bidirectional relationship? entitymanager#flush(); needed every time entity added collection of entities being maintained inverse side of relationship generate id associated newly persisted entity?
also, required manually delete employee list<employee> (maintained inverse side of relationship - department) while deleting employee entity (using entitymanager.remove(employee);)?
edit : entity classes :
department :
@entity @table(catalog = "testdb", schema = "", uniqueconstraints = { @uniqueconstraint(columnnames = {"department_id"})}) public class department implements serializable { @id @generatedvalue(strategy = generationtype.identity) @basic(optional = false) @column(name = "department_id", nullable = false) private long departmentid; @column(name = "department_name", length = 255) private string departmentname; @column(length = 255) private string location; @onetomany(mappedby = "department", fetch = fetchtype.lazy) private list<employee> employeelist = new arraylist<employee>(0); private static final long serialversionuid = 1l; // constructors + getters + setters + hashcode() + equals() + tostring(). } employee :
@entity @table(catalog = "testdb", schema = "", uniqueconstraints = { @uniqueconstraint(columnnames = {"employee_id"})}) public class employee implements serializable { @id @generatedvalue(strategy = generationtype.identity) @basic(optional = false) @column(name = "employee_id", nullable = false) private long employeeid; @column(name = "employee_name", length = 255) private string employeename; @joincolumn(name = "department_id", referencedcolumnname = "department_id") @manytoone(fetch = fetchtype.lazy) private department department; private static final long serialversionuid = 1l; // constructors + getters + setters + hashcode() + equals() + tostring(). }
when persisting employee, need set both sides of association.
in department should have method:
public void addemployee(employee employee) { employees.add(employee); employee.setdepartment(this); } make sure cascade de persist , merge events children association:
@onetomany(cascade = cascadetype.all, mappedby = "department", orphanremoval = true) private list<employee> children = new arraylist<>(); and persisting logic becomes:
employee employee = new employee(); employee.setemployeename("k"); department department = entitymanager.find(department.class, 1l); department.addemployee(employee);
Comments
Post a Comment