-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate Self-Association
PostPosted: Tue Jun 26, 2007 2:47 pm 
Beginner
Beginner

Joined: Tue Jun 26, 2007 2:31 pm
Posts: 21
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
Hibernate Version 3.0

Mapping documents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.ibm.hibernate.Note" table="note">

<id name="noteId" column="note_id">
<generator class="assigned"></generator>
</id>

<property name="createDt" column="create_dt"></property>

<property name="crtdBy" column="crtd_by"></property>

<property name="effvDt" column="effv_dt"></property>

<property name="endDt" column="end_dt"></property>

<property name="note1" column="note_topic"></property>

<property name="updtdBy" column="updtd_by"></property>

<property name="updtDt" column="updt_dt"></property>

<!--property name="subrdtNoteId" column="subrdt_note_id"></property-->
<!-- child-->
<many-to-one name="note" class="com.ibm.hibernate.Note">
<column name="subrdt_note_id"></column>
</many-to-one>

<set name="noteassn">
<key column="note_id"></key>
<one-to-many class="com.ibm.hibernate.NoteAssn"/>
</set>

<set name="notes">
<key column="note_id"></key>
<one-to-many class="com.ibm.hibernate.Note"/>
</set>


</class>

</hibernate-mapping>

*************************************************************
The way i am testing my code is here:

package com.ibm.hibernatetest;

import java.util.Set;

import com.ibm.hibernate.Note;
import com.ibm.hibernatedao.NoteDAO;


public class NoteDAOTest {

public static void main(String args[])
{

NoteDAO dao = new NoteDAO();
Note note = dao.findById(new Long(1));
System.out.println(note.getNote1());


Note noteSub = new Note();
Long l1 = new Long(5);
noteSub.setCreateDt("11-MAY-07");
noteSub.setUpdtDt("09-JUN-07");
noteSub.setCrtdBy("NAVEEN");
noteSub.setUpdtdBy("NAVEEN");
noteSub.setEffvDt("10-JUN-05");
noteSub.setEndDt("10-JUN-09");
noteSub.setNoteId(l1);
noteSub.setNote1("EMPLOYER CASE DETAILS");

noteSub.setNote(note);
note.getNotes().add(noteSub);

System.out.println("Done");

dao.appendNote(note);

}

}

This should actually update the DataBase but there is nothing actually updated



Code between sessionFactory.openSession() and session.close():

public void appendNote(Object object)
{
log.debug("Appending Notes to Subordinate Note");
try
{
Transaction tx = getSession().beginTransaction();
getSession().saveOrUpdate(object);
tx.commit();
}
catch(RuntimeException re)
{
log.error("Append Failed",re);
throw re;
}
finally
{
getSession().flush();
System.out.println("Session Flushed");
getSession().close();
System.out.println("Session Closed");
}
}

Full stack trace of any exception that occurs:

14:28:18,031 INFO Environment:479 - Hibernate 3.1.3
14:28:18,046 INFO Environment:509 - hibernate.properties not found
14:28:18,046 INFO Environment:525 - using CGLIB reflection optimizer
14:28:18,046 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
14:28:18,140 INFO Configuration:1308 - configuring from resource: hibernate.cfg.xml
14:28:18,140 INFO Configuration:1285 - Configuration resource: hibernate.cfg.xml
14:28:18,468 INFO Configuration:469 - Reading mappings from resource: config/entity.hbm.xml
14:28:18,625 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.Entity -> entity
14:28:18,734 INFO Configuration:469 - Reading mappings from resource: config/entitytyp.hbm.xml
14:28:18,781 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.EntityTyp -> entity_typ
14:28:18,781 INFO Configuration:469 - Reading mappings from resource: config/entrprs.hbm.xml
14:28:18,828 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.EnterpriseNameType -> entrprs_nm_typ
14:28:18,843 INFO Configuration:469 - Reading mappings from resource: config/intdparty.hbm.xml
14:28:18,875 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.IntdPty -> intd_pty
14:28:18,906 INFO HbmBinder:883 - Mapping joined-subclass: com.ibm.hibernate.IndivEntity -> indiv_entity
14:28:18,906 INFO HbmBinder:883 - Mapping joined-subclass: com.ibm.hibernate.Worker -> indiv_entity
14:28:18,921 INFO HbmBinder:883 - Mapping joined-subclass: com.ibm.hibernate.BusnsEntity -> busns_entity
14:28:18,921 INFO HbmBinder:883 - Mapping joined-subclass: com.ibm.hibernate.Employer -> employer
14:28:18,921 INFO Configuration:469 - Reading mappings from resource: config/intdptyassn.hbm.xml
14:28:18,937 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.IntdPtyAssn -> intd_pty_assn
14:28:18,937 INFO Configuration:469 - Reading mappings from resource: config/intdptynm.xml
14:28:18,968 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.IntdPtyNm -> intd_pty_nm
14:28:18,968 INFO Configuration:469 - Reading mappings from resource: config/intdptyrole.hbm.xml
14:28:18,984 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.IntdPtyRole -> intd_pty_role
14:28:18,984 INFO Configuration:469 - Reading mappings from resource: config/intdptyrolecat.hbm.xml
14:28:19,000 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.IntdPtyRoleCat -> intd_pty_role_cat
14:28:19,000 INFO Configuration:469 - Reading mappings from resource: config/naics.hbm.xml
14:28:19,015 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.Naics -> naics
14:28:19,015 INFO Configuration:469 - Reading mappings from resource: config/ofcrownrship.hbm.xml
14:28:19,031 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.OfcrOwnrshp -> ofcr_ownrshp
14:28:19,031 INFO Configuration:469 - Reading mappings from resource: config/ofcrtitle.hbm.xml
14:28:19,046 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.OfcrTitle -> ofcr_title
14:28:19,046 INFO Configuration:469 - Reading mappings from resource: config/roletyp.hbm.xml
14:28:19,062 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.RoleTyp -> role_typ
14:28:19,062 INFO Configuration:469 - Reading mappings from resource: config/intdptytypassn.hbm.xml
14:28:19,078 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.IntdPtyTypAssn -> intd_pty_typ_assn
14:28:19,078 INFO Configuration:469 - Reading mappings from resource: config/note.hbm.xml
14:28:19,093 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.Note -> note
14:28:19,093 INFO Configuration:469 - Reading mappings from resource: config/noteassn.hbm.xml
14:28:19,109 INFO HbmBinder:309 - Mapping class: com.ibm.hibernate.NoteAssn -> note_assn
14:28:19,109 INFO Configuration:1419 - Configured SessionFactory: null
14:28:19,109 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.Entity.officer -> ofcr_title
14:28:19,109 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.EntityTyp.intdPtyRole -> intd_pty_role
14:28:19,109 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.EnterpriseNameType.intdptynm -> intd_pty_nm
14:28:19,109 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.IntdPty.entity -> entity
14:28:19,109 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.IntdPty.intdptytypassn -> intd_pty_typ_assn
14:28:19,109 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.IntdPty.notesassn -> note_assn
14:28:19,109 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.BusnsEntity.intdptynm -> intd_pty_nm
14:28:19,109 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.IntdPtyRoleCat.intdPtyRole -> intd_pty_role
14:28:19,109 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.RoleTyp.intdPtyRole -> intd_pty_role
14:28:19,125 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.Note.noteassn -> note_assn
14:28:19,125 INFO HbmBinder:2349 - Mapping collection: com.ibm.hibernate.Note.notes -> note
14:28:19,296 WARN RootClass:210 - composite-id class does not override equals(): com.ibm.hibernate.OfcrOwnershipId
14:28:19,296 WARN RootClass:215 - composite-id class does not override hashCode(): com.ibm.hibernate.OfcrOwnershipId
14:28:19,312 WARN RootClass:210 - composite-id class does not override equals(): com.ibm.hibernate.OfcrTitleId
14:28:19,312 WARN RootClass:215 - composite-id class does not override hashCode(): com.ibm.hibernate.OfcrTitleId
14:28:19,328 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
14:28:19,328 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
14:28:19,328 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
14:28:19,343 INFO DriverManagerConnectionProvider:80 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@Naveen:1521:XE
14:28:19,343 INFO DriverManagerConnectionProvider:86 - connection properties: {user=naveen, password=****}
14:28:19,687 INFO SettingsFactory:77 - RDBMS: Oracle, version: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
14:28:19,687 INFO SettingsFactory:78 - JDBC driver: Oracle JDBC driver, version: 10.1.0.4.2
14:28:19,703 INFO Dialect:103 - Using dialect: org.hibernate.dialect.Oracle9Dialect
14:28:19,703 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
14:28:19,718 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
14:28:19,718 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
14:28:19,718 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
14:28:19,718 INFO SettingsFactory:136 - JDBC batch size: 15
14:28:19,718 INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
14:28:19,718 INFO SettingsFactory:144 - Scrollable result sets: enabled
14:28:19,718 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): disabled
14:28:19,718 INFO SettingsFactory:160 - Connection release mode: auto
14:28:19,718 INFO SettingsFactory:187 - Default batch fetch size: 1
14:28:19,718 INFO SettingsFactory:191 - Generate SQL with comments: disabled
14:28:19,718 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
14:28:19,718 INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
14:28:19,718 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
14:28:19,718 INFO SettingsFactory:203 - Query language substitutions: {}
14:28:19,718 INFO SettingsFactory:209 - Second-level cache: enabled
14:28:19,718 INFO SettingsFactory:213 - Query cache: disabled
14:28:19,718 INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.EhCacheProvider
14:28:19,718 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
14:28:19,718 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
14:28:19,734 INFO SettingsFactory:257 - Echoing all SQL to stdout
14:28:19,734 INFO SettingsFactory:264 - Statistics: disabled
14:28:19,734 INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
14:28:19,734 INFO SettingsFactory:283 - Default entity-mode: pojo
14:28:19,765 INFO SessionFactoryImpl:154 - building session factory
14:28:19,765 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/Program%20Files/MyEclipse%205.1.1%20GA/eclipse/plugins/com.genuitec.org.hibernate.eclipse_5.1.1/myeclipse-data/3.0/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
14:28:20,421 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
Hibernate: select note0_.note_id as note1_16_0_, note0_.create_dt as create2_16_0_, note0_.crtd_by as crtd3_16_0_, note0_.effv_dt as effv4_16_0_, note0_.end_dt as end5_16_0_, note0_.note_topic as note6_16_0_, note0_.updtd_by as updtd7_16_0_, note0_.updt_dt as updt8_16_0_, note0_.subrdt_note_id as subrdt9_16_0_ from note note0_ where note0_.note_id=?
CLAIM INFO
Hibernate: select notes0_.note_id as note1_1_, notes0_.note_id as note1_16_0_, notes0_.create_dt as create2_16_0_, notes0_.crtd_by as crtd3_16_0_, notes0_.effv_dt as effv4_16_0_, notes0_.end_dt as end5_16_0_, notes0_.note_topic as note6_16_0_, notes0_.updtd_by as updtd7_16_0_, notes0_.updt_dt as updt8_16_0_, notes0_.subrdt_note_id as subrdt9_16_0_ from note notes0_ where notes0_.note_id=?
Done
Hibernate: update note set note_id=? where note_id=?
Session Flushed
Session Closed


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

The generated SQL (show_sql=true):
Hibernate: select note0_.note_id as note1_16_0_, note0_.create_dt as create2_16_0_, note0_.crtd_by as crtd3_16_0_, note0_.effv_dt as effv4_16_0_, note0_.end_dt as end5_16_0_, note0_.note_topic as note6_16_0_, note0_.updtd_by as updtd7_16_0_, note0_.updt_dt as updt8_16_0_, note0_.subrdt_note_id as subrdt9_16_0_ from note note0_ where note0_.note_id=?

CLAIM INFO

Hibernate: select notes0_.note_id as note1_1_, notes0_.note_id as note1_16_0_, notes0_.create_dt as create2_16_0_, notes0_.crtd_by as crtd3_16_0_, notes0_.effv_dt as effv4_16_0_, notes0_.end_dt as end5_16_0_, notes0_.note_topic as note6_16_0_, notes0_.updtd_by as updtd7_16_0_, notes0_.updt_dt as updt8_16_0_, notes0_.subrdt_note_id as subrdt9_16_0_ from note notes0_ where notes0_.note_id=?

Done

Hibernate: update note set note_id=? where note_id=?


Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html





Last edited by sainaveenp on Tue Jun 26, 2007 8:33 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 26, 2007 4:14 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Try this:

Quote:
<set name="notes">
<key column="note_id"></key>
<one-to-many class="com.ibm.hibernate.Note"/>
</set>


Change the key column name in this mapping to 'subrdt_note_id'. Then it should work fine.

Regards,
Jitendra

*Please don't forget to rate*


Top
 Profile  
 
 Post subject: Thank you
PostPosted: Tue Jun 26, 2007 6:47 pm 
Beginner
Beginner

Joined: Tue Jun 26, 2007 2:31 pm
Posts: 21
I Tried changing it but it did not work for me.....let me know if i have to change anything else


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 3:34 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Kindly repost the details again with this new setting and explain your problem. You cannot expect us to read the log and then find out the problem.

Regards,
Jitendra


Top
 Profile  
 
 Post subject: I am reposting after the changes i made to the mapping file
PostPosted: Wed Jun 27, 2007 9:55 am 
Beginner
Beginner

Joined: Tue Jun 26, 2007 2:31 pm
Posts: 21
My problem is i could not update the database after i inserted the values

Kindly let me know if i need to make any changes to the Controller (marked in Blue) I guess the main problem is with what i am doing in the Controller...
**********************************************************
This is the mapping file note.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.ibm.hibernate.Note" table="note">

<id name="noteId" column="note_id">
<generator class="assigned"></generator>
</id>

<property name="createDt" column="create_dt"></property>

<property name="crtdBy" column="crtd_by"></property>

<property name="effvDt" column="effv_dt"></property>

<property name="endDt" column="end_dt"></property>

<property name="note1" column="note_topic"></property>

<property name="updtdBy" column="updtd_by"></property>

<property name="updtDt" column="updt_dt"></property>


<!-- child-->
<many-to-one name="note" class="com.ibm.hibernate.Note">
<column name="subrdt_note_id"></column>
</many-to-one>

<set name="noteassn">
<key column="note_id"></key>
<one-to-many class="com.ibm.hibernate.NoteAssn"/>
</set>

<set name="notes">
<key column="subrdt_note_id"></key>
<one-to-many class="com.ibm.hibernate.Note"/>
</set>


</class>

</hibernate-mapping>

******************************************************
This the Controller where i am inserting the values into the database:


public class NoteDAOTest {

public static void main(String args[])
{
NoteDAO dao = new NoteDAO();
Long l = new Long(4);
Note note = (Note)dao.findById(new Long(1));


Note noteSub = new Note();
noteSub.setCreateDt("09-JUN-07");
noteSub.setCrtdBy("NAVEEN");
noteSub.setUpdtDt("10-JUN-09");
noteSub.setUpdtdBy("NAVEEN");
noteSub.setEffvDt("09-JUL-07");
noteSub.setEndDt("10-AUG-09");
noteSub.setNote1("EMPlOYEE CASE DETAILS");
noteSub.setNoteId(l);

// noteSub.setNote(note);
// dao.create(noteSub);
note.getNotes().add(noteSub);
note.setNote(noteSub);

dao.appendNote(note);


}

}

******************************************************


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 11:45 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Would like to see the code inside appendNote method. Are you flushing the session before closing ?

This one works:
Code:
       <class name="Self">
      <id name="id" column="id" type="long" unsaved-value="0">
          <generator class="increment"/>
      </id>
      
      <property name="name"/>
      
      <set name="children"  cascade="all-delete-orphan">
         <key column="parent_id"/>
         <one-to-many class="Self" />
      </set>
      
      <many-to-one name="parent" column="parent_id" class="Self"/>
      
   </class>


With this test:
Code:

      Self a = new Self();
      a.setName("parent");
      Session s = factory.openSession();
      s.save(a);
      
      s.flush();
      
      a = (Self)s.load(Self.class, a.getId());
      
      Self b = new Self();
      b.setName("child1");
      
      a.getChildren().add(b);
      
      b = new Self();
      b.setName("child2");
      
      a.getChildren().add(b);
      
      s.save(a);
      
      s.flush();
      
      s.close();
      
   



*Please do rate if it helps*

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 7:30 pm 
Beginner
Beginner

Joined: Tue Jun 26, 2007 2:31 pm
Posts: 21
Hi it works finee thank you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.