-->
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.  [ 9 posts ] 
Author Message
 Post subject: could not initialize a collection -Error
PostPosted: Sat Feb 21, 2009 5:57 pm 
Newbie

Joined: Sat Feb 21, 2009 5:36 pm
Posts: 8
Hey Guys,
can somebody explain me why i get this ugly "can not initialize a collection"error?

First i wanna post my code and config-file for a little overview:

This are my Methods am using(this one and nothing else):

Code:
public UserVO login(String username, String password) {

      Session sess = getSessionFactory().openSession();
      Query q = sess.createQuery("from UserVO where username='" + username
            + "'");
      if (q.list().size() == 0)
         return null;
      UserVO user = (UserVO)q.list().get(0);
      
      if(user.getPassword().equals(password)){
         return user;
      }else{
         return null;
      }
   }

   public boolean register(UserVO newUser) {
      try {

         Session sess = getSessionFactory().openSession();
         Query q = sess.createQuery("from UserVO where username='"
               + newUser.getUsername() + "'");

         if (q.list().size() > 0) {
            
            return false;
            
         } else {
            Transaction trans = sess.beginTransaction();
            sess.save(newUser);
            trans.commit();
         }
         sess.close();
         return true;
      } catch (HibernateException e) {
         // TODO Auto-generated catch block
         System.out.println(e.getMessage());
         return false;
      }
   }


Now my Mapping File:

Code:
<hibernate-mapping package="vo">
   <class name="UserVO">
      <id name="id">
         <generator class="native"/>
      </id>
      <property name="username"/>
      <property name="password"/>
      <property name="email"/>
      
      <list name="favCategories">
         <key column="userID"/>
         <list-index column="index"/>
         <many-to-many column="categoryID" class="vo.CategoryVO"/>
      </list>
      
      <list name="books" cascade="all">
         <key column="userID"/>
         <list-index column="index"/>
         <many-to-many column="bookID" class="vo.BookVO"/>
      </list>
   </class>
</hibernate-mapping>


So when i call the register-method everything works fine. The User is created in the database. But when i want to login the console is showing the following:
Code:
[size=9]Exception occurred during serialization: org.hibernate.exception.SQL
mmarException: could not initialize a collection: [vo.UserVO.books#1
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConvert
ava:90)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCException
er.java:66)
        at org.hibernate.loader.Loader.loadCollection(Loader.java:2022)
        at org.hibernate.loader.collection.CollectionLoader.initialize(Colle
nLoader.java:59)
        at org.hibernate.persister.collection.AbstractCollectionPersister.in
lize(AbstractCollectionPersister.java:587)
        at org.hibernate.event.def.DefaultInitializeCollectionEventListener.
itializeCollection(DefaultInitializeCollectionEventListener.java:83)
        at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.j
1743)
        at org.hibernate.collection.AbstractPersistentCollection.initialize(
ractPersistentCollection.java:366)
        at org.hibernate.collection.AbstractPersistentCollection.read(Abstra
rsistentCollection.java:108)
        at org.hibernate.collection.PersistentList.toArray(PersistentList.ja
46)
        at java.util.ArrayList.<init>(ArrayList.java:131)
        at flex.messaging.io.ArrayCollection.<init>(ArrayCollection.java:44)
        at flex.messaging.io.amf.Amf3Output.writeArrayCollection(Amf3Output.
:462)
        at flex.messaging.io.amf.Amf3Output.writeObject(Amf3Output.java:182)
        at flex.messaging.io.amf.Java15Amf3Output.writeObject(Java15Amf3Outp
ava:58)
        at flex.messaging.io.amf.Amf3Output.writeObjectProperty(Amf3Output.j
250)
        at flex.messaging.io.amf.Amf3Output.writePropertyProxy(Amf3Output.ja
73)
        at flex.messaging.io.amf.Amf3Output.writeCustomObject(Amf3Output.jav
5)
        at flex.messaging.io.amf.Amf3Output.writeObject(Amf3Output.java:200)
        at flex.messaging.io.amf.Java15Amf3Output.writeObject(Java15Amf3Outp
ava:58)
        at flex.messaging.messages.AbstractMessage.writeExternal(AbstractMes
.java:353)
        at flex.messaging.messages.AsyncMessage.writeExternal(AsyncMessage.j
140)
        at flex.messaging.messages.AcknowledgeMessage.writeExternal(Acknowle
essage.java:93)
        at flex.messaging.messages.AcknowledgeMessageExt.writeExternal(Ackno
geMessageExt.java:55)
        at flex.messaging.io.amf.Amf3Output.writePropertyProxy(Amf3Output.ja
63)
        at flex.messaging.io.amf.Amf3Output.writeCustomObject(Amf3Output.jav
5)
        at flex.messaging.io.amf.Amf3Output.writeObject(Amf3Output.java:112)
        at flex.messaging.io.amf.Java15Amf3Output.writeObject(Java15Amf3Outp
ava:58)
        at flex.messaging.io.amf.Amf0Output.writeObject(Amf0Output.java:198)
        at flex.messaging.io.amf.Java15Amf0Output.writeObject(Java15Amf0Outp
ava:69)
        at flex.messaging.io.amf.AmfMessageSerializer.writeObject(AmfMessage
alizer.java:196)
        at flex.messaging.io.amf.AmfMessageSerializer.writeBody(AmfMessageSe
izer.java:186)
        at flex.messaging.io.amf.AmfMessageSerializer.writeMessage(AmfMessag
ializer.java:142)
        at flex.messaging.endpoints.amf.SerializationFilter.invoke(Serializa
Filter.java:179)
        at flex.messaging.endpoints.BaseHTTPEndpoint.service(BaseHTTPEndpoin
va:278)
        at flex.messaging.MessageBrokerServlet.service(MessageBrokerServlet.
:322)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
icationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(Applicat
ilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrap
alve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardCont
alve.java:175)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostVal
ava:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportVal
ava:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngin
ve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter
a:263)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.
:844)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.p
ss(Http11Protocol.java:584)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.jav
7)
        at java.lang.Thread.run(Thread.java:619)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'deskc
favcategories' doesn't exist
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatemen
va:1332)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.j
1467)
        at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.j
208)
        at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
        at org.hibernate.loader.Loader.doQuery(Loader.java:697)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollection
ader.java:259)
        at org.hibernate.loader.Loader.loadCollection(Loader.java:2015)
        ... 46 more

21.02.2009 22:25:40 org.apache.catalina.startup.HostConfig checkResources
INFO: Undeploying context [/deskcook]
log4j:ERROR LogMananger.repositorySelector was null likely due to error in c
reloading, using NOPLoggerRepository.
21.02.2009 22:25:40 org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive deskcook.war
21.02.2009 22:25:42 org.apache.catalina.loader.WebappClassLoader loadClass
INFO: Illegal access: this web application instance has been stopped already
ould not load java.net.BindException.  The eventual following stack trace is
sed by an error thrown for debugging purposes as well as to attempt to termi
the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClas
der.java:1244)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClas
der.java:1204)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        at com.mysql.jdbc.CommunicationsException.<init>(CommunicationsExcep
.java:161)
        at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2759)
        at com.mysql.jdbc.MysqlIO.quit(MysqlIO.java:1410)
        at com.mysql.jdbc.Connection.realClose(Connection.java:4947)
        at com.mysql.jdbc.Connection.cleanup(Connection.java:2063)
        at com.mysql.jdbc.Connection.finalize(Connection.java:3403)
        at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
        at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
        at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)[/size]


I think my mapping is correct?!(hopefully) The only intressting think is,when i create a user (calling save()) hibernate doesn't create the table to map the context between User and Book. Maybe that is so,because at registration time a user hasn't got any Book? I really don't know!

I hope there is somebody who can bring a little bit light in this really dark room!

Greetz
Flavy

AND THANKS!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2009 11:03 pm 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
Looks like some object does not implement Serializable. Your UserVO and Book probably need to be serializable.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 7:21 am 
Newbie

Joined: Sat Feb 21, 2009 5:36 pm
Posts: 8
thanks for reply,
i didn't know that objects which i using with hibernate have to implement serializable. Is that just in this case or is that always so?
//EDIT: It don't work User,Category an Book implementing Serializable. But i get the same error! Other Ideas?!

Greetz Flavy


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

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
There was another interesting piece in you log:

Quote:
at java.lang.Thread.run(Thread.java:619)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'deskc
favcategories' doesn't exist


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 11:01 am 
Newbie

Joined: Sat Feb 21, 2009 5:36 pm
Posts: 8
yeah your are right!
Code:
        at java.lang.Thread.run(Thread.java:619)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'deskcook.
favcategories' doesn't exist


the tabel doesn't exist. But i don't know why it is not getting created.

I have a UserVO,which has a list of book (and categories). When i register a User,the user have no book and category, but why is hibernate not creating these tables to map this context,when i do the "save"-Operation (see register).

<hibernate-mapping package="vo">
<class name="UserVO">
<id name="id">
<generator class="native"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="email"/>

<list name="favCategories">
<key column="userID"/>
<list-index column="index"/>
<many-to-many column="categoryID" class="vo.CategoryVO"/>
</list>

<list name="books" cascade="all">
<key column="userID"/>
<list-index column="index"/>
<many-to-many column="bookID" class="vo.BookVO"/>
</list>

</class>
</hibernate-mapping>


These tables aren't created,but why?


Oh my god this error is pushing me down!

//EDIT:
My UserVO:

Code:
package vo;

import java.util.List;

public class UserVO{
   
   private String _username;
   private String _password;
   private String _email;
   
   private List<CategoryVO>_favCategories;
   private List<BookVO>_books;
   private int _id;
   
   public UserVO() {
   }
   
   public String getUsername() {
      return _username;
   }
   public void setUsername(String _username) {
      this._username = _username;
   }
   public String getPassword() {
      return _password;
   }
   public void setPassword(String _password) {
      this._password = _password;
   }
   public String getEmail() {
      return _email;
   }
   public void setEmail(String _email) {
      this._email = _email;
   }
   public List<CategoryVO> getFavCategories() {
      return _favCategories;
   }
   public void setFavCategories(List<CategoryVO> kategorien) {
      _favCategories = kategorien;
   }
   public List<BookVO> getBooks() {
      return _books;
   }
   public void setBooks(List<BookVO> _books) {
      this._books = _books;
   }
   public int getId() {
      return _id;
   }
   public void setId(int _id) {
      this._id = _id;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 11:26 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
I'm a little unclear on your questions about hibernate creating the "table(s)". I typically use hibernate starting with a db then working back.

But in your register() method what you are saving is a user
Code:
sess.save(newUser);
. You have to tell hibernate to assign members to the set i.e relationship.

Code:
e.g. myUserVO.getBooks().add(someReferenceToABook);
       sess.save(myUserVO);


TMK, the table should already be created. Looks like you'd need a join table for the <many-to-many>.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 11:38 am 
Newbie

Joined: Sat Feb 21, 2009 5:36 pm
Posts: 8
i am sorry, forgot that the tables are created, when Hibernate do his configuration...i am really sorry.

Then i have to say:

Why is hibernate not creating tables for the users collections?(books and categories)

Quote:
Looks like you'd need a join table for the <many-to-many>


Isn't the list-declaration enough? Could you explain that for a dummy ?!

Thank for your help!


//EDIT Could we discuss this problem live at AIM?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 11:58 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
No worries... Don't beat yourself up. We all have to start somewhere, make a bunch of mistakes, learn and grow. Most of all: have fun while you're putting yourself through all this torture :).

Code:
Isn't the list-declaration enough? Could you explain that for a dummy ?!


No the list declaration is not enough. You should explicitly add elements to the list.
For example, fetch a Book lets call it myBook, then add it to myUserVo.getBooks().add(myBook).

TMK, your join table (something like: UserBook table having userId, bookId columns) should already be created. These columns would be the primary key to UserBook. You'd also need to add foreign key constraints.

I'm not sure if this is helping you - let me know and I can continue.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 12:07 pm 
Newbie

Joined: Sat Feb 21, 2009 5:36 pm
Posts: 8
honestly i don't know what you mean...

When i am registrating a user the user have no book the list is empty.

But i think that isn't the problem...when i am do the hibernate configuration (Configuartion.configure() ...exprort schema and so on). The Tables to map the context between UserVO and e.g. CategoryVO or BookVO aren't created!


Perhaps you can getting online at AIM i have added you ;-)


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