2013-08-31 61 views
1

自從幾周以來,我一直在使用Hibernate。那麼它是一個非常有用的工具,但我不能解析以下任務:Hibernate繼承SingleTable子類加入

表:

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 { 
     ... 
    } 

有兩種類型的實例,其中一個「項目「可能但不一定總能配上」捆綁「。 「捆綁」至少有一個「項目」

@Entity 
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
    @DiscriminatorValue(value = "Item") 
    public class Item extends Product { 
     Bundle bundle; 
     .... 
     @ManyToOne (fetch=FetchType.LAZY, targetEntity=Bundle.class) 
    @JoinColumn (name="bundle_id") 
     public Bundle getBundle() {...} 
     public void setBundle(Bundle bundle) {...} 
     .... 
    } 

和:

@Entity 
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
    @DiscriminatorValue(value = "Bundle") 
    public class Bundle extends Product { 
     Set<Item> items; 
     .... 
     @OneToMany (mappedBy="bundle", targetEntity=Item.class) 
    @OrderBy ("list_nr") 
    public Set<Item> getItems() {...} 
     public void setItems(Set<Item> items) {...} 
     .... 
    } 

在運行時它不是可以調用任何數據,錯誤: 預期類型:org.blah.Bundle,實際值:org.blah.Item

有沒有人有想法或提示。正在谷歌上漲&下來,但我找不到這個具體問題。

不知道爲什麼Hibernate的嘗試:

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 

findAllItems():

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()); 
    } 
} 
+0

1.請添加完整的堆棧跟蹤錯誤。 2.不可能調用任何數據。究竟是什麼? – shevchyk

+0

跟蹤: 2013年9月1日零時36分49秒org.hibernate.property.BasicPropertyAccessor $ BasicSetter設置 錯誤:HHH000123:拋出:IllegalArgumentException類:org.blah.Item,財產setter方法:束 2013年9月1日00:36 :49 org.hibernate.property.BasicPropertyAccessor $ BasicSetter set 錯誤:HHH000091:預期類型:org.blah.Bundle,實際值:org.blah.Item 01.09.2013 00:36:49 org.blah.QueryMngr findAllItems 幾個:得到失敗 – eddiriarte

+0

其他任何工作,但這個屬性不能填充,我得不到數據返回。這就是爲什麼我試圖使用targetEntity。 – eddiriarte

回答

0

在從

@OneToMany (mappedBy="bundle", targetEntity=Bundle.class) 
類變化

@OneToMany (mappedBy="bundle", targetEntity=Item.class) 

或移除參數targetEntity可言。

Hibernate documentation

@ManyToOne有一個名爲targetEntity參數描述目標實體名稱。 您通常不需要此參數,因爲在幾乎所有情況下,默認值(存儲關聯的屬性的類型)都很好。但是,如果要將接口用作返回類型而非常規實體,則這很有用。