我是Hibernate的新手。在我的情况下,我有一个具有包含连接ID到许多其他表的记录的具体表 - 都具有相同的结构。 我想达成的目标是得到这样的Hibernate继承和多个表具有相同的类
SELECT *
FROM main_records mr, ref1 r1, ref2 r2
WHERE r1.id = mr.id_ref1
AND r2.id = mr.id_ref2;
主要的想法是重用类所有连接引用。
SQL
CREATE TABLE main_records
(
id integer NOT NULL,
id_ref1 integer NOT NULL,
id_ref2 integer NOT NULL
)
CREATE TABLE ref1
(
id integer NOT NULL,
value character varying
)
CREATE TABLE ref2
(
id integer NOT NULL,
value character varying
)
我设置基础POJO类
Java类
public class MainRecord {
private Integer id;
private Ref ref1;
private Ref ref2;
...
// getters and setters
}
public class Ref {
private Integer id;
private String value;
...
// getters and setters
}
我的想法是在下面的方式来定义的Hibernate映射:
定义一个抽象超类
<hibernate-mapping package="test">
<class abstract="true" name="Ref">
<id name="id" type="java.lang.Integer" column="ID">
<generator class="native" />
</id>
<property name="value" type="java.lang.String" column="VALUE" />
</class>
</hibernate-mapping>
地图的主要实体,扩展了超类,但使用单独的表
<hibernate-mapping package="test">
<union-subclass name="Ref1" table="REF1" extends="Ref" />
<union-subclass name="Ref2" table="REF2" extends="Ref" />
<class name="MainRecord" table="MAIN_RECORDS">
<id name="id" column="ID" type="java.lang.Integer" />
<many-to-one name="ref1" class="Ref1" column="ID_REF1" fetch="join" unique="true" />
<many-to-one name="ref2" class="Ref2" column="ID_REF2" fetch="join" unique="true" />
</union-subclass>
</class>
</hibernate-mapping>
我手动包括在配置映射文件,加载似乎确定但随后出现错误,没有任何详细说明:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.cxf.transport.servlet.ServletTransportFactory' defined in class path resource [META-INF/cxf/cxf-servlet.xml]: Error setting property values;
nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'bus' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/database.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'transportIds' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/database.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
该系统是Spring 2.5中的一个组合,具备Hibernate 3.2 CXF 2.3.4,Javassist是3.11,
我的问题是:
(一)这是正确的做法?
(B),只要我介绍
<union-subclass name="Ref1" table="REF1" extends="Ref" />
<union-subclass name="Ref2" table="REF2" extends="Ref" />
,所以我想这是不是做的最好的方式出现的错误?
(c)它可以用注释书写吗?我无法理解如何定义Ref1和Ref2类,而无需为它们创建POJO类。 (d)我可以使用多于一级的继承吗?例如,我想为我所有的具体表格使用一个抽象超类,它们涵盖了它们都有的共同审计领域? 让我们假设类Ref在Java和Hibernate映射中扩展了抽象AuditTable类。
感谢这个例子,进入了这个方向bot进入了新的麻烦。更多细节在这里http://stackoverflow.com/questions/7105832/hibernate-throws-wrongclassexception-for-a-simple-foreign-key – Andraz