-->
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.  [ 1 post ] 
Author Message
 Post subject: How to use property-ref with legacy database
PostPosted: Fri Mar 17, 2006 3:19 pm 
Newbie

Joined: Fri Mar 19, 2004 11:04 pm
Posts: 4
Location: WI
I'm trying to map a legacy user database which has a child table with user role codes. The key on the role code is user_id which is a natural id of the user table. The role code is mapped into a List of strings on the User object.

I'm using a bag and property-ref but am unable to query all users with "session.createCriteria(User.class).list()". However, if I pull an iterator instead and then grab each User, it works fine.

Oracle describe of simplified tables in question:

Code:
USERS
Name                                      Null?    Type
----------------------------------------- -------- ----------------------------
OBJECT_ID                                 NOT NULL NUMBER(12)
USER_ID                                   NOT NULL VARCHAR2(8)
NAME                                      NOT NULL VARCHAR2(40)

USER_ROLES
Name                                      Null?    Type
----------------------------------------- -------- ----------------------------
USER_ID                                   NOT NULL VARCHAR2(8)
ROLE                                      NOT NULL VARCHAR2(2)


Hibernate version: 3.1.2

Mapping documents:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
   <class name="domain.User" table="USERS" schema="RAT">
      <id name="objectId">
         <column name="OBJECT_ID" precision="12" scale="0" />
         <generator class="increment" />
      </id>
      <natural-id>
         <property name="userId" column="USER_ID" length="8" not-null="true" />
      </natural-id>
      <property name="name" column="NAME" length="40" not-null="true" />
      <set name="roles" table="USER_ROLES" lazy="false">
         <key column="USER_ID" property-ref="userId" />
         <element column="ROLE" type="string" />
      </set>
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
      // DOES NOT WORK!!
      Session session = sessionFactory.openSession();
      List list = session.createCriteria(User.class).list();
      assertNotNull(list);
      Iterator iterator = list.iterator();
      while (iterator.hasNext()) {
         User user = (User)iterator.next();
         System.out.println("User = " + user.toString());
      }
      session.close();


Code:
      // THIS WORKS FINE
      Session session = sessionFactory.openSession();
      List list = new ArrayList();
      Iterator iterator = session.createQuery("from User").iterate();
      while (iterator.hasNext()) list.add(iterator.next());
      assertNotNull(list);
      iterator = list.iterator();
      while (iterator.hasNext()) {
         User user = (User)iterator.next();
         System.out.println("User = " + user.toString());
      }
      session.close();


Full stack trace of any exception that occurs:
Code:
org.hibernate.HibernateException: collection is not associated with any session
   at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:449)
   at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:827)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:229)
   at org.hibernate.loader.Loader.doList(Loader.java:2150)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
   at org.hibernate.loader.Loader.list(Loader.java:2024)
   at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
   at test.UserMapping.testFindAll(UserMapping.java:70)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:41)
   at java.lang.reflect.Method.invoke(Method.java:386)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)


Name and version of the database you are using: Oracle
Code:
13:03:38,154  INFO SettingsFactory:77 - RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production
13:03:38,154  INFO SettingsFactory:78 - JDBC driver: Oracle JDBC driver, version: 9.2.0.5.0
13:03:38,204  INFO Dialect:103 - Using dialect: org.hibernate.dialect.Oracle9Dialect


The generated SQL (show_sql=true):
Code:
Hibernate: select this_.OBJECT_ID as OBJECT1_0_0_, this_.USER_ID as USER2_0_0_, this_.NAME as NAME0_0_ from RAT.USERS this_
Hibernate: select roles0_.USER_ID as USER1_0_, roles0_.ROLE as ROLE0_ from USER_ROLES roles0_ where roles0_.USER_ID=?


Debug level Hibernate log excerpt:
Code:
13:03:35,380  INFO Environment:479).null(Unknown Source - Hibernate 3.1.2
13:03:35,420  INFO Environment:494).null(Unknown Source - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider, hibernate.max_fetch_depth=1, hibernate.dialect=org.hibernate.dialect.Oracle9Dialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.connection.username=rat, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:oracle:thin:@cedar:1521:o_cm01d, hibernate.show_sql=true, hibernate.connection.password=****, hibernate.jdbc.batch_versioned_data=true, hibernate.transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory, hibernate.connection.pool_size=1}
13:03:35,430  INFO Environment:524).null(Unknown Source - using java.io streams to persist binary types
13:03:35,430  INFO Environment:525).null(Unknown Source - using CGLIB reflection optimizer
13:03:35,430  INFO Environment:555).null(Unknown Source - using JDK 1.4 java.sql.Timestamp handling
13:03:35,540  INFO Configuration:1308 - configuring from resource: /hibernate.cfg.xml
13:03:35,540  INFO Configuration:1285 - Configuration resource: /hibernate.cfg.xml
13:03:35,801  INFO Configuration:469 - Reading mappings from resource: domain/User.hbm.xml
13:03:36,211  INFO HbmBinder:309 - Mapping class: domain.User -> USERS
13:03:36,261  INFO HbmBinder:1397 - Mapping collection: domain.User.roles -> USER_ROLES
13:03:36,272  INFO Configuration:1419 - Configured SessionFactory: null
13:03:36,422  INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
13:03:36,422  INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
13:03:36,422  INFO DriverManagerConnectionProvider:45 - autocommit mode: false
13:03:36,682  INFO DriverManagerConnectionProvider:80 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@cedar:1521:o_cm01d
13:03:36,682  INFO DriverManagerConnectionProvider:86 - connection properties: {user=rat, password=****}
13:03:38,154  INFO SettingsFactory:77 - RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production
13:03:38,154  INFO SettingsFactory:78 - JDBC driver: Oracle JDBC driver, version: 9.2.0.5.0
13:03:38,204  INFO Dialect:103 - Using dialect: org.hibernate.dialect.Oracle9Dialect
13:03:38,214  INFO TransactionFactoryFactory:34 - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
13:03:38,224  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
13:03:38,244  INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
13:03:38,244  INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
13:03:38,244  INFO SettingsFactory:136 - JDBC batch size: 15
13:03:38,244  INFO SettingsFactory:139 - JDBC batch updates for versioned data: enabled
13:03:38,244  INFO SettingsFactory:144 - Scrollable result sets: enabled
13:03:38,244  INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): disabled
13:03:38,244  INFO SettingsFactory:160 - Connection release mode: auto
13:03:38,244  INFO SettingsFactory:184 - Maximum outer join fetch depth: 1
13:03:38,254  INFO SettingsFactory:187 - Default batch fetch size: 1
13:03:38,254  INFO SettingsFactory:191 - Generate SQL with comments: disabled
13:03:38,254  INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
13:03:38,254  INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
13:03:38,254  INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
13:03:38,264  INFO SettingsFactory:203 - Query language substitutions: {no='N', true=1, yes='Y', false=0}
13:03:38,264  INFO SettingsFactory:209 - Second-level cache: enabled
13:03:38,264  INFO SettingsFactory:213 - Query cache: disabled
13:03:38,264  INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.EhCacheProvider
13:03:38,274  INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
13:03:38,274  INFO SettingsFactory:233 - Cache region prefix: hibernate.test
13:03:38,274  INFO SettingsFactory:237 - Structured second-level cache entries: disabled
13:03:38,294  INFO SettingsFactory:257 - Echoing all SQL to stdout
13:03:38,294  INFO SettingsFactory:264 - Statistics: disabled
13:03:38,294  INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
13:03:38,294  INFO SettingsFactory:283 - Default entity-mode: pojo
13:03:38,354  INFO SessionFactoryImpl:153 - building session factory
13:03:38,365  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/My%20Data/Workspaces/WSAD51/RaTRaP/RaTRaP/WebContent/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
13:03:39,035  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
Hibernate: select this_.OBJECT_ID as OBJECT1_0_0_, this_.USER_ID as USER2_0_0_, this_.NAME as NAME0_0_ from RAT.USERS this_
Hibernate: select roles0_.USER_ID as USER1_0_, roles0_.ROLE as ROLE0_ from USER_ROLES roles0_ where roles0_.USER_ID=?


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

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.