【发布时间】:2011-07-29 12:06:22
【问题描述】:
我对 Hibernate 还是很陌生。在我的情况下,我有一个具体的表,其中包含与许多其他表的连接 ID 的记录——所有表都具有相同的结构。 我想要实现的是得到类似
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,
我的问题是:
(a) 这是正确的方法吗?
(b) 我一介绍就出错了
<union-subclass name="Ref1" table="REF1" extends="Ref" />
<union-subclass name="Ref2" table="REF2" extends="Ref" />
所以我想这不是最好的方法吗?
(c) 可以用注解写吗?如果不为它们实际创建 POJO 类,我无法理解如何定义 Ref1 和 Ref2 类。 (d) 我可以使用 1 级以上的继承吗?例如,我想为我所有的具体表使用一个抽象超类,它涵盖了它们共有的审计字段? 假设类 Ref 在 Java 和 Hibernate 映射中扩展了一个抽象的 AuditTable 类。
【问题讨论】: