Hibernate Annotations不适用于getter但适用于属性

Man*_*ann 5 java orm hibernate

我面临着hibernate注释的问题.对于下面显示的代码,我有一个酒店类,客户类,我使用customerhotelbooking来跟踪哪个客户预订了哪个酒店,反之亦然.但是当我在酒店和顾客的吸气剂上放置注释时,它给出了一个例外,令人惊讶的是,当我将它放在属性上时它会起作用.有人可以告诉为什么会如此?

`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)`
Run Code Online (Sandbox Code Playgroud)

我有酒吧豆,如图所示`

@Id
 @GeneratedValue
 private int id;

 private String description;

 private String city;

 private String name;

 private String rating ;

 private int isBooked; 
 `
Run Code Online (Sandbox Code Playgroud)

Cusomer豆为`

        @Id
 @GeneratedValue
 private int id;

 private String userName;

 private String password;
Run Code Online (Sandbox Code Playgroud)

`

和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;
 }
Run Code Online (Sandbox Code Playgroud)

Boz*_*zho 9

文档说:

2.2.2.2.访问类型

默认情况下,类层次结构的访问类型由@Id或@EmbeddedId注释的位置定义.如果这些注释位于某个字段上,则只考虑字段的持久性,并通过该字段访问该状态.如果注释在getter上,那么只考虑getter持久性,并通过getter/setter访问状态.这在实践中运作良好,是推荐的方法.

所以它是预期的和记录的行为 - 所以一致地放置你的注释 - 无论是字段还是getter.

(如果您阅读下面引用的文档,它说有一种方法可以使用@Access注释混合访问类型,但我不建议 - 保持一致.我个人更喜欢在字段上添加注释)