下面有五个域类(动态编程)。我需要更新记录清单标题中有两个列表,这些列表又有两个列表,所以共有五个域。我需要更新所有五条记录,在更新期间我得到以下异常:
引起:org.hibernate.exception.ConstraintViolationException:无法删除或更新父行:外键约束失败(
new1
。checklist_property
,CONSTRAINTFK14E0B84E32ADD034
FOREIGN KEY({ {1}})参考checklist_property_title
(checklist_property_title
))
我删除子记录以放入新记录。
id
答案 0 :(得分:0)
package com.stie.cmms.repositoryImplementation;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.stie.cmms.domain.ChecklistFaultDescription;
import com.stie.cmms.domain.ChecklistHeader;
import com.stie.cmms.domain.ChecklistProperty;
import com.stie.cmms.domain.ChecklistPropertyTitle;
import com.stie.cmms.domain.ChecklistSearchModel;
import com.stie.cmms.domain.TaskChecklist;
import com.stie.cmms.domain.Workspace;
import com.stie.cmms.repositoryInterfaces.CheckListDaoInterface;
@Repository
@Transactional
public class CheckListDaoImpl implements CheckListDaoInterface {
private EntityManager entitymanager;
public EntityManager getEntitymanager() {
return entitymanager;
}
@PersistenceContext
public void setEntitymanager(EntityManager entitymanager) {
this.entitymanager = entitymanager;
}
@Override
public List<ChecklistHeader> listAll() {
TypedQuery<ChecklistHeader> d = (TypedQuery<ChecklistHeader>) entitymanager
.createQuery("Select l from ChecklistHeader l");
return d.getResultList();
}
@Override
public ChecklistHeader save(ChecklistHeader b) {
entitymanager.persist(b);
return b;
}
@Override
public boolean remove(long id) throws Exception {
// entitymanager.remove(entitymanager.find(ChecklistHeader.class, id));
TypedQuery<ChecklistHeader> d = (TypedQuery<ChecklistHeader>) entitymanager
.createQuery("DELETE FROM ChecklistHeader c WHERE c.id=:id");
d.setParameter("id", id);
d.executeUpdate();
return true;
}
@Override
public ChecklistHeader find(long id) {
return entitymanager.find(ChecklistHeader.class, id);
}
@Override
public ChecklistHeader update(ChecklistHeader b) {
return entitymanager.merge(b);
}
@Override
public long count(Workspace w) {
// TODO Auto-generated method stub
TypedQuery d = (TypedQuery) entitymanager
.createQuery("Select count(l) from ChecklistHeader l where l.workspace=:w");
d.setParameter("w", w);
return (Long) d.getResultList().get(0);
}
@Override
public List<ChecklistHeader> getCostPage(int from, int to, Workspace w) {
// TODO Auto-generated method stub
TypedQuery<ChecklistHeader> d = (TypedQuery<ChecklistHeader>) entitymanager
.createQuery("Select c from ChecklistHeader c where c.workspace=:w");
d.setParameter("w", w);
return d.setFirstResult(from).setMaxResults(to).getResultList();
}
@Override
public List<ChecklistHeader> listAllWorkspaceRelated(Workspace w) {
TypedQuery<ChecklistHeader> d = (TypedQuery<ChecklistHeader>) entitymanager
.createQuery("Select c from ChecklistHeader c where c.workspace=:w");
d.setParameter("w", w);
// System.out.println("cost centre list size with proj id "+w.getId()+" is "+d.getResultList().size());
return d.getResultList();
}
@Override
public ChecklistHeader getByName(String name) {
// TODO Auto-generated method stub
TypedQuery<ChecklistHeader> d = (TypedQuery<ChecklistHeader>) entitymanager
.createQuery("Select c from ChecklistHeader c where c.checklistName=:name");
d.setParameter("name", name);
ArrayList<ChecklistHeader> checkList = (ArrayList<ChecklistHeader>) d
.getResultList();
// System.out.println("total records" + checkList.size());
if (checkList.isEmpty())
return null;
else
return checkList.get(0);
}
@Override
public void update(TaskChecklist checklist) {
entitymanager.merge(checklist);
}
@Override
public List<ChecklistHeader> listAll(Long id) {
TypedQuery<ChecklistHeader> d = (TypedQuery<ChecklistHeader>) entitymanager
.createQuery("Select c from ChecklistHeader c where c.equipment.id=:id");
d.setParameter("id", id);
return d.getResultList();
}
@Override
public void remove(ChecklistProperty cp) {
entitymanager.remove(cp);
}
@Override
public void removeChecklistProperties(ChecklistHeader u) {
//removingVal(u);
try {
System.out.println("id is"+ u.getId());
Query q = entitymanager
.createQuery(
"DELETE FROM ChecklistPropertyTitle c WHERE c.checklistHeader.id=:id");
q.setParameter("id", u.getId());
//q.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Long count(ChecklistSearchModel cSearchModel) {
TypedQuery<ChecklistHeader> query = createSearchCriteriaQuery(cSearchModel);
return (long) query.getResultList().size();
}
private TypedQuery<ChecklistHeader> createSearchCriteriaQuery(
ChecklistSearchModel cSearchModel) {
CriteriaBuilder cb = entitymanager.getCriteriaBuilder();
CriteriaQuery<ChecklistHeader> cq = cb
.createQuery(ChecklistHeader.class);
Root<ChecklistHeader> fromCheck = cq.from(ChecklistHeader.class);
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(cb.equal(
fromCheck.<Workspace> get("equipment").get("workspace"),
cSearchModel.getWorkspace()));
if (cSearchModel.getChecklistName() != null)
if (!cSearchModel.getChecklistName().isEmpty()) {
predicates.add(cb.like((Expression<String>) fromCheck
.<String> get("checklistName"),
"%" + cSearchModel.getChecklistName() + "%"));
}
if (cSearchModel.getBuilding() != null)
predicates.add(cb.equal(fromCheck.get("equipment").get("building")
.get("id"), cSearchModel.getBuilding().getId()));
if (cSearchModel.getLocation() != null)
predicates.add(cb.equal(fromCheck.get("equipment").get("location")
.get("id"), cSearchModel.getLocation().getId()));
if (cSearchModel.getSystem() != null
&& (!cSearchModel.getSystem().equals(""))) {
predicates.add(cb.equal(
fromCheck.get("equipment").get("equipmentType"),
cSearchModel.getSystem()));
}
if (cSearchModel.getSubSystem() != null) {
predicates.add(cb.equal(fromCheck.get("equipment").get("subtype")
.get("id"), cSearchModel.getSubSystem().getId()));
}
if (cSearchModel.getEquipment() != null)
predicates.add(cb.equal(fromCheck.get("equipment").get("id"),
cSearchModel.getEquipment().getId()));
cq.select(fromCheck).where(predicates.toArray(new Predicate[] {}));
return entitymanager.createQuery(cq);
}
@Override
public List<ChecklistHeader> getPage(ChecklistSearchModel cSearchModel,
Integer from, Integer rows) {
TypedQuery<ChecklistHeader> query = createSearchCriteriaQuery(cSearchModel);
return query.setFirstResult(from).setMaxResults(rows).getResultList();
}
@Override
public boolean isAssigned(ChecklistHeader checklist) {
TypedQuery<Long> d = (TypedQuery<Long>) entitymanager
.createQuery("Select count(c) from TaskChecklist c where c.checklistProperty.checklistPropertyTitle.checklistHeader.id=:id");
d.setParameter("id", checklist.getId());
if (d.getSingleResult() < 1)
return false;
else
return true;
}
@Override
public List<ChecklistPropertyTitle> findPropertyTitles(
ChecklistHeader chklistHeader) {
TypedQuery<ChecklistPropertyTitle> d = (TypedQuery<ChecklistPropertyTitle>) entitymanager
.createQuery("Select l from ChecklistPropertyTitle l where l.checklistHeader.id=:id");
d.setParameter("id", chklistHeader.getId());
return d.getResultList();
}
@Override
public List<ChecklistFaultDescription> findFaultDescription(
ChecklistHeader chklistHeader) {
// TODO Auto-generated method stub
TypedQuery<ChecklistFaultDescription> d = (TypedQuery<ChecklistFaultDescription>) entitymanager
.createQuery("Select l from ChecklistFaultDescription l where l.checklistHeader.id=:id");
d.setParameter("id", chklistHeader.getId());
return d.getResultList();
}
@Override
public boolean isAssignedF(ChecklistHeader checklist) {
// TODO Auto-generated method stub
TypedQuery<Long> d = (TypedQuery<Long>) entitymanager
.createQuery("Select count(c) from TaskChecklist c where c.checklistfaults.checklistFaultTitle.checklistHeader.id=:id");
d.setParameter("id", checklist.getId());
if (d.getSingleResult() < 1)
return false;
else
return true;
}
public void removingVal(ChecklistHeader u)
{
Query q = entitymanager
.createQuery(
"DELETE FROM ChecklistFaultDescription c WHERE c.checklistHeader.id=:id");
q.setParameter("id", u.getId());
q.executeUpdate();
}
@Override
public void delete(Class clazz, Long id) {
// TODO Auto-generated method stub
Query q = entitymanager
.createQuery(
"DELETE FROM " + clazz.getName() + " c WHERE c.checklistHeader.id=:id");
q.setParameter("id", id);
q.executeUpdate();
}
}