2015-03-13 112 views
0

我也遇到了這個例外休眠無法找到邏輯名稱

Caused by: 
org.hibernate.MappingException: Unable to find column with logical name: picture_id in org.hibernate.mapping.Table(TXN_INVENTORY_PICTURE) and its related supertables and secondary tables 
     at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:564) 
     at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:258) 
     at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116) 
     at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1596) 
     at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1519) 
     at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1420) 
     at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) 
     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) 

下面是這些類的要點涉及

@Entity 
@Table(name = "REF_PRODUCT") 
public class Product extends JpaModel{ 

    @Column(name= "NAME") 
    private String name; 

    @Column(name = "MANUFACTURER") 
    private String manufacturer; 

    /** 
    * Work around for missing 
    */ 
    @OneToOne(optional = true) 
    @JoinColumn(name="picture",referencedColumnName = "picture_id") 
    private InventoryItemPicture picture; 
} 

而這裏的inventoryItemPicutre

@Entity 
@Table(name = "TXN_INVENTORY_PICTURE") 
public class InventoryItemPicture extends JpaModel{ 

    @Column 
    private byte[] image; 

    @Column 
    private String fileName; 

    public InventoryItemPicture(){ 

    } 
.... 
} 

而這裏的JPAModel

@MappedSuperclass 
public abstract class JpaModel { 

    @Id 
    @Column(name ="ID", columnDefinition = "serial") 
    @GeneratedValue(strategy = GenerationType.SEQUENCE) 
    private Long id; 

    @Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime") 
    @Column(name = "created_date") 
    private DateTime createdDate; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public DateTime getCreatedDate() { 
     return createdDate; 
    } 

    public void setCreatedDate(DateTime createdDate) { 
     this.createdDate = createdDate; 
    } 
} 

注:省略了getter和setter方法,而我想在這裏實現的是該圖片可以在產品類爲空

+0

有一列'picture_id'表'TXN_INVENTORY_PICTURE'? – 2015-03-13 15:42:33

+0

@PredragMaric沒有,但它有一個id字段。 – user962206 2015-03-13 15:48:04

回答

-1

您的映射錯誤,referencedColumnName目標列picture_idTXN_INVENTORY_PICTURE表,這是不存在的。

如果picture_idREF_PRODUCT表中的列是一個外鍵TXN_INVENTORY_PICTURE表的名稱,然後更改映射到這個

@OneToOne(optional = true) 
@JoinColumn(name="picture_id") 
private InventoryItemPicture picture; 
+0

已經嘗試過,但它顯示了一個非null約束的錯誤。我想要的是圖片對象也可以設置爲null – user962206 2015-03-13 15:58:14

+0

我已經編輯了我的答案,但不能確定沒有數據庫結構的確切名稱,但您會明白。如果數據庫是使用這些註釋從實體模型創建的,那麼'picture_id'將被創建爲空。如果數據庫已經創建,並且'picture_id'不能爲空,則直接在數據庫中將其更改爲可空。 JPA僅使用這些註釋來從實體模型生成數據庫模式。 – 2015-03-13 16:02:21

+0

已經嘗試過。我使用實體模型爲我生成表,但外鍵picture_id始終設置爲非null。我必須在數據庫中進行特別更改,是否有解決此問題的方法 – user962206 2015-03-13 16:16:06