-->
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: Problem doing a save with many-to-many associated entities
PostPosted: Thu Jun 16, 2005 1:23 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 2:00 pm
Posts: 38
Hibernate version: 3.0.5

Mapping documents:
As follows:
Code:
<class name="User" table="MM_USERS">
      <id name="id" column="id" type="int" unsaved-value="null" length="5">
         <generator class="native"/>
      </id>
      <property name="name" column="name" type="string" length="50" not-null="true"/>
      
      <many-to-one name="community" column="communityID" not-null="false" class="Community" cascade="all" unique="true"/>
      <set name="memberships" table="MM_USERGROUPLINKS" cascade="none" order-by="name asc" where="" >
         <key column="userID" />
         <many-to-many class="Group" column="groupID" />
      </set>
      
      <joined-subclass name="Employee" table="MM_EMPLOYEE">
         <key column="personID"/>
         <property name="employeeID" column="employeeID" type="string" length="10" not-null="false" />
         <property name="department" column="department" type="string" length="30" not-null="false" />
      </joined-subclass>
   </class>
<class name="Group" table="MM_GROUPS">
      <id name="id" column="id" type="int" unsaved-value="null" length="5">
         <generator class="native"/>
      </id>
      <property name="name" column="name" type="string" length="50" not-null="true"/>
      
      <set name="members" table="MM_USERGROUPLINKS" cascade="none" order-by="name asc" where="" >
         <key column="groupID" />
         <many-to-many class="User" column="userID" />
      </set>
   </class>


Code between sessionFactory.openSession() and session.close():
Code:
Group group = new Group();
            session.load(group, new Integer(1));
            System.out.println("name = " + group.getName());
           
            User newUser = new User();
            session.load(newUser, new Integer(8));
            group.getMembers().add(newUser);
            session.save(group);


Full stack trace of any exception that occurs:
Jun 17, 2005 1:11:30 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 156, SQLState: S1000
Jun 17, 2005 1:11:30 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Incorrect syntax near the keyword 'and'.
org.hibernate.exception.GenericJDBCException: could not initialize a collection:
[project.vo.Group.members#1]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLSta
teConverter.java:82)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1441)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader
.java:99)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(Ab
stractCollectionPersister.java:488)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializ
eCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPer
sistentCollection.java:176)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersiste
ntCollection.java:61)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:158)
at HibernateTest.main(HibernateTest.java:36)
Caused by: java.sql.SQLException: Incorrect syntax near the keyword 'and'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:367
)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2606)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2048)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:574)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:3
21)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedSta
tement.java:667)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:120)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1272)
at org.hibernate.loader.Loader.doQuery(Loader.java:391)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.ja
va:218)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1434)


Name and version of the database you are using: SQL Server 2000

The generated SQL (show_sql=true):
Hibernate: select group0_.id as id0_, group0_.name as name4_0_ from MM_GROUPS gr
oup0_ where group0_.id=?
name = Group 1
Hibernate: select user0_.id as id0_, user0_.name as name0_0_, user0_.communityID
as communit3_0_0_, user0_1_.employeeID as employeeID2_0_, user0_1_.department a
s department2_0_, case when user0_1_.personID is not null then 1 when user0_.id
is not null then 0 end as clazz_0_ from MM_USERS user0_ left outer join MM_EMPLO
YEE user0_1_ on user0_.id=user0_1_.personID where user0_.id=?
Hibernate: select members0_.groupID as groupID1_, members0_.userID as userID1_,
user1_.id as id0_, user1_.name as name0_0_, user1_.communityID as communit3_0_0_
, user1_1_.employeeID as employeeID2_0_, user1_1_.department as department2_0_,
case when user1_1_.personID is not null then 1 when user1_.id is not null then 0
end as clazz_0_ from MM_USERGROUPLINKS members0_ inner join MM_USERS user1_ on
members0_.userID=user1_.id left outer join MM_EMPLOYEE user1_1_ on user1_.id=use
r1_1_.personID where and members0_.groupID=? order by members0_.name asc

The bit in bold red seems to be where it's gone wonky. Any ideas why Hibernate would generate such a erroneous statement? Is this a bug?

Thanks in advance for any advice


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 1:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Remove where="" from your mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 1:41 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 2:00 pm
Posts: 38
Ah! Of course! Programmer's blindness :-(

Thanks for the fresh pair of eyes spotting that :-)

gavin wrote:
Remove where="" from your mapping.


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.