java - persist many-to-many relationship with EJB -
i'm using jpa (hibernate), ejb , cdi bean (jsf). have 2 tables: technology (entity technology.class) , component (entity component.class) many-to-many relationship. entity code:
public class technology implements serializable{ ... @lob @column(nullable=false) private string title; ... @manytomany(mappedby="technologies") private list<component> components; .. } public class component implements serializable { ... @manytomany @jointable( name="technology_has_component" , joincolumns={ @joincolumn(name="component_title", nullable=false) } , inversejoincolumns={ @joincolumn(name="technology_tid", nullable=false) } ) private list<technology> technologies; ... }
code in ejb:
@stateless @localbean public void addtech(technology tech) throws exception { em.persist(tech); }
my jsf page uses cdi bean properties , method:
@named(value = "adminactiontech") @sessionscoped public class adminactiontech implements serializable{ ... public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string addtech() { technology tech = new technology(); tech.settitle(title); ... tech.setcomponents(getlistavaicomps()); try { techservice.addtech(tech); } catch (exception e) { e.printstacktrace(); } } private list<component> getlistavaicomps() { list<component> listnewcomponent = new arraylist<component>(); component findcomp = compservice.findcomp(component.gettitle()); // findcomp() method in ejb listnewcomponent.add(findcomp); return listnewcomponent; }
when add new technology title,....,list components. except list components not added. checked technology table, new record created, technology_has_component table has not more record added. debug, , sure, getlistavailcomps method not null. can show me how fix. thanks
you need set @cascadetype
on relationship including persist
if want new objects persisted when container is.
Comments
Post a Comment