【问题标题】:JPA @OneToMany Mapping ProblemJPA @OneToMany 映射问题
【发布时间】:2011-07-20 02:10:33
【问题描述】:

我正在尝试执行 JPA/Hibernate 映射来映射两个表,但出现此错误。任何帮助将不胜感激!

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

出现此错误

线程“主”org.hibernate.MappingException 中的异常:不能 确定类型:bb.entities.Restaurants,在表:MENUS,对于 列:[org.hibernate.mapping.Column(restaurant)] 在 org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306) 在 org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290) 在 org.hibernate.mapping.Property.isValid(Property.java:217) 在 org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:464) 在 org.hibernate.mapping.RootClass.validate(RootClass.java:235) 在 org.hibernate.cfg.Configuration.validate(Configuration.java:1362) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865) 在 bb.TestMain.setUp(TestMain.java:26) 在 bb.TestMain.main(TestMain.java:59)

谢谢。

【问题讨论】:

    标签: jpa hibernate-mapping


    【解决方案1】:

    使用@OneToMany 注释似乎是一种误解。 @OneToMany 注解用于表示 1:M 关系中的 1 侧,反向 @ManyToOne 关系用于表示 M 侧。因此,@OneToMany 注释应该定义在实体中的集合类型上,而不是普通引用类型上。

    因此,您应该:

    • 使用 @OneToOne 关联,如果这是实体之间关系的性质。
    • 或者,决定哪个实体代表 1:M 关系中的 1 侧。通过在Restaurants 中使用LinkedList 类,我认为Restaurants 类是1 面,并在Restaurants 类中使用@OneToMany 注释,同时使用反向@987654334 Menus 类中的 @ 关系。优化后的代码是:

    Restaurants.java

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

    Menus.java

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

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

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多