Envers 审核表中缺少属性

发布于 2024-12-02 01:51:26 字数 3631 浏览 1 评论 0原文

我正在使用 envers 来审核我的 ParameterToValue 实体。它的属性“containerId”、“containerType”、“parameterId”在映射数据库表“values_for_params”(常规 Hibernate 表)中显示为列,但在 envers 生成的“values_for_params_AUD”数据库表中缺失。我需要能够获取给定的历史“值”(containerId、containerType、parameterId)。

ParameterToValue 类:

@Audited
public class ParameterToValue extends BasicValueHolder {
private Long containerId;
private ContainerType containerType;
private Long parameterId;

public ParameterToValue(ContainerID containerId, Long parameterId, Value value) {
    super(value);
    this.containerId = containerId.getContainerId();
    this.parameterId = parameterId;
    containerType = containerId.getContainerType();
}

ParameterToValue() {
}

public Long getParameterId() {
    return parameterId;
}

public void setParameterId(Long parameterId) {
    this.parameterId = parameterId;
}

public Value getValue() {
    return value;
}

public void setValue(Value value) {
    this.value = value;
}

public Long getContainerId() {
    return containerId;
}

public void setContainerId(Long containerId) {
    this.containerId = containerId;
}

public ContainerType getContainerType() {
    return containerType;
}

public void setContainerType(ContainerType containerType) {
    this.containerType = containerType;
}
}

Hibernate 映射定义:

<class name="platform.server.dataservices.model.ParameterToValue" table="values_for_params">
    <cache usage="read-write" include="all" />
    <id name="id" column="ID">
        <generator class="native"/>
    </id>
    <properties name="uniqueProps" unique="true">
        <property name="containerId" index="ParamValsContainerIdIndx"/>
        <property name="parameterId" index="ParamValsParamIdIndx"/>
        <property name="containerType" column="CONTAINER_TYPE">
            <type name="org.hibernate.type.EnumType">
                <param name="enumClass">platform.server.dataservices.model.ContainerType</param>
                <param name="type">4</param>
                <!-- 12 = string, 5 = smallint, 4 = integer, default 4 -->
            </type>
        </property>
    </properties>

    <many-to-one name="value" cascade="all" lazy="false" unique="true" index="PRM_VAL_IDX"/>
</class>

在 MySQL 中显示 CREATE TABLE values_for_params

CREATE TABLE `values_for_params` (
  ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `containerId` bigint(20) DEFAULT NULL,
  `parameterId` bigint(20) DEFAULT NULL,
  `CONTAINER_TYPE` int(11) DEFAULT NULL,
  `value` bigint(20) DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `value` (`value`),
UNIQUE KEY `containerId` (`containerId`,`parameterId`,`CONTAINER_TYPE`),
KEY `ParamValsParamIdIndx` (`parameterId`),
KEY `ParamValsContainerIdIndx` (`containerId`),
KEY `PRM_VAL_IDX` (`value`),
KEY `FKE02CB4F981565307` (`value`),
CONSTRAINT `FKE02CB4F981565307` FOREIGN KEY (`value`) REFERENCES `value` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8

SHOW CREATE TABLE MySQL 中的 values_for_params_AUD

CREATE TABLE `values_for_params_AUD` (
  `ID` bigint(20) NOT NULL,
  `REV` int(11) NOT NULL,
  `REVTYPE` tinyint(4) DEFAULT NULL,
  `value` bigint(20) DEFAULT NULL,
PRIMARY KEY (`ID`,`REV`),
KEY `FKE093BE4AEB88DFB` (`REV`),
CONSTRAINT `FKE093BE4AEB88DFB` FOREIGN KEY (`REV`) REFERENCES `DesignRevisionEntity`     (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

I am using envers to audit my ParameterToValue entity. Its properties "containerId", "containerType", "parameterId" which do appear as columns in a mapped DB table "values_for_params" (a regular Hibernate table), are missing at the envers generated "values_for_params_AUD" DB table. I need the ability to get historic "value" for given (containerId, containerType, parameterId).

The ParameterToValue class:

@Audited
public class ParameterToValue extends BasicValueHolder {
private Long containerId;
private ContainerType containerType;
private Long parameterId;

public ParameterToValue(ContainerID containerId, Long parameterId, Value value) {
    super(value);
    this.containerId = containerId.getContainerId();
    this.parameterId = parameterId;
    containerType = containerId.getContainerType();
}

ParameterToValue() {
}

public Long getParameterId() {
    return parameterId;
}

public void setParameterId(Long parameterId) {
    this.parameterId = parameterId;
}

public Value getValue() {
    return value;
}

public void setValue(Value value) {
    this.value = value;
}

public Long getContainerId() {
    return containerId;
}

public void setContainerId(Long containerId) {
    this.containerId = containerId;
}

public ContainerType getContainerType() {
    return containerType;
}

public void setContainerType(ContainerType containerType) {
    this.containerType = containerType;
}
}

The Hibernate mapping definition:

<class name="platform.server.dataservices.model.ParameterToValue" table="values_for_params">
    <cache usage="read-write" include="all" />
    <id name="id" column="ID">
        <generator class="native"/>
    </id>
    <properties name="uniqueProps" unique="true">
        <property name="containerId" index="ParamValsContainerIdIndx"/>
        <property name="parameterId" index="ParamValsParamIdIndx"/>
        <property name="containerType" column="CONTAINER_TYPE">
            <type name="org.hibernate.type.EnumType">
                <param name="enumClass">platform.server.dataservices.model.ContainerType</param>
                <param name="type">4</param>
                <!-- 12 = string, 5 = smallint, 4 = integer, default 4 -->
            </type>
        </property>
    </properties>

    <many-to-one name="value" cascade="all" lazy="false" unique="true" index="PRM_VAL_IDX"/>
</class>

SHOW CREATE TABLE values_for_params in MySQL:

CREATE TABLE `values_for_params` (
  ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `containerId` bigint(20) DEFAULT NULL,
  `parameterId` bigint(20) DEFAULT NULL,
  `CONTAINER_TYPE` int(11) DEFAULT NULL,
  `value` bigint(20) DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `value` (`value`),
UNIQUE KEY `containerId` (`containerId`,`parameterId`,`CONTAINER_TYPE`),
KEY `ParamValsParamIdIndx` (`parameterId`),
KEY `ParamValsContainerIdIndx` (`containerId`),
KEY `PRM_VAL_IDX` (`value`),
KEY `FKE02CB4F981565307` (`value`),
CONSTRAINT `FKE02CB4F981565307` FOREIGN KEY (`value`) REFERENCES `value` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8

SHOW CREATE TABLE values_for_params_AUD in MySQL:

CREATE TABLE `values_for_params_AUD` (
  `ID` bigint(20) NOT NULL,
  `REV` int(11) NOT NULL,
  `REVTYPE` tinyint(4) DEFAULT NULL,
  `value` bigint(20) DEFAULT NULL,
PRIMARY KEY (`ID`,`REV`),
KEY `FKE093BE4AEB88DFB` (`REV`),
CONSTRAINT `FKE093BE4AEB88DFB` FOREIGN KEY (`REV`) REFERENCES `DesignRevisionEntity`     (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子是时光的心 2024-12-09 01:51:26

固定的。如果有人需要答案:

问题是 envers 忽略了属性标记中写入的任何内容。当我以这种方式删除标签时:

<properties name="uniqueProps" unique="true">
    <property name="containerId" index="ParamValsContainerIdIndx"/>
    <property name="parameterId" index="ParamValsParamIdIndx"/>
    <property name="containerType" column="CONTAINER_TYPE">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">platform.server.dataservices.model.ContainerType</param>
            <param name="type">4</param>
        </type>
    </property>
</properties>

变成:

<property name="containerId" index="ParamValsContainerIdIndx"/>
<property name="parameterId" index="ParamValsParamIdIndx"/>
<property name="containerType" column="CONTAINER_TYPE">
  <type name="org.hibernate.type.EnumType">
    <param name="enumClass">platform.server.dataservices.model.ContainerType</param>
    <param name="type">4</param>
  </type>
</property>

这样,所有属性都经过审核,但我仍然需要三元组(containerId、parameterId、containerType)是唯一的。最终的解决方案是这样的:

<property name="containerId" index="ParamValsContainerIdIndx"/>
<property name="parameterId" index="ParamValsParamIdIndx"/>
<property name="containerType" column="CONTAINER_TYPE">
  <type name="org.hibernate.type.EnumType">
    <param name="enumClass">platform.server.dataservices.model.ContainerType</param>
    <param name="type">4</param>
  </type>
</property>

<properties name="uniqueProps" unique="true">
  <property name="containerId" insert="false" update="false"/>
  <property name="parameterId" insert="false" update="false"/>
  <property name="containerType" insert="false" update="false"/>
</properties>

Fixed. In case anybody needs the answer:

The problem was that envers ignored whatever was written inside the properties tag. When I removed the tag this way:

<properties name="uniqueProps" unique="true">
    <property name="containerId" index="ParamValsContainerIdIndx"/>
    <property name="parameterId" index="ParamValsParamIdIndx"/>
    <property name="containerType" column="CONTAINER_TYPE">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">platform.server.dataservices.model.ContainerType</param>
            <param name="type">4</param>
        </type>
    </property>
</properties>

Became:

<property name="containerId" index="ParamValsContainerIdIndx"/>
<property name="parameterId" index="ParamValsParamIdIndx"/>
<property name="containerType" column="CONTAINER_TYPE">
  <type name="org.hibernate.type.EnumType">
    <param name="enumClass">platform.server.dataservices.model.ContainerType</param>
    <param name="type">4</param>
  </type>
</property>

This way, all of the properties became audited, but I still needed the triplet (containerId, parameterId, containerType) to be unique. The final solution was this:

<property name="containerId" index="ParamValsContainerIdIndx"/>
<property name="parameterId" index="ParamValsParamIdIndx"/>
<property name="containerType" column="CONTAINER_TYPE">
  <type name="org.hibernate.type.EnumType">
    <param name="enumClass">platform.server.dataservices.model.ContainerType</param>
    <param name="type">4</param>
  </type>
</property>

<properties name="uniqueProps" unique="true">
  <property name="containerId" insert="false" update="false"/>
  <property name="parameterId" insert="false" update="false"/>
  <property name="containerType" insert="false" update="false"/>
</properties>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文