Hibernate 6 Envers:如何自定义@CompositeType的@ElementCollection的审计表?

t0ybt7op  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(121)

我正在将一个项目从Sping Boot 2.7.x迁移到3.0.x。我的一个实体有一个复合类型的集合:

@Entity
@Audited
public class Specialist {

    @Id
    @GeneratedValue
    private Long id;

    @ElementCollection
    @CompositeType(QualificationConverter.class)
    private Set<Qualification> qualifications;

    // ... 
}

public class Qualification implements Serializable {

    private LocalDate date = LocalDate.now();
    private String customData;

    // ...
}

public class Expert extends Qualification {
    // ...
}

public class Professional extends Qualification {
    // ...
}

转换器类(不相关部分省略):

public class QualificationConverter implements CompositeUserType<Qualification> {

    @Override
    public Object getPropertyValue(Qualification qualification, int i) throws HibernateException {
        return switch (i) {
            case 0 -> qualification.getCustomData();
            case 1 -> qualification.getDate();
            case 2 -> qualification.getClass().getName();
            default -> null;
        };
    }

    @Override
    public Qualification instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactoryImplementor) {
        String type = valueAccess.getValue(2, String.class);
        try {
            QualificationBuilder builder = (QualificationBuilder) Class.forName(type).getMethod("builder").invoke(null);
            return builder.customData(valueAccess.getValue(0, String.class))
                    .date(valueAccess.getValue(1, LocalDate.class))
                    .build();
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException ex) {
            throw new HibernateException(ex);
        }
    }

    @Override
    public Class<?> embeddable() {
        return QualificationMapper.class;
    }

    @Override
    public Class<Qualification> returnedClass() {
        return Qualification.class;
    }

    // ...

    public static class QualificationMapper {

        String customData;
        LocalDate date;
        String type;

    }
}

当我启动应用程序时,Hibernate Envers记录资格收集的审计Map:

<hibernate-mapping xmlns="http://www.hibernate.org/xsd/orm/hbm">
    <class table="specialist_qualifications_AUD" entity-name="specialist_qualifications_AUD">
        <composite-id name="originalId">
            <key-many-to-one class="org.hibernate.envers.DefaultRevisionEntity" name="REV">
                <column name="REV"/>
            </key-many-to-one>
            <key-property name="REVTYPE" type="org.hibernate.envers.internal.entities.RevisionTypeType"/>
            <key-property name="Specialist_id">
                <column name="specialist_id"/>
                <type name="long">
                    <param name="org.hibernate.type.ParameterType.primaryKey">false</param>
                    <param name="org.hibernate.type.ParameterType.dynamic">true</param>
                    <param name="org.hibernate.type.ParameterType.returnedClass">java.lang.Long</param>
                    <param name="org.hibernate.type.ParameterType.accessType">field</param>
                    <param name="org.hibernate.type.ParameterType.entityClass">com.example.hibernate6enverscompositeusertype.domain.Specialist</param>
                    <param name="org.hibernate.type.ParameterType.propertyName">id</param>
                </type>
            </key-property>
            <key-property name="SETORDINAL" type="integer">
                <column name="SETORDINAL"/>
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

这不是我所期望的,也不是我想要的。我需要列 customDatadatetype,而不是 setordinal 列。
我读了两遍文档,谷歌,搜索这个网站,和休眠问题,但找不到任何解决方案。我尝试添加@Colums或@ AttributeEnotypes注解,但都没有成功。
我如何告诉Envers在审计表中生成与在收集表中相同的列?
当然,它可以与Sping Boot 2.7.x和旧的@Type(Def)注解和实现一起工作。

5cg8jx4n

5cg8jx4n1#

在我看来,这是一个bug,所以请在问题跟踪器(https://hibernate.atlassian.net)中创建一个问题,并使用一个测试用例(https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)重现该问题。不要忘记报告您创建的问题;)

相关问题