-->
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: Duplicate: Assigned Id Objects and cascading persistence
PostPosted: Wed Jun 22, 2005 3:29 pm 
Newbie

Joined: Wed Jun 22, 2005 9:03 am
Posts: 4
Resubmiting did not see the 1st one come through
Hibernate version:
[INFO] Environment - Hibernate 3.0.5


Mapping documents:
other mappings are not needed, they are not shown
Network.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.test.opcam">
<class name="com.test.opcam.Network">
<id name="id">
<generator class="sequence"/>
</id>
<property name="name" column="network" type="string"/>

<bag name="devBasics" table="devbasic" lazy="true" cascade="save-update">
<key>
<column name="nwk_id" not-null="true"/>
</key>

<one-to-many class="com.test.opcam.DevBasic"/>
</bag>

</class>
</hibernate-mapping>


DevBasic.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="com.test.opcam.DevBasic" table="DevBasic">
<meta attribute="class-description">
Represents a single playable track in the music database.
@author Jim Elliott (with help from Hibernate)
</meta>

<id name="id" type="int" column="Id">
<meta attribute="scope-set">protected</meta>
<!--generator class="native"/-->
<generator class="assigned"/>
</id>

<property name="name" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="name" not-null="true" index="devBas_name"/>
</property>


<bag name="addresses" table="dev_addr" lazy="true" cascade="save-update" inverse="true">
<key>
<column name="dev_id" not-null="true"/>
</key>

<one-to-many class="com.test.opcam.DevAddr"/>
</bag>

<!-- The other side of this bidirectional one-to-many association to item. -->
<many-to-one name="parentNetwork" class="com.test.opcam.Network"
column="nwk_id" update="true" cascade="none" not-null="false"
outer-join="false"
/>

<map name="seqNbrMap"
table="devb_seq_nbr" cascade="all">
<key column="dev_basid"/>
<index column="seqnbrkey" type="string"/>
<!--index-many-to-many column = "seqnbrid"
class = "com.test.opcam.SeqNbr" /-->
<many-to-many column="seqnbrid" class="com.test.opcam.SeqNbr"/>
</map>




</class>


</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Configuration config = new Configuration();
SessionFactory sessionFactory = config.buildSessionFactory();
// Ask for a session using the JDBC information we've configured
Session session = sf.openSession();
Transaction tx = null;
int currId = 0;
try {
// Create some data and persist it
tx = session.beginTransaction();
Network aNwk = new Network("non-default");
DevAddr addr1 = new DevAddr();
addr1.setAddress("addr4");
addr1.setId(new Integer(currId++));
DevAddr addr2 = new DevAddr();
addr2.setAddress("addr5");
addr2.setId(new Integer(currId++));
ArrayList al = new ArrayList();
al.add(addr1);
al.add(addr2);
DevBasic devb1 = new DevBasic("devBasic1", al);
devb1.setId(new Integer(currId++));
devb1.setParentNetwork(aNwk);

addr1.setDevBasic(devb1);
addr2.setDevBasic(devb1);
ArrayList devColl = new ArrayList();
devColl.add(devb1);
SeqNbr aSeqNbr1 = new SeqNbr("nbr1");
SeqNbr aSeqNbr2 = new SeqNbr("nbr2");
HashMap aMap = new HashMap();
aMap.put(aSeqNbr1.getName(), aSeqNbr1);
aMap.put(aSeqNbr2.getName(), aSeqNbr2);
devb1.setSeqNbrMap(aMap);

aNwk.setDevBasics(devColl);

session.saveOrUpdate(aNwk);



// We're done; make our changes permanent
tx.commit();

} catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}

Full stack trace of any exception that occurs:
no exception happens

Name and version of the database you are using:
Oracle 9i version Release 9.2.0.1.0

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


More Information
I have Java class Network which (has one to many relationship) with object DevBasic.
classInformation here:

public class Network implements Serializable {

/** identifier field */
private Integer id;

/** nullable persistent field */
private String name;

/** persistent field */
private Collection devBasics;

private AssignedId assId;
/** full constructor */
public Network(String name) {
this.name = name;
}
..
.. //methods not shown
}

public class DevBasic implements Serializable {

/** identifier field */
private Integer id;

/** nullable persistent field */
private String name;

/** persistent field */
private List addresses;

private Map seqNbrMap;

private Network parentNetwork;
...
..
// once again methods suppressed
}

The id generation for Network is sequence based, while I am assigning the id generation for DevBasic objects from outside.

The problem that I am having is when I am assigning IDS for DevBasic from outside (the appication does on its own) cascading persistence (that is when Network object is saved, it should save DevBasic) is not happening.
However when I convert the Id generation in DevBasic to type sequence or native it works, that is let hibernate do it, it works well.

Question is can Hibernate identify and use persistence by reachibilty only if ID (object is null) because I do not want to do individual save on every object (when I am assigning ids from outside), and let hibernate determine if the object is new or not. Looks like when I assign IDS from outside cascade update feature does not work.
Or do I need some other additional mapping which let hibernate do this work.

Thanks in advance.

_________________
Amish Shah
OPNET Technologies


Top
 Profile  
 
 Post subject: Can anyone please throw some light on this
PostPosted: Thu Jun 23, 2005 5:32 pm 
Newbie

Joined: Wed Jun 22, 2005 9:03 am
Posts: 4
As we are migrating to hibernate using existing application, the ids are getting assigned and need to have the knowledge of what is the IDS before commit happens. Can anyone reply to the above, if they have knowledge on it.
Amish

_________________
Amish Shah
OPNET Technologies


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.