I am trying to do do JPA/Hibernate mappings to map two tables, but am getting this error. any help would be greatly appreciated!!
Restaurants.java
@Entity
@Table(name="RESTAURANTS")
public class Restaurants{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OneToMany(mappedBy="restaurant")
private LinkedList<Menus> menus = new LinkedList<Menus>();
/* constructors **/
public Restaurants(){
this.dateJoined = new Date();
};
/* getters and setters **/
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public LinkedList<Menus> getMenus() {return menus;}
public void setMenus(LinkedList<Menus> menus) {this.menus = menus;}
}
Menus.java
@Entity
@Table(name = "MENUS")
public class Menus {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private Long restaurantID;
@OneToMany
@JoinColumn(name="restaurant")
private Restaurants restaurant;
/* constructors */
public Menus(){}
/* getters and setters */
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
@Column(nullable = false)
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public Long getRestaurantID() {return restaurantID;}
public void setRestaurantID(Long restaurantID) {this.restaurantID = restaurantID;}
public void setRestaurant(Restaurants restaurant) {this.restaurant = restaurant;}
public Restaurants getRestaurant() {return restaurant;}
}
With this error
Exception in thread “main” org.hibernate.MappingException: Could not
determine type for: bb.entities.Restaurants, at table: MENUS, for
columns: [org.hibernate.mapping.Column(restaurant)] at
org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306) at
org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290) 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:235) at
org.hibernate.cfg.Configuration.validate(Configuration.java:1362) at
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
at bb.TestMain.setUp(TestMain.java:26) at
bb.TestMain.main(TestMain.java:59)
Thanks.
It appears to be a misconception in the use of the
@OneToManyannotation. The@OneToManyannotation is used to represent the 1-side in a 1:M relationship, and the inverse@ManyToOnerelationship is used to represent the M-side. Therefore, a@OneToManyannotation should be defined on a collection-type in an entity and not on a normal reference type.You should therefore:
@OneToOneassociation if that is the nature of the relationship between the entities.LinkedListclass inRestaurants, I would consider theRestaurantsclass to be the 1-side, and use the@OneToManyannotation in theRestaurantsclass, while using the inverse@ManyToOnerelationship in theMenusclass. The refined code would be:Restaurants.java
Menus.java
Note the change in the declaration of the
menusmember variable fromLinkedList<Menus>toList<Menus>. Apparently, in this case, it is wiser to declare any collection with the interface-type of the collection, instead of the concrete collection class. The rationale is that the underlying JPA provider will use it’s own concrete collection types at runtime, for the purpose of proxying the collection values. Hibernate for instance, will use aPeristentListat runtime, to represent the List in a managed entity, and not aLinkedListas created by the entity. If you use the concrete type, Hibernate might fail in mapping the column, or might fail in retrieving the associated records from the database; I’m not sure about the specifics of the runtime behavior, except that I know of the eventual failure.