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