-->
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: one-to-many mapping errors could not initialize a collection
PostPosted: Fri Apr 07, 2006 3:24 pm 
Newbie

Joined: Fri Apr 07, 2006 3:10 pm
Posts: 4
Hi,

When I am trying to execute one-to-many relation, I am getting could not initialize a collection...

Appreciate your help on this.

Rgds,
Sri

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2

Mapping documents:
<?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="users.User" table="USER">
<id name="id" column="USER_ID">
<generator class="native"/>
</id>
<property name="username" column="USER_NAME"/>
<set name="usergroups" lazy="true" table="USER_GROUPS">
<key column="USER_ID"/>
<one-to-many class="users.Group"/>
</set>
</class>
<class name="users.Group" table="GROUP">
<composite-id>
<key-property name="groupId" column="GROUP_ID"/>
<key-property name="appId" column="APP_ID"/>
</composite-id>
<property name="groupName" column="GROUP_KEY"/>

</class>

</hibernate-mapping

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

Full stack trace of any exception that occurs:


Name and version of the database you are using:
oracle 9i
The generated SQL (show_sql=true):
[java] Hibernate: select * from ( select user0_.USER_ID as USER1_0_, user0_.USER_NAME as USER2_0_ from USER user0_ ) where rownum <= ?
[java] User Id:1351User Name:TEST
[java] Hibernate: select usergroups0_.USER_ID as USER4_1_, usergroups0_.GROUP_ID as RESPONSI1_1_, usergroups0_.APP_ID as APP2_1_, usergroups0_.GROUP_ID as GROUPI1_1_0_, usergroups0_.APP_ID as APPT2_1_0_, usergroups0_.GROUP_KEY as GROUI3_1_0_ from GROUP usergroups0_ where usergroups0_.USER_ID=?
[java] org.hibernate.exception.SQLGrammarException: could not initialize acollection: [users.User.usergroups#1351]
[java] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
[java] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
[java] at org.hibernate.loader.Loader.loadCollection(Loader.java:1926)
[java] at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
[java] at org.hibernate.persister.collection.AbstractCollectionPersiste
r.initialize(AbstractCollectionPersister.java:520)
[java] at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
[java] at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1676)
[java] at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
[java] at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
[java] at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
[java] at users.UserManager.main(Unknown Source)
[java] Caused by: java.sql.SQLException: ORA-00904: "USERGROUPS0_"."USER_ID": invalid identifier

[java] at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
[java] at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
[java] at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:573)
[java] at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1891)
[java] at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:830)
[java] at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2391)
[java] at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2672)

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 4:46 pm 
Newbie

Joined: Wed Apr 05, 2006 5:58 pm
Posts: 13
I believe you'll need a many-to-many to get from users to groups. You need to get all the groups based on a user id in the user_group table. If you check that sql, you'll see that it's attempting to select the user_id from the group table.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 3:52 am 
Newbie

Joined: Fri Apr 07, 2006 3:10 pm
Posts: 4
Thanks for you reply..
the situation is each user will have group of tasks.. what I am trying to do is I want to access all users with theire tasks.

So I thought this will be one-to-many relationship.

Even if I change the many-to-many I am getting the following error

must have same number of columns as the referenced primary key.

Pls suggest me how to ahead in this case.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 12:44 pm 
Newbie

Joined: Wed Nov 30, 2005 4:13 pm
Posts: 12
Hi, you have to specify columns for Group composite id in one-to-many element, they must be exactly in the same order as they apear in composite-id element


<?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="users.User" table="USER">
<id name="id" column="USER_ID">
<generator class="native"/>
</id>
<property name="username" column="USER_NAME"/>
<set name="usergroups" lazy="true" table="USER_GROUPS">
<key column="USER_ID"/>
<one-to-many class="users.Group">

<column name="GROUP_ID"/>
<column name="APP_ID"/>

</one-to-many>
</set>
</class>
<class name="users.Group" table="GROUP">
<composite-id>
<key-property name="groupId" column="GROUP_ID"/>
<key-property name="appId" column="APP_ID"/>
</composite-id>
<property name="groupName" column="GROUP_KEY"/>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 4:05 pm 
Newbie

Joined: Fri Apr 07, 2006 3:10 pm
Posts: 4
Thank you very much your reply.. I am new to Hibernate...I modified the mapping as you suggested. Now I am getting INVALID MAPPING error.

[java] Caused by: org.xml.sax.SAXParseException: The content of element type "one-to-many" must match "EMPTY".

Please suggest me how to go ahead

Thanks in advance...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 4:31 pm 
Newbie

Joined: Wed Nov 30, 2005 4:13 pm
Posts: 12
I am also new, so since <one-to-many> has to be empty
you can use <many-to-many> with <column> subelements
and treat it as <one-to-many>
I can't see the way in that case to use <one-to-many> with user_groups table.
And if that is realy one to many association why don't you just add user_id column to group table?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 5:30 pm 
Newbie

Joined: Fri Apr 07, 2006 3:10 pm
Posts: 4
Hi,

Thank you very much.. It resolved the issue.


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.