User类 主要部分
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name = "system-uuid",strategy="uuid")
@Column(length=32)
private String id;
@OneToMany(cascade={CascadeType.ALL},fetch = FetchType.EAGER,mappedBy="user")
// @JoinColumn(name="U_id")
private Set<Message> messageSet=new HashSet<Message>(0);
public User(){
}
public User(String username,Set<Message> mesSet){
this.userName=username;
this.messageSet=mesSet;
}
Message类 主要部分
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name = "system-uuid",strategy="uuid")
@Column(length=32)
private String id;
@Column(length=32)
private String address;
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="user_id")//加入一列作为外键
private User user;
执行查询语句
String hql = "select u.id,u.userName,u.age,m.address,m.id from User u,Message m where u.id=m.user_id";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
return query.list();
报错:
Servlet.service() for servlet [springMVC] in context with path [/test_ssh] threw exception [Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: user_id of: com.tgb.entity.Message [select u.id,u.userName,u.age,m.address,m.id from com.tgb.entity.User u,com.tgb.entity.Message m where u.id=m.user_id]] with root cause
org.hibernate.QueryException: could not resolve property: user_id of: com.tgb.entity.Message [select u.id,u.userName,u.age,m.address,m.id from com.tgb.entity.User u,com.tgb.entity.Message m where u.id=m.user_id]
------解决思路----------------------
异常不是说的很清楚了么: could not resolve property: user_id of: com.tgb.entity.Message
把HQL改成:select u.id,u.userName,u.age,m.address,m.id from User u,Message m where u.id=m.user 试一下