我有两个表所有者和成员,在成员表中有字段mid,同名字段在所有者中间但是有空值,我想从所有者表更新mid作为在成员表中间插入的值,两者都应该一样的使用hibernate所以我尝试了但是查询不正确。
public void updateOwnerMember(Owner owner) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hql="update Owner set Owner.mId = Member.mId from Member Owner inner join mId Member on Member.mId = Owner.mID where Owner.mId is null ";
Query query=sessionFactory.openSession().createQuery(hql);
query.setInteger(0,1);
query.setInteger(1,2);
query.executeUpdate();
tx.commit();
session.close();
}
答案 0 :(得分:0)
只有当mId不是主键时才可以这样做,否则您无法加入它们。
示例查询(未验证):
UPDATE Owner
SET Owner.mId = (SELECT Member.mId FROM Member WHERE m.id = o.id)