-->
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: reverse engineer many-to-many hiccup
PostPosted: Sat Feb 21, 2009 2:26 pm 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
I'm using ANT (Hibernate 3.3.1 & Tools 3.2.4) to reverse engineer a MySQL 5.0.51 DB. The mapping and java files appear to be generated fine.

I have a many-to-many between GROUP and ROLE. I have a join table GROUPROLE.

The Group.hbm.xml mapping file shows the relationship:
Code:
<set name="roles" inverse="false" table="GroupRole">
            <key>
                <column name="groupId" not-null="true" />
            </key>
            <many-to-many entity-name="com.flair.model.Role">
                <column name="roleId" not-null="true" />
            </many-to-many>
</set>

Problem is: when I try to access 'ROLES' from GROUP using this:
Code:
List<Group> list = getHibernateTemplate().getSessionFactory()
   .getCurrentSession().createCriteria(Group.class)
   .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
   .setFetchMode("roles", FetchMode.JOIN)
   .add(Expression.eq("groupId", id))
        .list();


I get the following error:
Code:
Hibernate: select this_.groupId as groupId5_1_, this_.companyId as companyId5_1_, this_.creatorUserId as creatorU3_5_1_, this_.classNameId as classNam4_5_1_, this_.classPK as classPK5_1_, this_.parentGroupId as parentGr6_5_1_, this_.liveGroupId as liveGrou7_5_1_, this_.name as name5_1_, this_.description as descript9_5_1_, this_.type_ as type10_5_1_, this_.typeSettings as typeSet11_5_1_, this_.friendlyURL as friendl12_5_1_, this_.active_ as active13_5_1_, roles2_.groupId as groupId3_, role3_.roleId as roleId3_, role3_.roleId as roleId9_0_, role3_.companyId as companyId9_0_, role3_.classNameId as classNam3_9_0_, role3_.classPK as classPK9_0_, role3_.name as name9_0_, role3_.description as descript6_9_0_, role3_.type_ as type7_9_0_ from flair.Group_ this_ left outer join GroupRole roles2_ on this_.groupId=roles2_.groupId left outer join flair.role_ role3_ on roles2_.roleId=role3_.roleId where this_.groupId=?
Feb 21, 2009 11:49:31 AM org.hibernate.util.JDBCExceptionReporter logExceptions

WARNING: SQL Error: 1046, SQLState: 3D000
Feb 21, 2009 11:49:31 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: No database selected


I haven't seen this before. Anyone see anything abnormal?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 7:34 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
This sounds like a database connection problem, can you run any other statement sucessfully?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 10:27 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
Yes, I can run other queries with no problem. Actually what I'm doing is fetching a ROLE from the db, then fetch a GROUP, then I want to add the ROLE to the "set of ROLES" in GROUP <many-to-many>.

Here is the entire method that fetches the GROUP:
Code:
public Group findById(Long id) {
      
      log.debug("In Group findById");
      List<Group> list = getHibernateTemplate().getSessionFactory()
            .getCurrentSession().createCriteria(Group.class)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
             .setFetchMode("roles", FetchMode.JOIN)
            .add(Restrictions.eq("groupId", id)).list();
      log.debug("after fetch");
      Group g = list.get(0);
      
log.debug("group.roles.size is: " + g.getRoles().size());

      return (list.size() > 0) ? list.get(0) : null;
      
   }


This specific piece of code fails in the .list() method. If I comment out //.setFetchMode("roles", FetchMode.JOIN) then the .list() method succeeds and the list.get(0) fails.

Each failure stems from trying to access the set in the <many-to-many>. If I only access the GROUP there is no problem but GROUP.getRoles() fails.

Here is the logging starting from my log statement at the beginning of the method:
Code:
DEBUG - GroupDAOImpl.findById(45) | In Group findById
DEBUG - AbstractBatcher.logOpenPreparedStatement(410) | about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG - SQLStatementLogger.logStatement(111) | select this_.groupId as groupId5_1_, this_.companyId as companyId5_1_, this_.creatorUserId as creatorU3_5_1_, this_.classNameId as classNam4_5_1_, this_.classPK as classPK5_1_, this_.parentGroupId as parentGr6_5_1_, this_.liveGroupId as liveGrou7_5_1_, this_.name as name5_1_, this_.description as descript9_5_1_, this_.type_ as type10_5_1_, this_.typeSettings as typeSet11_5_1_, this_.friendlyURL as friendl12_5_1_, this_.active_ as active13_5_1_, roles2_.groupId as groupId3_, role3_.roleId as roleId3_, role3_.roleId as roleId9_0_, role3_.companyId as companyId9_0_, role3_.classNameId as classNam3_9_0_, role3_.classPK as classPK9_0_, role3_.name as name9_0_, role3_.description as descript6_9_0_, role3_.type_ as type7_9_0_ from flair.Group_ this_ left outer join GroupRole roles2_ on this_.groupId=roles2_.groupId left outer join flair.role_ role3_ on roles2_.roleId=role3_.roleId where this_.groupId=?
Hibernate: select this_.groupId as groupId5_1_, this_.companyId as companyId5_1_, this_.creatorUserId as creatorU3_5_1_, this_.classNameId as classNam4_5_1_, this_.classPK as classPK5_1_, this_.parentGroupId as parentGr6_5_1_, this_.liveGroupId as liveGrou7_5_1_, this_.name as name5_1_, this_.description as descript9_5_1_, this_.type_ as type10_5_1_, this_.typeSettings as typeSet11_5_1_, this_.friendlyURL as friendl12_5_1_, this_.active_ as active13_5_1_, roles2_.groupId as groupId3_, role3_.roleId as roleId3_, role3_.roleId as roleId9_0_, role3_.companyId as companyId9_0_, role3_.classNameId as classNam3_9_0_, role3_.classPK as classPK9_0_, role3_.name as name9_0_, role3_.description as descript6_9_0_, role3_.type_ as type7_9_0_ from flair.Group_ this_ left outer join GroupRole roles2_ on this_.groupId=roles2_.groupId left outer join flair.role_ role3_ on roles2_.roleId=role3_.roleId where this_.groupId=?
DEBUG - AbstractBatcher.logClosePreparedStatement(418) | about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - JDBCExceptionReporter.logExceptions(92) | could not execute query [select this_.groupId as groupId5_1_, this_.companyId as companyId5_1_, this_.creatorUserId as creatorU3_5_1_, this_.classNameId as classNam4_5_1_, this_.classPK as classPK5_1_, this_.parentGroupId as parentGr6_5_1_, this_.liveGroupId as liveGrou7_5_1_, this_.name as name5_1_, this_.description as descript9_5_1_, this_.type_ as type10_5_1_, this_.typeSettings as typeSet11_5_1_, this_.friendlyURL as friendl12_5_1_, this_.active_ as active13_5_1_, roles2_.groupId as groupId3_, role3_.roleId as roleId3_, role3_.roleId as roleId9_0_, role3_.companyId as companyId9_0_, role3_.classNameId as classNam3_9_0_, role3_.classPK as classPK9_0_, role3_.name as name9_0_, role3_.description as descript6_9_0_, role3_.type_ as type7_9_0_ from flair.Group_ this_ left outer join GroupRole roles2_ on this_.groupId=roles2_.groupId left outer join flair.role_ role3_ on roles2_.roleId=role3_.roleId where this_.groupId=?]
java.sql.SQLException: No database selected
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
   at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
   at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
   at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
   at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)
   at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
   at org.hibernate.loader.Loader.doQuery(Loader.java:697)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
   at org.hibernate.loader.Loader.doList(Loader.java:2228)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
   at org.hibernate.loader.Loader.list(Loader.java:2120)
   at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1596)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
   at com.flair.dao.impl.GroupDAOImpl.findById(GroupDAOImpl.java:50)
   at com.flair.service.impl.GroupManagerImpl.findById(GroupManagerImpl.java:41)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy5.findById(Unknown Source)
   at com.flair.dao.RoleTestCase.testGroupRole(RoleTestCase.java:49)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at junit.framework.TestCase.runTest(TestCase.java:168)
   at junit.framework.TestCase.runBare(TestCase.java:134)
   at junit.framework.TestResult$1.protect(TestResult.java:110)
   at junit.framework.TestResult.runProtected(TestResult.java:128)
   at junit.framework.TestResult.run(TestResult.java:113)
   at junit.framework.TestCase.run(TestCase.java:124)
   at junit.framework.TestSuite.runTest(TestSuite.java:232)
   at junit.framework.TestSuite.run(TestSuite.java:227)
   at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
WARN - JDBCExceptionReporter.logExceptions(100) | SQL Error: 1046, SQLState: 3D000
ERROR - JDBCExceptionReporter.logExceptions(101) | No database selected
DEBUG - JDBCTransaction.rollback(186) | rollback
DEBUG - JDBCTransaction.toggleAutoCommit(227) | re-enabling autocommit
DEBUG - JDBCTransaction.rollback(197) | rolled back JDBC Connection
DEBUG - ConnectionManager.afterTransaction(325) | transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG - ConnectionManager.closeConnection(464) | releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG - ConnectionManager.afterTransaction(325) | transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 7:36 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
can you post the part where you configure your db connection?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 9:06 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
I'm using Spring 2.5.6 to configure the database (see below). I can hit the database the problem comes when accessing the *..* relationship.

One other note: this is happening in my JUNIT test. I haven't deployed this to an app server.

Code:
<bean id="hibernateTemplate"
      class="org.springframework.orm.hibernate3.HibernateTemplate">
      <property name="sessionFactory">
         <ref bean="jruffinSF" />
      </property>
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" >      
         <ref bean="jruffinSF" />
      </property>
   </bean>   

<bean id="jruffinSF"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource"><ref local="DataSource" /></property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">${db.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.autocommit">true</prop>      
            <prop key="hibernate.cache.provider_class"> org.hibernate.cache.HashtableCacheProvider</prop>
         </props>
      </property>
      
      <property name="mappingJarLocations">         
            <value>${db.mappingJar}</value>         
      </property>
   </bean>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 9:10 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Can you also post the part where you configure the database connection url? I guess the problem could be that you are missing the schema somewhere.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: RESOLVED
PostPosted: Mon Feb 23, 2009 9:39 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
Right on!!! Thanks!!!

In my applicationContext.xml (where I set up the DataSource) I was passing in a url WITHOUT the schema. Adding it solved my hiccup - see url property below. Thanks again!

Code:
<bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName">
         <value>${db.driverClassName}</value>
      </property>
      <property name="url">
         <value>${db.url}/flair</value>
      </property>
      <property name="username">
         <value>${db.username}</value>
      </property>
      <property name="password">
         <value>${db.password}</value>
      </property>
   </bean>


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.