2011-07-20 132 views
0

我正在尝试执行映射两个表的JPA/Hibernate映射,但遇到此错误。任何帮助将不胜感激!!JPA @OneToMany映射问题

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;} 
} 

有了这个错误

线程“main”中的异常org.hibernate.MappingException:不能 确定类型为:bb.entities.Restaurants,在表中:MENUS, 列:[org.hibernate.mapping.Column(restaurant)] at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)处 org.hibernate.mapping.Property.isValid org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290)(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.bui ldSessionFactory(Configuration.java:1865) 在bb.TestMain.setUp(TestMain.java:26)在 bb.TestMain.main(TestMain.java:59)

感谢。

回答

2

这似乎是使用@OneToMany注释时的一个误解。 @OneToMany注释用于以1:M关系表示1面,而逆关系用于表示M面。因此,@OneToMany注释应该在实体中的集合类型上定义,而不是在正常引用类型上定义。

您因此应:

  • 使用@OneToOne协会如果是这样的实体之间的关系的性质。
  • 或决定哪个实体表示1:M关系中的1面。在Restaurants使用LinkedList类去,我会考虑Restaurants类是1侧,并且,使用@OneToMany标注在Restaurants类,而使用在Menus类逆@ManyToOne关系。精致的代码将是:

Restaurants.java

... 
@OneToMany(mappedBy="restaurant") 
private List<Menus> menus = new LinkedList<Menus>(); 

菜单。的java

@ManyToOne 
@JoinColumn(name="restaurant") 
private Restaurants restaurant; 

注意在menus成员变量的声明从LinkedList<Menus>List<Menus>的变化。显然,在这种情况下,用集合的接口类型而不是具体的集合类来声明任何集合是明智的。其基本原理是基础JPA提供者将在运行时使用它自己的具体集合类型,以代理集合值。例如,Hibernate在运行时将使用PeristentList来表示被管实体中的List,而不是由实体创建的LinkedList。如果使用具体类型,Hibernate可能无法映射列,或者可能无法从数据库中检索关联的记录;我不确定运行时行为的细节,只是我知道最终的失败。