休眠有问题。我有实体用户
@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
我做错了什么?