java - Order to save OneToMany relationship with JPA and Hibernate -
java - Order to save OneToMany relationship with JPA and Hibernate -
i have these 2 beans:
public class agentsubscription { //... @onetomany(fetch = fetchtype.eager, cascade = {cascadetype.persist, cascadetype.merge, cascadetype.remove}, mappedby = "subscription") @fetch(fetchmode.subselect) @orderby("id_subscription") private list<outputchannel> channels; //... }
and
public class outputchannel { //... @manytoone(fetch = fetchtype.eager) @joincolumn(name = "id_subscription", nullable = false) private agentsubscription subscription; //... }
if seek utilize method:
@transactional public void addchannel(agentsubscription subscription, outputchannel channel) { seek { list<outputchannel> channels = new arraylist<outputchannel>(); channels.add(channel); subscription.setchannels(channels); subscription = agentsubscriptiondao.insert(subscription); } grab (exception e) { logger.error("unable add together channel subscription " + subscription.getsubscriptionid()); } }
we exception:
javax.persistence.persistenceexception: org.hibernate.propertyvalueexception: not-null property references null or transient value: com.domain.agent.beans.channel.outputchannel.subscription
shouldn't hibernate save subscription before channel? problem cascadetype?
first of all, need set subscription
in channel
object, references , value not nullable. cause of exception recieved. type of mapping creates agentsubscription
foreign key in outputchannel
table, have set it, cascading or not. hibernate not perform autosetting or sort of that. in terms of code, adding
channel.setsubscription(subscription);
before dao insert phone call should solve issue. way when entitymanager.persist(subscription)
(or persist) called, channel
persisted too.
java hibernate jpa-2.0
Comments
Post a Comment