2010-11-17 57 views
5

我面臨着hibernate註釋的問題。對於下面顯示的代碼,我有一個酒店類,客戶類,我使用customerhotelbooking來跟蹤哪個客戶預訂了哪家酒店和副店-versa。 但是,當我把註釋放在酒店和客戶的getter上時,它會給出一個異常,並且當我將它放在屬性上時它會有效。 有人可以告訴爲什麼這樣嗎?Hibernate註解不適用於getters,但適用於屬性

`Caused by: org.hibernate.MappingException: Could not determine type for: com.xebia.hotelBooking.domain.Customer, at table: CustomerHotelBooking, for columns: [org.hibernate.mapping.Column(customer)] 
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:290) 
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:274) 
at org.hibernate.mapping.Property.isValid(Property.java:217) 
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:464) 
at org.hibernate.mapping.RootClass.validate(RootClass.java:236) 
at org.hibernate.cfg.Configuration.validate(Configuration.java:1193) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1378) 
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954) 
at org.jboss.seam.persistence.HibernateSessionFactory.createSessionFactory(HibernateSessionFactory.java:165) 
at org.jboss.seam.persistence.HibernateSessionFactory.startup(HibernateSessionFactory.java:79) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.jboss.seam.util.Reflections.invoke(Reflections.java:22) 
at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144) 
at org.jboss.seam.Component.callComponentMethod(Component.java:2249) 
at org.jboss.seam.Component.callCreateMethod(Component.java:2172) 
at org.jboss.seam.Component.newInstance(Component.java:2132)` 

我有酒店豆如`

@Id 
@GeneratedValue 
private int id; 

private String description; 

private String city; 

private String name; 

private String rating ; 

private int isBooked; 
` 

Cusomer bean作爲`

 @Id 
@GeneratedValue 
private int id; 

private String userName; 

private String password; 

`

和CustomerHotelBooking類作爲

 @Id 
@GeneratedValue 
private int id; 

private Hotel hotel; 

private Customer customer; 


     @ManyToOne 
@Cascade(value = { CascadeType.ALL }) 
public Customer getCustomer() { 
    return customer; 
} 

/** 
    * @param customer the customer to set 
    */ 
public void setCustomer(Customer customer) { 
    this.customer = customer; 
} 

/** 
    * @return the user 
    */ 



/** 
    * @return the hotel 
    */ 
     @ManyToOne 
@Cascade(value = { CascadeType.ALL }) 
public Hotel getHotel() { 
    return hotel; 
} 

/** 
    * @param hotel 
    *   the hotel to set 
    */ 
public void setHotel(Hotel hotel) { 
    this.hotel = hotel; 
} 

回答

6

The docs說:

2.2.2.2。訪問類型

默認情況下,類層次結構的訪問類型由@Id或@EmbeddedId註釋的位置定義。如果這些註釋位於一個字段上,那麼只有字段被認爲是持久性的,並且狀態通過該字段被訪問。如果註釋位於getter上,那麼只有getter被認爲是持久性的,並且通過getter/setter來訪問狀態。這在實踐中效果很好,並且是推薦的方法。

因此,它是預期和記錄的行爲 - 所以一直放置您的註釋 - 無論是領域或獲得者。 (如果您閱讀了我所引用的文檔,它說有一種方法可以使用@Access註釋混合訪問類型,但我不建議 - 保持一致。我個人更喜歡將註釋放在字段)

+0

感謝Bozho ...得到了你的觀點:) – Mann 2010-11-17 08:56:50

+0

不能將我的ID註釋移動到ID getter方法... – 2013-08-15 12:33:52