简体   繁体   English

Hibernate“资源类型Session没有实现java.lang.AutoCloseable”

[英]Hibernate “The resource type Session does not implement java.lang.AutoCloseable”

I want to use construction 我想用建筑

import org.hibernate.Session;
...
try (Session session){

}

How can I do that? 我怎样才能做到这一点? Because "The resource type Session does not implement java.lang.AutoCloseable" 因为“资源类型Session没有实现java.lang.AutoCloseable”

I know, that I need to extend Session and override AutoCloseable method, but when I've try to do that, there is error "The type Session cannot be the superclass of SessionDAO; a superclass must be a class" 我知道,我需要扩展Session并覆盖AutoCloseable方法,但是当我尝试这样做时,会出现错误“类型Session不能是SessionDAO的超类;超类必须是一个类”

Update 更新

I've wrote my own DAO framework, but will be use Spring for that 我已经编写了自己的DAO框架,但是会使用Spring

First, you should use a much more solid session/transaction handling infrastructure, like Spring offers you. 首先,您应该使用更加可靠的会话/事务处理基础架构,就像Spring为您提供的那样。 This way you can use the Same Session across multiple DAO calls and the transaction boundary is explicitly set by the @Transactional annotation. 这样,您可以跨多个DAO调用使用相同会话,并且事务边界由@Transactional注释显式设置。

If this is for a test project of yours, you can use a simple utility like this one: 如果这是您的测试项目,您可以使用像这样的简单实用程序

protected <T> T doInTransaction(TransactionCallable<T> callable) {
    T result = null;
    Session session = null;
    Transaction txn = null;
    try {
        session = sf.openSession();
        txn = session.beginTransaction();

        result = callable.execute(session);
        txn.commit();
    } catch (RuntimeException e) {
        if ( txn != null && txn.isActive() ) txn.rollback();
        throw e;
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return result;
}

And you can call it like this: 你可以这样称呼它:

final Long parentId = doInTransaction(new TransactionCallable<Long>() {
        @Override
        public Long execute(Session session) {
            Parent parent = new Parent();
            Child son = new Child("Bob");
            Child daughter = new Child("Alice");
            parent.addChild(son);
            parent.addChild(daughter);
            session.persist(parent);
            session.flush();
            return parent.getId();
        }
});

Check this GitHub repository for more examples like this one. 检查此GitHub存储库以获取更多此类示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java try-catch 连接中的错误“资源类型连接未实现 java.lang.AutoCloseable” - Java error "The resource type Connection does not implement java.lang.AutoCloseable" in the try-catch connection Neo4j 3.5中的事务未实现java.lang.AutoCloseable - Transaction in Neo4j 3.5 does not implement java.lang.AutoCloseable 为什么JMSProducer接口不扩展java.lang.Autocloseable? - Why JMSProducer interface does not extend java.lang.Autocloseable? 幂等方法是什么意思,调用java.lang.AutoCloseable的close方法有什么副作用? - What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable? 为什么在Java 1.7中添加了java.lang.AutoCloseable接口 - Why java.lang.AutoCloseable interface is added in Java 1.7 找不到java.lang.AutoCloseable的类文件-ActiveMQ代码 - class file for java.lang.AutoCloseable not found - ActiveMQ code 如何修复此错误:java.lang.NoSuchMethodError: 'java.lang.AutoCloseable org.mockito.MockitoAnnotations.openMocks(java.lang.Object)' - How to fix this error: java.lang.NoSuchMethodError: 'java.lang.AutoCloseable org.mockito.MockitoAnnotations.openMocks(java.lang.Object)' 为什么java.lang.AutoCloseable的close方法会抛出异常,但java.io.Closeable的close方法会抛出IOException? - Why close method of java.lang.AutoCloseable throws Exception, but close method of java.io.Closeable throws IOException? 如何为建筑上开放的资源实施自动关闭 - How to implement Autocloseable for resource opened on construction 为什么ExecutorService接口没有实现AutoCloseable? - Why does the ExecutorService interface not implement AutoCloseable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM