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.  [ 2 posts ] 
Author Message
 Post subject: could not deserialize exception (simple test case provided)
PostPosted: Fri Jan 22, 2010 2:56 pm 
Newbie

Joined: Fri Jan 22, 2010 2:47 pm
Posts: 2
Folks
I have two tables employee and address:

Code:
drop table employee;
create table employee
(
  name varchar(30),
  street varchar(30),
  primary key (name)
) engine=innodb
;

drop table address;
create table address
(
  street varchar(30),
  city varchar(10),
  primary key (street)
) engine=innodb
;

insert into employee(name, street) values ('emp1', 'street1');
insert into employee(name, street) values ('emp2', 'street1');

insert into address(street, city) values( 'street1', 'city1');

select e.name, a.city
from employee e, address a
where e.street = a.street;


I also have the standard bean classes Employee and Address for these two tables. Both are serializable. I can reproduce them if required.

My hibernate mapping files for address is straight forward:
Code:
<?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="com.learning.hibernate.Address">
    <id name="street" column="street" type="string" />
    <property name="city" type="string" />
  </class>

</hibernate-mapping>


My hibernate mapping for Employee:
Code:
<?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="com.learning.hibernate.Employee">
    <id name="name" column="name" type="string" />

    <property name="address" type="com.learning.hibernate.Address" />

  </class>

  <sql-query name="empSql">
    <return alias="e" class="com.learning.hibernate.Employee">
      <return-property name="name" column="name" />
      <return-property name="address">
        <return-column name="city" />
      </return-property>
    </return>   
    <![CDATA[
      select e.name, a.city
        from employee e, address a
        where e.street = a.street             
    ]]>
  </sql-query>
</hibernate-mapping>


All I am trying to do is that when run the query, I want the Employee object created with the internal address property initialized. The relevant Java code is:

Code:
SessionFactory sf = (SessionFactory) beanFactory
        .getBean("mySessionFactory");
   
    Session session = null;
    try {
      session = sf.openSession();
      Query query = session.getNamedQuery("empSql");
      List emps = query.list();
      System.out.println("employees: " + emps);
    } finally {
      if (session != null) {
        session.close();
      }
    }


Can anyone please point out the mistake?

The full exception stack trace:
Code:
Exception in thread "main" org.hibernate.type.SerializationException: could not deserialize
   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:188)
   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:211)
   at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:105)
   at org.hibernate.type.SerializableType.get(SerializableType.java:62)
   at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
   at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173)
   at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
   at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2124)
   at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
   at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
   at org.hibernate.loader.Loader.getRow(Loader.java:1230)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
   at org.hibernate.loader.Loader.doQuery(Loader.java:724)
   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.custom.CustomLoader.list(CustomLoader.java:312)
   at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1722)
   at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
   at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175)
   at com.learning.hibernate.Map.main(Map.java:30)
Caused by: java.io.StreamCorruptedException: invalid stream header
   at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
   at java.io.ObjectInputStream.<init>(Unknown Source)
   at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:223)
   at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:180)
   ... 21 more


Thank you!


Top
 Profile  
 
 Post subject: Re: could not deserialize exception (simple test case provided)
PostPosted: Fri Jan 22, 2010 4:50 pm 
Newbie

Joined: Fri Jan 22, 2010 2:47 pm
Posts: 2
OK
I tried comparing the "named query" strategy that I am trying to get to work versus the query specified within Java code.

Following is the Employee.hbm.xml I used:
Code:
<?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="com.learning.hibernate.Employee" table="employee">
    <id name="name" column="name" type="string" />

    <!--
      <property name="address" type="com.learning.hibernate.Address" />
    -->
    <many-to-one name="address" class="com.learning.hibernate.Address"
      column="street" />

  </class>

  <sql-query name="empSql">
    <return alias="e" class="com.learning.hibernate.Employee">
      <return-property name="name" column="name" />
      <return-property name="address">
        <return-column name="city" />
      </return-property>
    </return>   
    <![CDATA[
      select e.name, a.city
        from employee e, address a
        where e.street = a.street             
    ]]>
  </sql-query>
</hibernate-mapping>


Following is the Java code:

Code:
package com.learning.hibernate;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Map {
  private static String[] springConfigFiles = { "src/config/hibContext.xml",
      "src/config/appContext.xml" };
  private static BeanFactory beanFactory = new FileSystemXmlApplicationContext(
      springConfigFiles);

  public static void main(String[] args) throws Exception {
    SessionFactory sf = (SessionFactory) beanFactory
        .getBean("mySessionFactory");

    Session session = null;
    try {
      session = sf.openSession();
      testQuery(session);
      testNamedQuery(session);
    } finally {
      if (session != null) {
        session.close();
      }
    }
  }

  private static void testNamedQuery(Session session) throws Exception {
    System.out.println("in test named query");
    SQLQuery query = (SQLQuery) session.getNamedQuery("empSql");
    processQuery(query);
  }

  private static void testQuery(Session session) throws Exception {
    System.out.println("in test regular query");
    SQLQuery query = session.createSQLQuery(" select e.name, a.city, a.street"
        + " from employee e, address a " + " where e.street = a.street");
    processQuery(query);
  }

  @SuppressWarnings("unchecked")
  private static void processQuery(SQLQuery query) throws Exception {
    query.addEntity(Employee.class);
    query.addEntity(Address.class);
    List emps = query.list();
    for (Object obj : emps) {
      Object[] arr = (Object[]) obj;
      Employee emp = (Employee) arr[0];
      System.out.println("employee: " + emp);
    }
  }
}


What I found is that the named query does not allow addEntity and gives the exception:
Code:
employee: name: emp1; address: street: street1; city: city1
employee: name: emp2; address: street: street1; city: city1
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.AbstractList.add(Unknown Source)
   at java.util.AbstractList.add(Unknown Source)
   at org.hibernate.impl.SQLQueryImpl.addEntity(SQLQueryImpl.java:304)
   at org.hibernate.impl.SQLQueryImpl.addEntity(SQLQueryImpl.java:285)
   at org.hibernate.impl.SQLQueryImpl.addEntity(SQLQueryImpl.java:289)
   at org.hibernate.impl.SQLQueryImpl.addEntity(SQLQueryImpl.java:277)
   at com.learning.hibernate.Map.processQuery(Map.java:47)
   at com.learning.hibernate.Map.testNamedQuery(Map.java:36)
   at com.learning.hibernate.Map.main(Map.java:26)


So if we can find out an equivalent of "addEntity" in named query configuration then the problem should be resolved:)

Any help is appreciated.

Thank you!


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