Idan
Idan

Reputation: 951

Hibernet with java ArrayList

i'm try to persist object (PERSON) with ArrayList but i get this execption:

ERROR:2012-04-23 20:58:54,336 org.hibernate.property.BasicPropertyAccessor  - IllegalArgumentException in class: Entities.Person, setter method of property: msgList
ERROR:2012-04-23 20:58:54,336 org.hibernate.property.BasicPropertyAccessor  - expected type: java.util.ArrayList, actual value: org.hibernate.collection.PersistentList

Person class:

public class Person implements Serializable {

    static final long serialVersionUID = 1L;

    private long id;
    private String _email;
    private ArrayList<AnsMsg> msgList=new ArrayList<AnsMsg>();

     long getId() {
        return id;
    }
  public void setId(long id) {
    this.id = id;
}
  public String getEmail() {
    return _email;
}


public void set_email(String _email) {
    this._email = _email;
}

public ArrayList<AnsMsg> getMsgList() {

   return msgList;
}


public void setMsgList(ArrayList<AnsMsg> msgList) {
    this.msgList = msgList;
}
}

Person Hbm:

    <hibernate-mapping>
    <class name="Entities.Person" table="PERSON">
        <id name="id" type="long">
            <column name="ID" />
            <generator class="increment" />
        </id>
        <property name="_email" type="java.lang.String" access="field">
            <column name="_EMAIL" />
        </property>

     <list name="msgList" cascade="all"  >
           <key column="parent_id"/>
             <index column="idx" />
            <one-to-many class="msg.AnsMsg" />
        </list>

    </class>
</hibernate-mapping>

now i don't want to use List instead ArrayList

(private List<AnsMsg> msgList=new ArrayList<AnsMsg>();)

because when i get person from DB Hibernate return org.hibernate.collection.PersistentList and i need to Serialize and Deserialize this object (Java server and android app) and "org.hibernate.collection.PersistentList" is't implements Serializable (or i'm wrong because i get null object when i try to Deserialize in android side).

this is my Deserialize/Serialize method:

 public static byte[] serializeObject(Object o) { 
            ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

            try { 
              ObjectOutput out = new ObjectOutputStream(bos); 
              out.writeObject(o); 
              out.close(); 


              byte[] buf = bos.toByteArray(); 

              return buf; 
            } 
            catch(IOException ioe)
            { 


              return null; 
            }
              catch(Exception e)
                {


                return null;
                }

          } 

 public static Object deserializeObject(byte[] b) {



Object object=null;
          try { 


ObjectInputStream in = new ObjectInputStream(new       ByteArrayInputStream(b)); 
               object = in.readObject(); 
              in.close(); 

              return object; 
        } catch(ClassNotFoundException cnfe) { 


          return null; 
        } catch(IOException ioe) { 


          return null; 
        } 
        catch(Exception e)
        {

        String ex=e.toString();
        return object; 
        }
      }

Update:

when i try to use List instead ArrayList

when i try do Deserialize this object in the android side (with "deserializeObject" method ) when i get to

object = in.readObject(); 

line it's throw

java.lang.ClassNotFoundException: org.hibernate.collection.PersistentList. 

this way i try to use ArrayList.

how can i map this array list? or there is a batter way to work with Hibernate in this case?

thanks in advance..

Upvotes: 0

Views: 5583

Answers (3)

viktor
viktor

Reputation: 1297

IMHO You are going the wrong way. These are options you have:

  • Copy the entities to POJOs and send them to android device. (Ineffective)
  • Add hibernate as library to your android app. This is too much for the android. (Bloats your android app)
  • Use different means of serialization (JSON, XML)

I would use different means of serialization - JSON. There are libraries that make the process quite painless.

Upvotes: 1

Shehzad
Shehzad

Reputation: 2940

You are using the ArrayList which is the implementation of List interface and hibernate is returning PersistentList which is also the implementation of List interface. So you should not be specific to ArrayList change Your ArrayList to java.util.List so it can accept any implementation of java.util.List . It will also solves your issue.

Upvotes: 3

ianpojman
ianpojman

Reputation: 1783

your types in your POJO method signatures are still ArrayList, you need to change it to List everywhere

Upvotes: 1

Related Questions