Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Annotations not working for getters but working for attributes

I am facing problem with hibernate annotations.For the code shown below I have a hotel class ,customer class and i use customerhotelbooking to keep track of which customer has booked which hotel and vice-versa. but when i place annotations on getters of hotel and customer it gives an exception and surprisingly it works when i place it on attributes. can somebody tell why is it so??

`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)`

I have Hotel bean as shown `

@Id
 @GeneratedValue
 private int id;

 private String description;

 private String city;

 private String name;

 private String rating ;

 private int isBooked; 
 `

Cusomer bean as `

        @Id
 @GeneratedValue
 private int id;

 private String userName;

 private String password;

`

and CustomerHotelBooking class as

       @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;
 }
like image 302
Mann Avatar asked Nov 17 '10 07:11

Mann


People also ask

Which annotation will be used to fetch the property to attribute?

The fetch attribute of the @ManyToMany annotation allows you to define the FetchType that shall be used for this association. The FetchType defines when the persistence provider fetches the referenced entities from the database.

What is @basic annotation in Hibernate?

We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.

Does Hibernate need getters and setters?

Although Hibernate does not require it, it is recommended to follow JavaBean conventions by defining getters and setters for you entities persistent attributes. You can still tell Hibernate to directly access the entity's fields.

Which of the following statements correctly describe Hibernate Annotations?

Q 13 - Which of the following is true about hibernate annotations? A - Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping.


1 Answers

The docs say:

2.2.2.2. Access type

By default the access type of a class hierarchy is defined by the position of the @Id or @EmbeddedId annotations. If these annotations are on a field, then only fields are considered for persistence and the state is accessed via the field. If there annotations are on a getter, then only the getters are considered for persistence and the state is accessed via the getter/setter. That works well in practice and is the recommended approach.

So it is expected and documented behaviour - so place your annotations consistently - either fields or getters.

(If you read the documentation below what I quoted, it says there is a way to mix access types, using the @Access annotation, but I would not recommend that - be consistent. I, personally, prefer putting the annotations on the fields)

like image 194
Bozho Avatar answered Oct 28 '22 04:10

Bozho