java 升级到Hibernate 6后,AttributeOverrides不再工作

2q5ifsrm  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(200)

我把下面的代码从Hibernate 5升级到了6.1.6,并得到了下面的异常。看起来Hibernate在遍历AttributeOverride列表时有一些问题。
主体

package com.mypackage

import java.io.Serializable;
import java.time.OffsetDateTime;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.AttributeOverrides;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table
public class MyClass implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "MY_ID", unique = true, nullable = false)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long myId;
       
    /**
     * The foreign key to the other table.
     */
    @AttributeOverrides(
    {
        @AttributeOverride(name = "column1", column = @Column(name = "column1", nullable = false)),
        @AttributeOverride(name = "column2", column = @Column(name = "column2", nullable = false))
    })
    private OtherTableId otherTableId;

组合外键

package mypackage;

import java.io.Serializable;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;

@Embeddable
public class OtherTableId implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "column1", nullable = false)
    private int column1;

    @Column(name = "column2", nullable = false)
    private int column2;

    public OtherTableId() {
        super();
    }

    public OtherTableId(int column1, int column2) {
        this.column1 = column1;
        this.column2 = column2;
    }

误差

Caused by: java.lang.IllegalStateException: PostInitCallback queue could not be processed...
        - PostInitCallbackEntry - EmbeddableMappingType(mypackage.MyClass.otherTableId.{inverse})#finishInitialization

    at org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess.executePostInitCallbacks(MappingModelCreationProcess.java:146)
    at org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess.execute(MappingModelCreationProcess.java:90)
    at org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess.process(MappingModelCreationProcess.java:39)
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.finishInitialization(MappingMetamodelImpl.java:229)
    at org.hibernate.metamodel.internal.RuntimeMetamodelsImpl.finishInitialization(RuntimeMetamodelsImpl.java:60)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:311)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:415)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1425)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
    ... 97 more
    Suppressed: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
        at org.hibernate.metamodel.mapping.internal.SelectableMappingsImpl.getSelectable(SelectableMappingsImpl.java:101)
        at org.hibernate.metamodel.mapping.internal.AbstractEmbeddableMapping.inverseInitializeCallback(AbstractEmbeddableMapping.java:153)
        at org.hibernate.metamodel.mapping.internal.EmbeddableMappingTypeImpl.lambda$new$2(EmbeddableMappingTypeImpl.java:172)
        at org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess$PostInitCallbackEntry.process(MappingModelCreationProcess.java:210)
        at org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess.executePostInitCallbacks(MappingModelCreationProcess.java:108)
        ... 107 more

我在Hibernate的迁移指南中找不到任何东西。你知道我必须适应什么才能在Hibernate 6中运行吗?

gfttwv5a

gfttwv5a1#

我刚刚在Hibernate 6.1和6.2 CR2中运行了以下测试,测试成功了:

@SessionFactory
@DomainModel(annotatedClasses = {BTest.MyClass.class, BTest.OtherTableId.class})
public class BTest {

    @Test
    void test(SessionFactoryScope scope) {
        scope.inTransaction(s -> {
            MyClass myClass = new MyClass();
            OtherTableId otherTableId = new OtherTableId();
            myClass.otherTableId = otherTableId;
            s.persist( myClass );
        });
        scope.inTransaction(s -> {
            s.createSelectionQuery("from BTest$MyClass").getSingleResult();
        });
    }

    @Entity
    @Table
    public static class MyClass implements Serializable {

        private static final long serialVersionUID = 1L;

        @Id
        @Column(name = "MY_ID", unique = true, nullable = false)
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long myId;

        /**
         * The foreign key to the other table.
         */
        @AttributeOverrides(
                {
                        @AttributeOverride(name = "column1", column = @Column(name = "column1", nullable = false)),
                        @AttributeOverride(name = "column2", column = @Column(name = "column2", nullable = false))
                })
        private OtherTableId otherTableId;
    }

    @Embeddable
    public static class OtherTableId implements Serializable {

        private static final long serialVersionUID = 1L;

        @Column(name = "column1", nullable = false)
        private int column1;

        @Column(name = "column2", nullable = false)
        private int column2;

        public OtherTableId() {
            super();
        }

        public OtherTableId(int column1, int column2) {
            this.column1 = column1;
            this.column2 = column2;
        }
    }
}

所以不管是什么导致了这个错误,一定是你没有给我们看的代码。

相关问题