Java Hibernate继承单表子类联接
我和Hibernate一起工作了几个星期。这是一个非常有用的工具,但我无法解决以下任务: 表:Java Hibernate继承单表子类联接,java,hibernate,single-table-inheritance,self-join,Java,Hibernate,Single Table Inheritance,Self Join,我和Hibernate一起工作了几个星期。这是一个非常有用的工具,但我无法解决以下任务: 表: Create Table `Product` ( `product_id` INT(10) PRIMARY KEY, `bundle_id` INT(10) NULL, `product_type` VARCHAR(50) NOT NULL, `title` VARCHAR(255) NOT NULL, `desc` VARCHAR(255) NULL, `price`
Create Table `Product`
(
`product_id` INT(10) PRIMARY KEY,
`bundle_id` INT(10) NULL,
`product_type` VARCHAR(50) NOT NULL,
`title` VARCHAR(255) NOT NULL,
`desc` VARCHAR(255) NULL,
`price` REAL(10) NOT NULL,
...
);
在Java中,我有3个类
@Entity
@Table(name = "Product")
@DiscriminatorColumn(name = "product_type")
public abstract class Product {
...
}
有两种类型的实例,其中一个“项目”可以但可能并不总是值得拥有一个“捆绑包”。“捆绑包”至少有一个“项”
以及:
错误:
01.09.2013 00:36:49 org.hibernate.property.BasicPropertyAccessor$BasicSetter
set ERROR: HHH000123: IllegalArgumentException in class: org.blah.Item,
setter method of property: bundle 01.09.2013 00:36:49
org.hibernate.property.BasicPropertyAccessor$BasicSetter set ERROR: HHH000091:
Expected type: org.blah.Bundle, actual value: org.blah.Item 01.09.2013 00:36:49
org.blah.QueryMngr findAllItems SEVERAL: get failed
findAllItems():
公共静态列表findAllItems(){
调试(“查找所有项实例”);
会话=空;
试一试{
session=HibernateUtil.getSessionFactory().openSession();
session.getTransaction().begin();
列表项=session.createQuery(“来自项”).List();
//用于(项目:项目){
//初始化(item.getBundle());
//}
session.getTransaction().commit();
调试(“获得成功”);
session.close();
退货项目;
}捕获(HibernateeException exc){
if(会话!=null){
session.getTransaction().rollback();
session.close();
}
日志错误(“获取失败”,exc);
抛出新的RuntimeException(exc.getMessage());
}
}
在捆绑包中
从
@OneToMany (mappedBy="bundle", targetEntity=Bundle.class)
到
或者完全删除参数targetEntity
发件人:
@ManyToOne
有一个名为targetEntity
的参数,用于描述目标实体名称通常不需要此参数,因为默认值(存储关联的属性类型)在几乎所有情况下都是良好的。但是,当您希望使用接口作为返回类型而不是常规实体时,这非常有用。1。请添加错误的完整堆栈跟踪。2.不可能调用任何数据。具体是什么?跟踪:01.09.2013 00:36:49 org.hibernate.property.basicProperty访问者$BasicSetter设置错误:HH000123:IllegalArgumentException在类中:org.blah.Item,属性的setter方法:bundle 01.09.2013 00:36:49 org.hibernate.property.basicProperty访问者$BasicSetter设置错误:HHH000091:预期类型:org.blah.bundle,实际值:org.blah.Item 01.09.2013 00:36:49 org.blah.QueryMngr findAllItems几个:get Failed其他任何东西都在工作,但此属性无法填充,并且我没有返回任何数据。这就是我尝试使用targetEntity的原因。请同时显示FindAllitem
Hibernate:
select
item0_.item_id as product1_7_,
item0_1_.price as price3_7_,
item0_1_.title as title4_7_,
item0_.bundle_id as bundle3_11_
from
Item item0_
inner join
Product item0_1_
on item0_.item_id=item0_1_.product_id
01.09.2013 00:36:49 org.hibernate.property.BasicPropertyAccessor$BasicSetter
set ERROR: HHH000123: IllegalArgumentException in class: org.blah.Item,
setter method of property: bundle 01.09.2013 00:36:49
org.hibernate.property.BasicPropertyAccessor$BasicSetter set ERROR: HHH000091:
Expected type: org.blah.Bundle, actual value: org.blah.Item 01.09.2013 00:36:49
org.blah.QueryMngr findAllItems SEVERAL: get failed
public static List<Item> findAllItems() {
log.debug("find all Item instances");
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
session.getTransaction().begin();
List<Item> items = session.createQuery("From Item").list();
//for (Item item : items) {
// Hibernate.initialize(item.getBundle());
//}
session.getTransaction().commit();
log.debug("get successful");
session.close();
return items;
} catch (HibernateException exc) {
if (session != null) {
session.getTransaction().rollback();
session.close();
}
log.error("get failed", exc);
throw new RuntimeException( exc.getMessage() );
}
}
@OneToMany (mappedBy="bundle", targetEntity=Bundle.class)
@OneToMany (mappedBy="bundle", targetEntity=Item.class)