我正在将项目从 Spring 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 会记录资格收集的审计映射:
<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>
这不是我期望或想要的。我需要列 customData、date 和 type.,而不是 setordinal
列我阅读了两次文档,用谷歌搜索,搜索了这个站点,并解决了休眠问题,但找不到任何解决方案。我尝试添加 @Colums 或 @AttributeOverride 注释,但都没有成功。
如何告诉 Envers 在审计表中生成与收集表中相同的列?
当然,它适用于 Spring Boot 2.7.x 和旧的 @Type(Def) 注释和实现。