Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.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
java jpa hibernate在onetomany上回滚 我有一个JPA的模型表头,我考虑了父< /P> @Entity @Table(name = "Form_Header") public class FormHeader { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "header_id", nullable = false) private Long headerId; @OneToMany( mappedBy = "formHeader", cascade = CascadeType.ALL, orphanRemoval = true ) private List<FormProdItem> formProdItem; -- more fields-- -- getter and setter-- }_Java_Spring Boot_Hibernate_Jpa - Fatal编程技术网

java jpa hibernate在onetomany上回滚 我有一个JPA的模型表头,我考虑了父< /P> @Entity @Table(name = "Form_Header") public class FormHeader { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "header_id", nullable = false) private Long headerId; @OneToMany( mappedBy = "formHeader", cascade = CascadeType.ALL, orphanRemoval = true ) private List<FormProdItem> formProdItem; -- more fields-- -- getter and setter-- }

java jpa hibernate在onetomany上回滚 我有一个JPA的模型表头,我考虑了父< /P> @Entity @Table(name = "Form_Header") public class FormHeader { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "header_id", nullable = false) private Long headerId; @OneToMany( mappedBy = "formHeader", cascade = CascadeType.ALL, orphanRemoval = true ) private List<FormProdItem> formProdItem; -- more fields-- -- getter and setter-- },java,spring-boot,hibernate,jpa,Java,Spring Boot,Hibernate,Jpa,} 和我的DAO来访问数据库 @Repository @Transactional public class FormHeaderDAO { @Autowired private EntityManager entityManager; public List<FormHeader> gettest() { try { String sql = " SELECT e FROM F

}

和我的DAO来访问数据库

@Repository
@Transactional
public class FormHeaderDAO {

    @Autowired
    private EntityManager entityManager;

    public List<FormHeader> gettest() {
    
        try {
        
            String sql = " SELECT e FROM FromHeader e "
            + " WHERE  e.isDeleted = 0 ";
        
            Query query = entityManager.createQuery(sql, FormHeader.class);
        
        
            return (List<FormHeader>) query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}

即使我添加了sql“join e.formProdIem f”,错误也是一样的,它也不会提供更多的细节。知道为什么吗?

我猜您得到的是一个非查询结果异常,因为您的查询返回多个结果,您调用了query.getSingleResult(),但您没有看到,因为您正在捕获异常并悄悄地丢弃它们。这真是个坏主意

@Slf4j
@Repository
@Transactional
public class FormHeaderDAO {

    @Autowired
    private EntityManager entityManager;

    public List<FormHeader> gettest() {
    
        try {
            String sql = " SELECT e FROM FromHeader e "
            + " WHERE  e.isDeleted = 0 ";
        
            Query query = entityManager.createQuery(sql, FormHeader.class);
        
            return (List<FormHeader>) query.getSingleResult();
        } catch (NoResultException e) {
            log.error(e)
            return null;
        } catch (NonUniqueResultException e) {
            log.error(e)
            return null;
        } catch (Exception e) {
            log.error(e)
            return null;
        }
    }
}
@Slf4j
@存储库
@交易的
公营班主任二道{
@自动连线
私人实体管理者实体管理者;
公共列表gettest(){
试一试{
String sql=“从标题e中选择e”
+“其中e.isDeleted=0”;
Query Query=entityManager.createQuery(sql,FormHeader.class);
return(List)query.getSingleResult();
}捕获(noresulte异常){
日志错误(e)
返回null;
}捕获(非查询结果异常){
日志错误(e)
返回null;
}捕获(例外e){
日志错误(e)
返回null;
}
}
}
如果您没有Slf4j,请使用System.err.println()或断点来确认您的代码现在正在接受哪种类型的异常

2020-09-16 09:19:10.324 ERROR 9624 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only] with root cause

org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only
@Slf4j
@Repository
@Transactional
public class FormHeaderDAO {

    @Autowired
    private EntityManager entityManager;

    public List<FormHeader> gettest() {
    
        try {
            String sql = " SELECT e FROM FromHeader e "
            + " WHERE  e.isDeleted = 0 ";
        
            Query query = entityManager.createQuery(sql, FormHeader.class);
        
            return (List<FormHeader>) query.getSingleResult();
        } catch (NoResultException e) {
            log.error(e)
            return null;
        } catch (NonUniqueResultException e) {
            log.error(e)
            return null;
        } catch (Exception e) {
            log.error(e)
            return null;
        }
    }
}