-->
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: How to use Generics
PostPosted: Wed Jan 03, 2007 11:17 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.x

Name and version of the database you are using: MySQL 5.x with InnoDB

Hi, I am all day looking for a way to map a generic class, like this

Code:
public class Version<T> {

  protected Version() {}

  public Version(   T data) {this.data = data;}

  void setData(T data) {
        this.data = data;
    }

  public T getData() {
          return data;
    }
}


Now I want to map this in a hbm-file, I cannot get it right.

The hbm-file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="org.openehr.rm.common.changecontrol.Version"
table="version">
<id type="long" column="_id" name="_id">
<generator class="identity"/>
</id>
<discriminator/>
<any name="data" meta-type="class" id-type="long" cascade="all">
<column name="data_class"/>
<column name="data_id"/>
</any>
</class>
</hibernate-mapping>


I tried several things.

I must say, maybe this helps. T can stand for a class known in this context (mapped in another hbm-file)
I also tried to use the class with string.

Like this
Code:
Version<String> v = new Version<String>("test");


I get this exception:
org.hibernate.MappingException: Unknown entity: java.lang.String
In this case it stops just before building the SQL-statement
DEBUG [main] (Cascades.java:836) - processing cascade ACTION_SAVE_UPDATE for: org.openehr.rm.support.identification.LocatableReference
DEBUG [main] (Cascades.java:861) - done processing cascade ACTION_SAVE_UPDATE for: org.openehr.rm.support.identification.LocatableReference
DEBUG [main] (Cascades.java:153) - cascading to saveOrUpdate: null


If I try it with a known class in this hibernate context (mapped in the same set of hbm-files. ) I get another error:
java.lang.ClassCastException: java.lang.String
In this case, it seems to stop inside the building of the SQL-statement.
(please not I show a simplified version of the problem, else it willb e too confusing, and I want you to save work

DEBUG [main] (Cascades.java:836) - processing cascade ACTION_SAVE_UPDATE for: org.openehr.rm.support.identification.LocatableReference
DEBUG [main] (Cascades.java:861) - done processing cascade ACTION_SAVE_UPDATE for: org.openehr.rm.support.identification.LocatableReference
DEBUG [main] (Cascades.java:153) - cascading to saveOrUpdate: null
DEBUG [main] (AbstractSaveEventListener.java:394) - persistent instance of: org.openehr.rm.support.identification.LocatableReference
DEBUG [main] (DefaultSaveOrUpdateEventListener.java:103) - ignoring persistent instance
DEBUG [main] (DefaultSaveOrUpdateEventListener.java:140) - object already associated with session: [org.openehr.rm.support.identification.LocatableReference#13]
DEBUG [main] (Cascades.java:861) - done processing cascade ACTION_SAVE_UPDATE for: org.openehr.rm.common.changecontrol.OriginalVersion
DEBUG [main] (BasicEntityPersister.java:1732) - Inserting entity: org.openehr.rm.common.changecontrol.OriginalVersion (native id)
DEBUG [main] (AbstractBatcher.java:290) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [main] (AbstractBatcher.java:324) - insert into version (signature, lifecycle_state_id, commit_audit_id, uid_id, preceding_version_id, contribution_id, data_class, data_id, is_merged_id, class) values (?, ?, ?, ?, ?, ?, ?, ?, ?, 'OriginalVersion')
Hibernate: insert into version (signature, lifecycle_state_id, commit_audit_id, uid_id, preceding_version_id, contribution_id, data_class, data_id, is_merged_id, class) values (?, ?, ?, ?, ?, ?, ?, ?, ?, 'OriginalVersion')
DEBUG [main] (AbstractBatcher.java:378) - preparing statement
DEBUG [main] (BasicEntityPersister.java:1612) - Dehydrating entity: [org.openehr.rm.common.changecontrol.OriginalVersion#<null>]
DEBUG [main] (NullableType.java:52) - binding null to parameter: 1
DEBUG [main] (NullableType.java:59) - binding '24' to parameter: 2
DEBUG [main] (NullableType.java:59) - binding '6' to parameter: 3
DEBUG [main] (NullableType.java:59) - binding '23' to parameter: 4
DEBUG [main] (NullableType.java:52) - binding null to parameter: 5
DEBUG [main] (NullableType.java:59) - binding '15' to parameter: 6
DEBUG [main] (AbstractBatcher.java:298) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [main] (AbstractBatcher.java:416) - closing statement

As you may have guessed, the problem occured in OriginalVersion which is a child of (abstract) Version. But I don't believe that that is the problem.
You see that it does no try to bind paramters 7, 8, etc.

I think the problem is in the hbm file, (wehere else can it be?)

thanks for any help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 6:17 pm 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
Solved myself!!

watch the Serializable

Code:
public class Version <T extends Serializable> {

   protected Version() {}

   public Version(   T data) {this.data = data;}

   void setData(T data) {
         this.data = data;
     }

   public T getData() {
           return data;
     }
}




watch the red line
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="org.openehr.rm.common.changecontrol.Version"
table="version">
<id type="long" column="_id" name="_id">
<generator class="identity"/>
</id>
<discriminator/>

<property name="data" column="data" length="65535"/>
</class>
</hibernate-mapping>


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.