0

休眠有问题。我有实体用户

@Entity
@Table(name = "user")

public class User {
@Id
@Column(name = "user_id")
@GeneratedValue
private Long userId;

@Column
@NotBlank(message = "Fill login")
private String login;

@OneToMany(fetch=FetchType.LAZY,mappedBy="user")
private List<Order> orders;

public List<Order> getOrders() {
    return orders;
}
public void addOrders(Order order) {
    this.orders.add(order);
}

也像你可以看到用户有订单。所以在 ORDER 表中我有指向 USER_ID 的 FK 所以这里是我的实体 Order

@Entity
@Table(name = "order")
public class Order {

    @Id
    @Column(name="order_id")
    @GeneratedValue
    private long orderId;   
    @Column 
    private long user_id;
    @Column
    private Date dateBegin;
    private Date dateEnd;

}

这里是我的单元测试

public class HibernateTutorial extends BaseTest{

    @Autowired
    SessionFactory sessionFactory;
    @Test
    public void hibernateTutorial(){        
        System.out.print(sessionFactory);       

        List<User> users =  sessionFactory.getCurrentSession().createQuery("from User").list();
        List<Order> orders = users.get(0).getOrders(); 

        System.out.print(orders);   
        Assert.assertNotNull(users);    

    }

}

运行时出现异常:

Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property  entity.Order.user in entity.User.orders
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:576)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:541)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1177)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1162)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 38 more

我做错了什么?

4

3 回答 3

0

You need an instance of User in your Order class.

Here is an example of One-to-many.

So, you might add something like this to the Order class:

private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
public User getUser() {
  return this.user;
}
于 2012-05-25T16:19:17.597 回答
0

mappedBy 属性指示关联表中定义关系的字段。如果您想保持 mappedBy 原样,您需要添加:

private User user;

到您的订单类别。此外,您需要在关系的这一侧添加一个 @ManyToOne 并定义连接列的语义。双向性并不总是必需的,但在这种情况下,您的业务似乎需要让用户知道订单,这是正确的方法。

作为一般规则,您应该在 Hibernate 实体之间使用完整的 Object 关系,而不是让实体引用彼此的 id 值。Hibernate 将为您正确管理保存和更新,并且获取 id 与完整对象很容易由您自己管理。

于 2012-05-25T16:29:05.613 回答
0

这个:

@Column 
private long user_id;

需要替换为引用

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="user_id")
private User user;

和多对一的映射。

于 2012-05-25T16:24:38.230 回答