我被 hibernate delete 困住了,它的集合类型给出了带有外部约束的异常。
这是我的 hibernate 表
@Entity
@Table
public class FrontRequest implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long tutorfront_id;
@Column
private String tutorialType;
@Column
private String tutorialLevel;
@ElementCollection(fetch=FetchType.EAGER)
@Fetch(value = FetchMode.SELECT)
private Collection<String> tutorialSubjects= new HashSet<String>();
@Column
private String tutorialCity;
@Column
private String noOfStudent;
@Column
private String classWeek;
@Column
private String tutionFee;
@Column
private String tutionFeerate;
@Column
private String tutorSex;
@Column
private String tutorEducation;
@Column
private Date postingDate;
@Column
private String studentSchool;
}
这是我删除对象的服务方法
public boolean removeFrontRequest(Long id) {
// TODO Auto-generated method stub
Session session= SessionFactoryImpl.returnService().getCurrentSession();
Transaction tx;
if(session.getTransaction() != null
&& session.getTransaction().isActive()){
tx=session.getTransaction();
}
else{
tx= session.beginTransaction();
}
try{
Query query=session.createQuery("from FrontRequest where tutorfront_id =:id");
query.setParameter("id", id);
FrontRequest frontRequest=(FrontRequest) query.list().get(0);
session.delete(frontRequest);
if(!tx.wasCommitted()) {
tx.commit();
}
}
catch(Exception e){
return false;
}
return true;
}
但是它给出了锁异常: SQL 错误:1205,SQLState:40001 超过锁定等待超时;尝试重启交易
请你指出我哪里做错了。这里..
谢谢
最佳答案
通过将 != 替换为 == 来更正您的 if 条件。提交后也结束事务。
关于java - hibernate delete with string 类型集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36198589/