-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problems trying to retrieve data from joined table
PostPosted: Fri Jan 19, 2007 11:10 am 
Newbie

Joined: Fri Jan 19, 2007 10:29 am
Posts: 4
Location: Ireland
My problem relates to, after setting up the join between two tables, and the join seeming to work, I cannot access the data of the table I have joined to within the 'main' table List instance variable? E.g. as in testing code below, the table bond is joined to the table BondRelationship. When the tables are joined with criteria 'bond_no_ser = 5', 11 rows should be returned, and this happens correctly according to the count I make as below in the jhava code, however, when I want to look at the data in the 11 BondRelationship objects, i.e. iterate through the List as below, the program , I am not getting the values back from the toString method.

In summary, the main problem below comes from this snippet:

BondRelationship rel = (BondRelationship)bond.getBondRelationship().get(i);

Any help with this would be greatly appreciated,

Thanks for your time,
John


Hibernate version:3.2

Mapping documents:

hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- hibernate.cfg.xml -->
<!-- Database connection settings -->
<!-- property name="connection.driver_class">org.hsqldb.jdbcDriver</property -->
<property name="connection.driver_class">com.informix.jdbc.IfxDriver</property>

<!-- property name="connection.url">jdbc:hsqldb:hsql://localhost</property -->
<property name="connection.url">jdbc:informix-sqli://devdub01:1528/sei_dev_focus:INFORMIXSERVER=devdub01;GL_DATE=%d%m%Y;FET_BUF_SIZE=32767</property>

<property name="connection.username">phelanj</property>
<property name="connection.password">Tipp456</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>

<!-- mapping resource="events/Event.hbm.xml"/-->
<mapping resource="tables/FundApproved.hbm.xml"/>
<mapping resource="tables/Bond.hbm.xml"/>
<mapping resource="tables/BondRelationship.hbm.xml"/>
</session-factory>

</hibernate-configuration>


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

<hibernate-mapping>

<class name="tables.Bond" table="bond">
<id name="bondNoSer" column="bond_no_ser">
<generator class="native"/>
</id>

<property name="productLabel" column="product_label"/>
<property name="cancelPeriod" column="cancel_period"/>
<property name="policyNumb" column="policy_numb"/>


<!-- <join table="bond_relationship">
<key>
<column name="bond_no_ser" not-null="true"/>
</key>
</join> -->
<!-- order-by="bond_no_ser" -->

<bag
name="BondRelationship"
inverse="true"
cascade="all">

<key column="bond_no_ser"/>
<one-to-many class="tables.BondRelationship"/>

</bag>


</class>


</hibernate-mapping>



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

<hibernate-mapping>

<class name="tables.BondRelationship" table="bond_relationship">
<id name="bondRelationshipId" column="bond_rel_ser">
<generator class="assigned"/>
</id>

<!--property name="" column=""/-->

<property name="clintTypeLabel" column="clinttyp_label"/>
<property name="bondNoSer" column="bond_no_ser"/>
</class>


</hibernate-mapping>


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

Session session = HibernateUtil.getSessionFactory().getCurrentSession();

System.out.println("Beginning transaction");
session.beginTransaction();

System.out.println("Get bond");
// List result = session. createSQLQuery("Select * from bond WHERE bond_no_ser = 5848").list();

Bond bond = null;

Query q = session.createQuery("FROM " +
"Bond as b " +
"left outer join fetch b.BondRelationship " +
"WHERE b.bondNoSer = :bondNum");
q.setParameter("bondNum", new Long(5));
bond = (Bond)q.uniqueResult();

int count = bond.getBondRelationship().size();
System.out.println("count: " + count);

Iterator listItr = bond.getBondRelationship().listIterator(count - 1);
while(listItr.hasNext()) {
System.out.println("1");
BondRelationship rel = (BondRelationship)listItr.next();
System.out.println("2");
rel.toString();
System.out.println("3");
}

Full stack trace of any exception that occurs:

C:\Documents and Settings\phelanj\Desktop>cmd
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\phelanj\Desktop>z:

Z:\>ant run
Buildfile: build.xml

clean:
[delete] Deleting directory Z:\bin
[mkdir] Created dir: Z:\bin

copy-resources:
[copy] Copying 5 files to Z:\bin
[copy] Copied 2 empty directories to 1 empty directory under Z:\bin

compile:
[javac] Compiling 8 source files to Z:\bin

run:
run:
[java] Ribbit: Event Manager created
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Environment <clinit>
[java] INFO: Hibernate 3.2.1
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Environment <clinit>
[java] INFO: hibernate.properties not found
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Environment buildBytecodeProvider
[java] INFO: Bytecode provider name : cglib
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Environment <clinit>
[java] INFO: using JDK 1.4 java.sql.Timestamp handling
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Configuration configure
[java] INFO: configuring from resource: /hibernate.cfg.xml
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Configuration getConfigurationInputStream
[java] INFO: Configuration resource: /hibernate.cfg.xml
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Configuration addResource
[java] INFO: Reading mappings from resource : tables/FundApproved.hbm.xml
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
[java] INFO: Mapping class: tables.FundApproved -> fund_approved
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Configuration addResource
[java] INFO: Reading mappings from resource : tables/Bond.hbm.xml
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
[java] INFO: Mapping class: tables.Bond -> bond
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Configuration addResource
[java] INFO: Reading mappings from resource : tables/BondRelationship.hbm.xml
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
[java] INFO: Mapping class: tables.BondRelationship -> bond_relationship
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.Configuration doConfigure
[java] INFO: Configured SessionFactory: null
[java] 22-Jan-2007 14:25:55 org.hibernate.cfg.HbmBinder bindCollectionSecondPass
[java] INFO: Mapping collection: tables.Bond.BondRelationship -> bond_relationship
[java] 22-Jan-2007 14:25:55 org.hibernate.connection.DriverManagerConnectionProvider configure
[java] INFO: Using Hibernate built-in connection pool (not for production use!)
[java] 22-Jan-2007 14:25:55 org.hibernate.connection.DriverManagerConnectionProvider configure
[java] INFO: Hibernate connection pool size: 1
[java] 22-Jan-2007 14:25:55 org.hibernate.connection.DriverManagerConnectionProvider configure
[java] INFO: autocommit mode: false
[java] 22-Jan-2007 14:25:55 org.hibernate.connection.DriverManagerConnectionProvider configure
[java] INFO: using driver: com.informix.jdbc.IfxDriver at URL: jdbc:informix-sqli://devdub01:1528/sei_dev_focus:INFORMIXSERVER=devdub01;GL_DATE=%d%m%Y;FET_
BUF_SIZE=32767
[java] 22-Jan-2007 14:25:55 org.hibernate.connection.DriverManagerConnectionProvider configure
[java] INFO: connection properties: {user=phelanj, password=****}
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: RDBMS: Informix Dynamic Server, version: 9.50.HC4
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: JDBC driver: Informix JDBC Driver for Informix Dynamic Server, version: 2.21.JC2
[java] 22-Jan-2007 14:26:02 org.hibernate.dialect.Dialect <init>
[java] INFO: Using dialect: org.hibernate.dialect.HSQLDialect
[java] 22-Jan-2007 14:26:02 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
[java] INFO: Using default transaction strategy (direct JDBC transactions)
[java] 22-Jan-2007 14:26:02 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
[java] INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Automatic flush during beforeCompletion(): disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Automatic session close at end of transaction: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: JDBC batch size: 15
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: JDBC batch updates for versioned data: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Scrollable result sets: enabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: JDBC3 getGeneratedKeys(): disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Connection release mode: auto
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Default batch fetch size: 1
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Generate SQL with comments: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Order SQL updates by primary key: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
[java] INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
[java] 22-Jan-2007 14:26:02 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
[java] INFO: Using ASTQueryTranslatorFactory
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Query language substitutions: {}
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: JPA-QL strict compliance: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Second-level cache: enabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Query cache: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory createCacheProvider
[java] INFO: Cache provider: org.hibernate.cache.NoCacheProvider
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Optimize cache for minimal puts: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Structured second-level cache entries: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Echoing all SQL to stdout
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Statistics: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Deleted entity synthetic identifier rollback: disabled
[java] 22-Jan-2007 14:26:02 org.hibernate.cfg.SettingsFactory buildSettings
[java] INFO: Default entity-mode: pojo
[java] 22-Jan-2007 14:26:02 org.hibernate.impl.SessionFactoryImpl <init>
[java] INFO: building session factory
[java] 22-Jan-2007 14:26:02 org.hibernate.impl.SessionFactoryObjectFactory addInstance
[java] INFO: Not binding factory to JNDI, no JNDI name configured
[java] 22-Jan-2007 14:26:02 org.hibernate.tool.hbm2ddl.SchemaExport execute
[java] INFO: Running hbm2ddl schema export
[java] 22-Jan-2007 14:26:02 org.hibernate.tool.hbm2ddl.SchemaExport execute
[java] INFO: exporting generated schema to database
[java] 22-Jan-2007 14:26:03 org.hibernate.tool.hbm2ddl.SchemaExport create
[java] SEVERE: Unsuccessful: create table bond (bond_no_ser bigint generated by default as identity (start with 1), product_label varchar(255), cancel_peri
od bigint, policy_numb varchar(255), primary key (bond_no_ser))
[java] 22-Jan-2007 14:26:03 org.hibernate.tool.hbm2ddl.SchemaExport create
[java] SEVERE: A syntax error has occurred.
[java] 22-Jan-2007 14:26:03 org.hibernate.tool.hbm2ddl.SchemaExport create
[java] SEVERE: Unsuccessful: create table bond_relationship (bond_rel_ser bigint not null, clinttyp_label varchar(255), bond_no_ser bigint, primary key (bo
nd_rel_ser))
[java] 22-Jan-2007 14:26:03 org.hibernate.tool.hbm2ddl.SchemaExport create
[java] SEVERE: Type (bigint) not found.
[java] 22-Jan-2007 14:26:03 org.hibernate.tool.hbm2ddl.SchemaExport create
[java] SEVERE: Unsuccessful: create table fund_approved (fund_ser bigint not null, insert_date date, modified_by varchar(255), modified_prog varchar(255),
modified_at date, primary key (fund_ser))
[java] 22-Jan-2007 14:26:03 org.hibernate.tool.hbm2ddl.SchemaExport create
[java] SEVERE: Type (bigint) not found.
[java] 22-Jan-2007 14:26:03 org.hibernate.tool.hbm2ddl.SchemaExport create
[java] SEVERE: Unsuccessful: alter table bond_relationship add constraint FKF9347A34DA90C2AA foreign key (bond_no_ser) references bond
[java] 22-Jan-2007 14:26:03 org.hibernate.tool.hbm2ddl.SchemaExport create
[java] SEVERE: A syntax error has occurred.
[java] 22-Jan-2007 14:26:03 org.hibernate.tool.hbm2ddl.SchemaExport execute
[java] INFO: schema export complete
[java] 22-Jan-2007 14:26:03 org.hibernate.util.JDBCExceptionReporter logWarnings
[java] WARNING: SQL Warning: 0, SQLState: 01I01
[java] 22-Jan-2007 14:26:03 org.hibernate.util.JDBCExceptionReporter logWarnings
[java] WARNING: Database has transactions
[java] 22-Jan-2007 14:26:03 org.hibernate.util.JDBCExceptionReporter logWarnings
[java] WARNING: SQL Warning: 0, SQLState: 01I04
[java] 22-Jan-2007 14:26:03 org.hibernate.util.JDBCExceptionReporter logWarnings
[java] WARNING: Database selected
[java] Calling HJhibernate task JohnoBeginning transaction
[java] Get bond
[java] Hibernate: select bond0_.bond_no_ser as bond1_1_0_, bondrelati1_.bond_rel_ser as bond1_2_1_, bond0_.product_label as product2_1_0_, bond0_.cancel_pe
riod as cancel3_1_0_, bond0_.policy_numb as policy4_1_0_, bondrelati1_.clinttyp_label as clinttyp2_2_1_, bondrelati1_.bond_no_ser as bond3_2_1_, bondrelati1_.bo
nd_no_ser as bond3_0__, bondrelati1_.bond_rel_ser as bond1_0__ from bond bond0_ left outer join bond_relationship bondrelati1_ on bond0_.bond_no_ser=bondrelati1
_.bond_no_ser where bond0_.bond_no_ser=?
[java] 5
[java] count: 11
[java] 1
[java] 2
[java] 3
[java] Ribbit: Closing session factory
[java] 22-Jan-2007 14:26:03 org.hibernate.impl.SessionFactoryImpl close
[java] INFO: closing
[java] 22-Jan-2007 14:26:03 org.hibernate.connection.DriverManagerConnectionProvider close
[java] INFO: cleaning up connection pool: jdbc:informix-sqli://devdub01:1528/sei_dev_focus:INFORMIXSERVER=devdub01;GL_DATE=%d%m%Y;FET_BUF_SIZE=32767

BUILD SUCCESSFUL
Total time: 18 seconds
Z:\>


Name and version of the database you are using:
IBM Informix, Version 10


Last edited by Cappanagrane on Mon Jan 22, 2007 10:32 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 1:23 pm 
Newbie

Joined: Thu Nov 30, 2006 12:21 pm
Posts: 14
Hi,
I think that you don't configure carefuly your association , a quick check demostrate that you use a "fictif key" in your bag in Bond.hbm.xml , the key of the bag must ne set in your case to "bond_rel_ser" the id of the second table ,
second think , you don't have to specify the join in your hql because hibernate do it for you .


Top
 Profile  
 
 Post subject: Actually works!
PostPosted: Mon Jan 22, 2007 1:02 pm 
Newbie

Joined: Fri Jan 19, 2007 10:29 am
Posts: 4
Location: Ireland
Actually, this all worked fine, I just needed to call the getter method in the second table class implementation rather than the toString()!

For some reason, the toString method that I overide in the BondRelationship class doesn't get called!

Anyway, this worked so all is good! Cheers,



int count = bond.getBondRelationship().size();
System.out.println("count: " + count);

Iterator listItr = bond.getBondRelationship().listIterator();
while(listItr.hasNext()) {
System.out.println("1");
BondRelationship rel = (BondRelationship)listItr.next();
System.out.println("2");
System.out.println("Clint type is: " + rel.getClintTypeLabel());
rel.toString();
System.out.println("3");
}


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