-->
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.  [ 3 posts ] 
Author Message
 Post subject: Polymorphic relationship and ID auto increment
PostPosted: Mon Oct 04, 2010 5:51 pm 
Newbie

Joined: Thu Sep 23, 2010 11:14 am
Posts: 10
Hi All!!

This is my first post in this forum.

I will try to be clear with my problem.

I have some clases and it's interfaces:
Interface Element (top level)
Code:
   public interface Element
   {

      public final long nullID               = 0L;

      public long getID();
      public void setID(long value);
      
      public long getParentID();
      public void setParentID(long value);

      public String getName();
      public void setName(String value);
   }


Class ElementImpl who implements Element interface
Code:
   public class ElementImpl implements Element
   {

      private long _id = Element.nullID;
      private long _parent_id = Element.nullID;
      private String _name = "";
      
      public long getID()           { return _id;  }
      public void setID(long value) { _id = value; }
      
      public long getParentID()           { return _parent_id;  }
      public void setParentID(long value) { _parent_id = value; }

      public String getName()           { return _name;  }
      public void setName(String value) { _name = value; }
   }


Interface Document
Code:
   public interface Document extends Element
   {
      public final String TYPE = "Document";
      public final String ATTR_DEFAULTCHILD = "DEFAULTCHILD";
      public final String ATTR_EXPORTNAME   = "EXPORTNAME";
      
      public Document clone();
      
      public long getDefaultChild();
      public void setDefaultChild(long value);
      
      public String getExportName();
      public void setExportName(String value);
   }


Class DocumentImpl who implements Document interface
Code:
   public class DocumentImpl implements Document
   {
      private long _id            = Element.nullID;
      private long _defaultchild  = Element.nullID;
      private long _parent_id     = Element.nullID;
      private String _export_name = "";
      private String _name        = "";

      public Document clone() { return null; }
      
      public long getDefaultChild()           { return _defaultchild;  }
      public void setDefaultChild(long value) { _defaultchild = value; }

      public String getExportName()           { return _export_name;  }
      public void setExportName(String value) { _export_name = value; }

      public long getID()           { return _id;  }
      public void setID(long value) { _id = value; }

      public String getName()           { return _name;  }
      public void setName(String value) { _name = value; }

      public long getParentID()           { return _parent_id;  }
      public void setParentID(long value) { _parent_id = value; }
   }


so, I made this mapping for those classes:
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="test.ElementImpl" table="element">
         <id name="_id" type="long" access="field">
            <column name="ID" />
            <generator class="increment" />
         </id>
         <property name="_name" type="java.lang.String" access="field">
            <column name="NAME" />
         </property>
         <property name="_parent_id" type="long" access="field">
            <column name="PARENT_ID" />
         </property>
      
         <union-subclass name="test.DocumentImpl" table="document" extends="test.ElementImpl">
            <property name="_defaultchild" type="long" access="field">
               <column name="DEFAULTCHILD" />
            </property>
            <property name="_export_name" type="java.lang.String" access="field">
               <column name="EXPORTNAME" />
            </property>
         </union-subclass>
      </class>
   </hibernate-mapping>


My Configuration XML is this one:
Code:
   <hibernate-configuration>
      <session-factory>
         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
         <property name="connection.url">jdbc:mysql://localhost/cms_demo_hibernate</property>
         <property name="connection.username">root</property>
         <property name="connection.password">sesamo</property>

         <!-- Disable the second-level cache -->
         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

         <!-- Echo all executed SQL to stdout -->
         <property name="show_sql">true</property>
         <property name="format_sql">true</property>

         <!-- Drop and re-create the database schema on startup -->
         <property name="hbm2ddl.auto">create-drop</property>

         <mapping resource="Mapeos.hbm.xml"/>
      </session-factory>
   </hibernate-configuration>


Here is my problem:
- When I use "identity" value for the ID I got this exception:
Code:
Cannot use identity column key generation with <union-subclass> mapping for: test.ElementImpl

Researching, I find that I can't use "identity" whit Polymorphic enviroment (somebody can tell me why??), researching more I find that can use the value "increment" that is most near to "identity". When I use "increment" got this exception:
Code:
Initial SessionFactory creation failed.org.hibernate.PropertyAccessException: could not get a field value by reflection getter of test.ElementImpl._id
   ...stacktrace...
   Caused by: java.lang.IllegalArgumentException: Can not set long field test.ElementImpl._id to test.DocumentImpl


What I want to do:
.- I need to make one table per concrete class with unique ID on each table like.
Table Document:
ID NAME
1 Doc1
2 Doc2
3 Etc

Table Element
ID NAME
1 element1
2 Ele2
3 Etc

Aditional questions:
.- How can I implement logging for hibernate with java code? (log4j).
.- Can I configure some things in java code and some other things (like default things) in the XML and load both for one session?
.- The HSQLDialect dialect is a hibernate dialect? or is from some DB?

Thanks in advance.
Sorry for my english.
Regards Juan Pablo From Argentina


Top
 Profile  
 
 Post subject: Re: Polymorphic relationship and ID auto increment
PostPosted: Wed Oct 06, 2010 6:40 am 
Newbie

Joined: Tue Feb 02, 2010 10:37 am
Posts: 13
To store Element and Document entities in two separate tables each having it's own-id, you have to write separating mappings for Document and Element,usage of joined-subclass will not yield your expected results.
Use separate mappings so that Document and Element inheritance is captured only in the beans and not in the hibernate mapping.


Top
 Profile  
 
 Post subject: Re: Polymorphic relationship and ID auto increment
PostPosted: Thu Oct 07, 2010 7:39 am 
Newbie

Joined: Thu Sep 23, 2010 11:14 am
Posts: 10
vijaybangy wrote:
To store Element and Document entities in two separate tables each having it's own-id, you have to write separating mappings for Document and Element,usage of joined-subclass will not yield your expected results.
Use separate mappings so that Document and Element inheritance is captured only in the beans and not in the hibernate mapping.


Thanks for your answer.

I will try it.

Regards Juan Pablo


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