-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Multiple Many-to-one using Dynamic Maps
PostPosted: Mon Oct 20, 2008 5:21 pm 
Newbie

Joined: Thu May 22, 2008 9:48 am
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2.6

Mapping documents:
<hibernate-mapping>
<class entity-name="Agents" table="AGENTS" schema="COMPASS_MI">
<id name="guid" type="string">
<column name="GUID" length="36" />
<generator class="assigned" />
</id>
<bag name="agentParticipantses" inverse="true" cascade="all">
<key>
<column name="A_GUID" length="36" not-null="true"/>
</key>
<one-to-many entity-name="AgentParticipants" />
</bag>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class entity-name="Participants" table="PARTICIPANTS" schema="COMPASS_MI">
<id name="guid" type="string">
<column name="GUID" length="36" />
<generator class="assigned" />
</id>
<bag name="agentParticipantses" inverse="true" cascade="all">
<key>
<column name="P_GUID" length="36" not-null="true"/>
</key>
<one-to-many entity-name="AgentParticipants" />
</bag>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class entity-name="AgentParticipants" table="AGENT_PARTICIPANTS" schema="COMPASS_MI">
<id name="guid" type="string">
<column name="GUID" length="36" />
<generator class="assigned" />
</id>
<many-to-one name="participants" entity-name="Participants" fetch="select">
<column name="P_GUID" length="36" not-null="true"/>
</many-to-one>
<many-to-one name="agents" entity-name="Agents" fetch="select">
<column name="A_GUID" length="36" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Map<String,Object> agent = new HashMap<String,Object>();
agent.put("guid", "012345678901234567890123456789123456");

Map<String,Object> participant = new HashMap<String,Object>();
participant.put("guid", "012345678901234567890123456789123456");

Map<String,Object> agentParticipant = new HashMap<String,Object>();
agentParticipant.put("guid", "012345678901234567890123456789123456");
agentParticipant.put("agents", agent);
agentParticipant.put("participants", participant);

agent.put("agentParticipantses", new ArrayList());
((List)agent.get("agentParticipantses")).add(agentParticipant);
participant.put("agentParticipantses", new ArrayList());
((List)participant.get("agentParticipantses")).add(agentParticipant);

sessionFactory.getCurrentSession().persist("Agents", agent);
// unsure if this step is redundant in this scheme
sessionFactory.getCurrentSession().persist("Participants", participant);
sessionFactory.getCurrentSession().flush();

/*
* Once the supporting objects are inserted, attempt to retrieve the agent participant
*/
Map<String,Object> returnedObject = (Map<String,Object>) sessionFactory.getCurrentSession().
createQuery("from AgentParticipants entity where entity.agents.guid = :guid").
setString("guid", "012345678901234567890123456789123456").uniqueResult();
assertNotNull(returnedObject);


Full stack trace of any exception that occurs:
org.hibernate.PropertyValueException: not-null property references a null or transient value: AgentParticipants.participants
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:609)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:601)
at org.hibernate.engine.CascadingAction$8.cascade(CascadingAction.java:295)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
at com.ciminc.compass.test.general.HibernateDynamicMaps.testDependencyHibernate(HibernateDynamicMaps.java:127)
..then some junit/spring stuff


Name and version of the database you are using:
Oracle 10g

The generated SQL (show_sql=true):
never does the inserts for the 3 entities used, but attempts the select for the AgentParticipant entity


Top
 Profile  
 
 Post subject: Fixed it
PostPosted: Tue Oct 21, 2008 10:06 am 
Newbie

Joined: Thu May 22, 2008 9:48 am
Posts: 13
I was being dumb. As a result of 2 not null foreign keys, I needed to persist both parents before the shared child could be persisted. If I tried to persist one and let the cascade work, the other parent would be null as it wasn't persisted yet, so this code works:

Map<String,Object> agent = new HashMap<String,Object>();
agent.put("guid", "012345678901234567890123456789123456");

Map<String,Object> participant = new HashMap<String,Object>();
participant.put("guid", "012345678901234567890123456789123456");

sessionFactory.getCurrentSession().persist("Participants", participant);
sessionFactory.getCurrentSession().persist("Agents", agent);

Map<String,Object> agentParticipant = new HashMap<String,Object>();
agentParticipant.put("guid", "012345678901234567890123456789123456");
agentParticipant.put("agents", agent);
agentParticipant.put("participants", participant);

agent.put("agentParticipantses", new ArrayList());
((List)agent.get("agentParticipantses")).add(agentParticipant);
participant.put("agentParticipantses", new ArrayList());
((List)participant.get("agentParticipantses")).add(agentParticipant);

sessionFactory.getCurrentSession().persist("AgentParticipants", agentParticipant);
sessionFactory.getCurrentSession().flush();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.