hibernate 已关闭逻辑连接管理执行程序休眠

sr4lhrrt  于 2022-11-24  发布在  其他
关注(0)|答案(3)|浏览(191)

我在JAVA上使用H2数据库和Hibernate,我得到了一个奇怪的错误。我已经创建了我的抽象仓库来管理基本的CRUD操作。
我得到的例外是这样的:

java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@d20d74a is closed
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java:37)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:135)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:254)
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.rollback(AbstractLogicalConnectionImplementor.java:116)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.rollback(JdbcResourceLocalTransactionCoordinatorImpl.java:294)
at org.hibernate.engine.transaction.internal.TransactionImpl.rollback(TransactionImpl.java:139)
at repositories.AbstractRepository.save(AbstractRepository.java:32)
at services.ResultService.saveResult(ResultService.java:76)
at services.API.WebRequestService.run(WebRequestService.java:124)
at services.API.ThreadService.run(ThreadService.java:67)

AbstractRepository保存方法:

public <T> T save(T t) {
    Transaction transaction = null;
    try (Session session = HibernateConfig.getSessionFactory().openSession()) {
        transaction = session.beginTransaction();
        Serializable entityId = session.save(t);
        transaction.commit();

        T createdEntity = (T) session.get(t.getClass(), entityId);
        return createdEntity;

    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
        }
        e.printStackTrace();
    }
    return null;
}

我是一个CS的学生,我对Hibernate不是很熟悉。我在我的计算机上没有得到这个错误,只有在其他计算机上构建了JAR文件。
另外,英语不是我的主要语言,所以如果你听不清楚我的话,我很抱歉!

pkbketx9

pkbketx91#

经过几个小时的调试,我发现了这个错误!错误是一个列超过了长度,异常来自catch块。catch块试图回滚它的连接已经关闭的东西。我希望这对某些人有帮助!

vom3gejh

vom3gejh2#

当我尝试使用Hibernate版本5.5.3在两个表之间创建一个嵌入关系时,我遇到了同样的错误。是的,上面的回答有助于我一次性调试错误。感谢@William。我的情况也是如此。由于Embeddable类中发生异常,catch块试图回滚事务。问题是我在Embeddable类中没有默认构造函数。
谢谢你!

z3yyvxxp

z3yyvxxp3#

@Vikarm的回答和@William的回答为我指出了正确的方向。您只需要 * 在具有EmbeddedId的类的构造函数中创建您的复合ID *。
下面是它在代码中的样子(出于完整性的目的,包含了完整的示例)

@Entity(name = "artwork_rating")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class ArtworkRating {
    @EmbeddedId
    private ArtworkRatingKey id; // This needs to be instantiated*

    private int score;

    private String content;

    // --------- Relations --------- //
    @ManyToOne
    @MapsId("userId")
    @JoinColumn(name = "user_id")
    User user;

    @ManyToOne
    @MapsId("artworkId")
    @JoinColumn(name = "artwork_id")
    Artwork artwork;

    // --------- Constructors --------- //
    public ArtworkRating(int score, String content, User user, Artwork artwork) {
        this.id = new ArtworkRatingKey(user.getId(), artwork.getId()); // *as shown here
        this.score = score;
        this.content = content;
        this.user = user;
        this.artwork = artwork;
    }
}

相关问题