Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jpa控制器和级联删除出现问题_Jpa_Foreign Keys_Cascading Deletes - Fatal编程技术网

jpa控制器和级联删除出现问题

jpa控制器和级联删除出现问题,jpa,foreign-keys,cascading-deletes,Jpa,Foreign Keys,Cascading Deletes,我有两个表,其中一个表与另一个表之间有一个简单的外键约束。 表A id:Long(主键) 表B id:Long(主键) a_id:Long(fk到表a:id) 如果我从表A中删除,我希望它从表B中级联删除,因此表A中的外键在JPA实体中建模如下 @OneToMany(cascade = CascadeType.ALL, mappedBy = "a_id", orphanRemoval = true) @CascadeOnDelete private List<B> bList; 但

我有两个表,其中一个表与另一个表之间有一个简单的外键约束。

表A
id:Long(主键)

表B
id:Long(主键)
a_id:Long(fk到表a:id)

如果我从表A中删除,我希望它从表B中级联删除,因此表A中的外键在JPA实体中建模如下

@OneToMany(cascade = CascadeType.ALL, mappedBy = "a_id", orphanRemoval = true)
@CascadeOnDelete
private List<B> bList;
但是,当使用NetBeans中的“实体类的新jpa控制器类”选项生成jpa控制器时,它会生成一个销毁方法,该方法检查孤立记录,如果存在,则无法销毁

        List<String> illegalOrphanMessages = null;
        List<B> bListOrphanCheck = a.getBList();
        for (B bListOrphanCheckB : bListOrphanCheck) {
            if (illegalOrphanMessages == null) {
                illegalOrphanMessages = new ArrayList<String>();
            }
            illegalOrphanMessages.add("This A (" + a + ") cannot be destroyed since the B " + bListOrphanCheckB + " in its bList field has a non-nullable a_id field.");
        }
列表非法消息=null;
List bListOrphanCheck=a.getBList();
对于(B bListOrphanCheck B:bListOrphanCheck){
if(illegalOrphanMessages==null){
illegalOrphanMessages=新ArrayList();
}
添加(“此A(“+A+”)无法销毁,因为其bList字段中的B“+bListOrphanCheckB+”具有不可为空的A_id字段。”);
}
当我使用jpa控制器销毁方法删除A时,我不能级联删除B,我在建模中犯了什么错误

谢谢……

(这不是答案,这是对karelss的回复,但太长了,无法发表评论)

这是控制器代码,表B中的“a_id”列在创建控制器时被编译为“aId”,但除此之外,没有其他更改

package test.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import uk.co.utel.dataaccess.entities.B;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import uk.co.utel.dataaccess.controllers.exceptions.IllegalOrphanException;
import uk.co.utel.dataaccess.controllers.exceptions.NonexistentEntityException;
import uk.co.utel.dataaccess.entities.A;

public class AJpaController implements Serializable {

    public AJpaController(EntityManagerFactory emf) {
    this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
    return emf.createEntityManager();
    }

    public void create(A a) {
    if (a.getBList() == null) {
        a.setBList(new ArrayList<B>());
    }

    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();

        List<B> attachedBList = new ArrayList<B>();
        for (B bListBToAttach : a.getBList()) {
        bListBToAttach = em.getReference(bListBToAttach.getClass(), bListBToAttach.getId());
        attachedBList.add(bListBToAttach);
        }
        a.setBList(attachedBList);

        for (B bListB : a.getBList()) {
        A oldAIdOfBListB = bListB.getAId();
        bListB.setAId(a);
        bListB = em.merge(bListB);
        if (oldAIdOfBListB != null) {
            oldAIdOfBListB.getBList().remove(bListB);
            oldAIdOfBListB = em.merge(oldAIdOfBListB);
        }
        }

        em.getTransaction().commit();
    } finally {
        if (em != null) {
        em.close();
        }
    }
    }

    public void edit(A a) throws IllegalOrphanException, NonexistentEntityException, Exception {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        A persistentA = em.find(A.class, a.getId());
        List<B> bListOld = persistentA.getBList();
        List<B> bListNew = a.getBList();

        List<String> illegalOrphanMessages = null;
        for (B bListOldB : bListOld) {
        if (!bListNew.contains(bListOldB)) {
            if (illegalOrphanMessages == null) {
            illegalOrphanMessages = new ArrayList<String>();
            }
            illegalOrphanMessages.add("You must retain B " + bListOldB + " since its aId field is not nullable.");
        }
        }

        if (illegalOrphanMessages != null) {
        throw new IllegalOrphanException(illegalOrphanMessages);
        }

        List<B> attachedBListNew = new ArrayList<B>();
        for (B bListNewBToAttach : bListNew) {
        bListNewBToAttach = em.getReference(bListNewBToAttach.getClass(), bListNewBToAttach.getId());
        attachedBListNew.add(bListNewBToAttach);
        }
        bListNew = attachedBListNew;
        a.setBList(bListNew);

        for (B bListNewB : bListNew) {
        if (!bListOld.contains(bListNewB)) {
            A oldAIdOfBListNewB = bListNewB.getAId();
            bListNewB.setAId(a);
            bListNewB = em.merge(bListNewB);
            if (oldAIdOfBListNewB != null && !oldAIdOfBListNewB.equals(a)) {
            oldAIdOfBListNewB.getBList().remove(bListNewB);
            oldAIdOfBListNewB = em.merge(oldAIdOfBListNewB);
            }
        }
        }

        em.getTransaction().commit();
    } catch (Exception ex) {
        String msg = ex.getLocalizedMessage();
        if (msg == null || msg.length() == 0) {
        Long id = a.getId();
        if (findA(id) == null) {
            throw new NonexistentEntityException("The a with id " + id + " no longer exists.");
        }
        }
        throw ex;
    } finally {
        if (em != null) {
        em.close();
        }
    }
    }

    public void destroy(Long id) throws IllegalOrphanException, NonexistentEntityException {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        A a;
        try {
        a = em.getReference(A.class, id);
        a.getId();
        } catch (EntityNotFoundException enfe) {
        throw new NonexistentEntityException("The a with id " + id + " no longer exists.", enfe);
        }
        List<String> illegalOrphanMessages = null;
        List<B> bListOrphanCheck = a.getBList();
        for (B bListOrphanCheckB : bListOrphanCheck) {
        if (illegalOrphanMessages == null) {
            illegalOrphanMessages = new ArrayList<String>();
        }
        illegalOrphanMessages.add("This A (" + a + ") cannot be destroyed since the B " + bListOrphanCheckB + " in its bList field has a non-nullable aId field.");
        }

        em.remove(a);
        em.getTransaction().commit();
    } finally {
        if (em != null) {
        em.close();
        }
    }
    }

    public List<A> findAEntities() {
    return findAEntities(true, -1, -1);
    }

    public List<A> findAEntities(int maxResults, int firstResult) {
    return findAEntities(false, maxResults, firstResult);
    }

    private List<A> findAEntities(boolean all, int maxResults, int firstResult) {
    EntityManager em = getEntityManager();
    try {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(A.class));
        Query q = em.createQuery(cq);
        if (!all) {
        q.setMaxResults(maxResults);
        q.setFirstResult(firstResult);
        }
        return q.getResultList();
    } finally {
        em.close();
    }
    }

    public A findA(Long id) {
    EntityManager em = getEntityManager();
    try {
        return em.find(A.class, id);
    } finally {
        em.close();
    }
    }

    public int getACount() {
    EntityManager em = getEntityManager();
    try {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<A> rt = cq.from(A.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    } finally {
        em.close();
    }
    }

}
package test.controller;
导入java.io.Serializable;
导入javax.persistence.Query;
导入javax.persistence.EntityNotFoundException;
导入javax.persistence.criteria.CriteriaQuery;
导入javax.persistence.criteria.Root;
import uk.co.utel.dataaccess.entities.B;
导入java.util.ArrayList;
导入java.util.List;
导入javax.persistence.EntityManager;
导入javax.persistence.EntityManagerFactory;
导入uk.co.utel.dataaccess.controllers.exceptions.IllegalOrphaneException;
导入uk.co.utel.dataaccess.controllers.exceptions.NonexistentEntityException;
import uk.co.utel.dataaccess.entities.A;
公共类AJpaController实现可序列化{
公共AJPAC控制器(EntityManagerFactory emf){
this.emf=emf;
}
私有EntityManagerFactory emf=null;
公共实体管理器getEntityManager(){
返回emf.createEntityManager();
}
公共作废创建(A){
如果(a.getBList()==null){
a、 setBList(新的ArrayList());
}
EntityManager em=null;
试一试{
em=getEntityManager();
em.getTransaction().begin();
List attachedBList=new ArrayList();
对于(B bListBToAttach:a.getBList()){
bListBToAttach=em.getReference(bListBToAttach.getClass(),bListBToAttach.getId());
附件列表。添加(bListBToAttach);
}
a、 挫折清单(附件);
对于(B bListB:a.getBList()){
A oldaidOfListB=bListB.getAId();
b.设置辅助(a);
bListB=em.merge(bListB);
if(oldaidOfListB!=null){
oldAIdOfBListB.getBList().remove(bListB);
OldAidOfListB=em.merge(OldAidOfListB);
}
}
em.getTransaction().commit();
}最后{
如果(em!=null){
em.close();
}
}
}
public void edit(A)抛出非法孤儿异常、不存在实体异常、异常{
EntityManager em=null;
试一试{
em=getEntityManager();
em.getTransaction().begin();
persistentA=em.find(A.class,A.getId());
List bListOld=persistentA.getBList();
List bListNew=a.getBList();
列表非法消息=null;
对于(B bListOld B:bListOld){
如果(!bListNew.contains(bListOldB)){
if(illegalOrphanMessages==null){
illegalOrphanMessages=新ArrayList();
}
添加(“您必须保留B”+bListOldB+,因为其辅助字段不可为空。”);
}
}
if(非法消息!=null){
抛出新的非法孤儿异常(非法孤儿消息);
}
List AttachedListNew=new ArrayList();
对于(B BlistNewToAttach:bListNew){
bListNewBToAttach=em.getReference(bListNewBToAttach.getClass(),bListNewBToAttach.getId());
attachedBListNew.add(blistnewbatach);
}
bListNew=attachedBListNew;
a、 挫折清单(bListNew);
对于(B bListNewB:bListNew){
如果(!bListOld.contains(bListNewB)){
OldAidOfListNewb=bListNewB.getAId();
bListNewB.setAId(a);
bListNewB=em.merge(bListNewB);
如果(OldAidOfListNewb!=null&!OldAidOfListNewb.equals(a)){
oldAIdOfBListNewB.getBList().remove(bListNewB);
OldAidOfListNewb=em.merge(OldAidOfListNewb);
}
}
}
em.getTransaction().commit();
}捕获(例外情况除外){
字符串msg=ex.getLocalizedMessage();
如果(msg==null | | msg.length()==0){
Long id=a.getId();
if(findA(id)==null){
抛出新的NoneExistentEntityException(“id为“+id+”的a不再存在”);
}
}
掷骰子;
}最后{
如果(em!=null){
em.close();
}
}
}
public void destroy(长id)引发IllegalOrphanException、NoneExistentEntityException{
EntityManager em=null;
试一试{
em=getEntityManager();
em.getTransaction().begin();
A A;
试一试{
a=em.getReference(a.class,id);
a、 getId();
}捕获(EntityNotFoundException enfe){
抛出新的不存在实体异常(“id为“+id+”的a不再存在。”,enfe);
}
列表非法消息=null;
List bListOrphanCheck=a.getBList();
对于(B bListOrphanCheck B:bListOrphanCheck){
如果(非法)消息==
package test.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import uk.co.utel.dataaccess.entities.B;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import uk.co.utel.dataaccess.controllers.exceptions.IllegalOrphanException;
import uk.co.utel.dataaccess.controllers.exceptions.NonexistentEntityException;
import uk.co.utel.dataaccess.entities.A;

public class AJpaController implements Serializable {

    public AJpaController(EntityManagerFactory emf) {
    this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
    return emf.createEntityManager();
    }

    public void create(A a) {
    if (a.getBList() == null) {
        a.setBList(new ArrayList<B>());
    }

    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();

        List<B> attachedBList = new ArrayList<B>();
        for (B bListBToAttach : a.getBList()) {
        bListBToAttach = em.getReference(bListBToAttach.getClass(), bListBToAttach.getId());
        attachedBList.add(bListBToAttach);
        }
        a.setBList(attachedBList);

        for (B bListB : a.getBList()) {
        A oldAIdOfBListB = bListB.getAId();
        bListB.setAId(a);
        bListB = em.merge(bListB);
        if (oldAIdOfBListB != null) {
            oldAIdOfBListB.getBList().remove(bListB);
            oldAIdOfBListB = em.merge(oldAIdOfBListB);
        }
        }

        em.getTransaction().commit();
    } finally {
        if (em != null) {
        em.close();
        }
    }
    }

    public void edit(A a) throws IllegalOrphanException, NonexistentEntityException, Exception {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        A persistentA = em.find(A.class, a.getId());
        List<B> bListOld = persistentA.getBList();
        List<B> bListNew = a.getBList();

        List<String> illegalOrphanMessages = null;
        for (B bListOldB : bListOld) {
        if (!bListNew.contains(bListOldB)) {
            if (illegalOrphanMessages == null) {
            illegalOrphanMessages = new ArrayList<String>();
            }
            illegalOrphanMessages.add("You must retain B " + bListOldB + " since its aId field is not nullable.");
        }
        }

        if (illegalOrphanMessages != null) {
        throw new IllegalOrphanException(illegalOrphanMessages);
        }

        List<B> attachedBListNew = new ArrayList<B>();
        for (B bListNewBToAttach : bListNew) {
        bListNewBToAttach = em.getReference(bListNewBToAttach.getClass(), bListNewBToAttach.getId());
        attachedBListNew.add(bListNewBToAttach);
        }
        bListNew = attachedBListNew;
        a.setBList(bListNew);

        for (B bListNewB : bListNew) {
        if (!bListOld.contains(bListNewB)) {
            A oldAIdOfBListNewB = bListNewB.getAId();
            bListNewB.setAId(a);
            bListNewB = em.merge(bListNewB);
            if (oldAIdOfBListNewB != null && !oldAIdOfBListNewB.equals(a)) {
            oldAIdOfBListNewB.getBList().remove(bListNewB);
            oldAIdOfBListNewB = em.merge(oldAIdOfBListNewB);
            }
        }
        }

        em.getTransaction().commit();
    } catch (Exception ex) {
        String msg = ex.getLocalizedMessage();
        if (msg == null || msg.length() == 0) {
        Long id = a.getId();
        if (findA(id) == null) {
            throw new NonexistentEntityException("The a with id " + id + " no longer exists.");
        }
        }
        throw ex;
    } finally {
        if (em != null) {
        em.close();
        }
    }
    }

    public void destroy(Long id) throws IllegalOrphanException, NonexistentEntityException {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        A a;
        try {
        a = em.getReference(A.class, id);
        a.getId();
        } catch (EntityNotFoundException enfe) {
        throw new NonexistentEntityException("The a with id " + id + " no longer exists.", enfe);
        }
        List<String> illegalOrphanMessages = null;
        List<B> bListOrphanCheck = a.getBList();
        for (B bListOrphanCheckB : bListOrphanCheck) {
        if (illegalOrphanMessages == null) {
            illegalOrphanMessages = new ArrayList<String>();
        }
        illegalOrphanMessages.add("This A (" + a + ") cannot be destroyed since the B " + bListOrphanCheckB + " in its bList field has a non-nullable aId field.");
        }

        em.remove(a);
        em.getTransaction().commit();
    } finally {
        if (em != null) {
        em.close();
        }
    }
    }

    public List<A> findAEntities() {
    return findAEntities(true, -1, -1);
    }

    public List<A> findAEntities(int maxResults, int firstResult) {
    return findAEntities(false, maxResults, firstResult);
    }

    private List<A> findAEntities(boolean all, int maxResults, int firstResult) {
    EntityManager em = getEntityManager();
    try {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(A.class));
        Query q = em.createQuery(cq);
        if (!all) {
        q.setMaxResults(maxResults);
        q.setFirstResult(firstResult);
        }
        return q.getResultList();
    } finally {
        em.close();
    }
    }

    public A findA(Long id) {
    EntityManager em = getEntityManager();
    try {
        return em.find(A.class, id);
    } finally {
        em.close();
    }
    }

    public int getACount() {
    EntityManager em = getEntityManager();
    try {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<A> rt = cq.from(A.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    } finally {
        em.close();
    }
    }

}