我有 2 个表(用户和个人资料),其中包含以下 @ManyToMany 关系:
简介类:
@Cache (usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"name"}))
public class Profile implements Serializable {
private static final long serialVersionUID = 1L;
private Long m_id;
private String m_name;
private List<User> m_users= new ArrayList<User>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return m_id;
}
private void setId(Long id) {
m_id = id;
}
public String getName() {
return m_name;
}
public void setName(String name) {
m_name = name;
}
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},
mappedBy="profiles")
public List<User> getUsers() {
return m_users;
}
public void setUsers(List<User> users) {
m_users = users;
}
用户等级:
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long m_id;
private String m_name;
private List<Profile> m_profiles = new ArrayList<Profile>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return m_id;
}
private void setId(Long id) {
m_id = id;
}
@NaturalId
@Column(unique = true, nullable = false)
public String getName() {
return m_name;
}
@Cache (usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "User_Profile",
joinColumns = { @JoinColumn(name = "user_id") },
inverseJoinColumns = { @JoinColumn(name = "profiles_id") })
public List<Profile> getProfiles() {
return m_profiles;
}
}
当我使用大型用户对象执行保存或更新数据时,休眠会话不起作用。
例子:
List<User> users = new ArrayList(); users.add(new User("test", ..));
users.add(new User());
// 3500x... new User()
Profile profile = new Profile(name, users);
getSession().saveOrUpdate(profile);
会话不起作用并引发了一些异常
Could not execute JDBC batch update; SQL [delete from User_Profile
where user_id=?]; nested exception is
org.hibernate.exception.LockAcquisitionException:
Could not execute JDBC batch update