java - Hibernate envers 获取修订日志

标签 java spring hibernate jakarta-ee

您好,我尝试找到问题的答案,但第二天没有成功:)所以我决定在这里写我的帖子。 因此,我使用 Hibernate envers 并通过其 ID 获取当前实体的所有修订。 然后我想获取该实体的所有已更改的 Prop ,并使用信息创建新实体,其中包含更改内容、更改内容、更改日期、旧值、新值的信息。 几天前,我的项目工作正常,之后就不行了:)。 所以这是图片: 这是我返回结果的函数。

@Override
public List<List<Object[]>> getAuditForPelaltyProtocolItem(int id) {
    List<List<Object[]>> auditResultList = new ArrayList<>();
    AuditReader reader = AuditReaderFactory.get(getSession());
    AuditQuery query = reader.createQuery().forRevisionsOfEntity(PenaltyProtocolItem.class, false, true)
            .add(AuditEntity.id().eq(id)).
            add(AuditEntity.property("changed").eq(false)); 
    auditResultList.add(query.getResultList());

    AuditQuery queryTwo = reader.createQuery().forRevisionsOfEntity(PenaltyProtocolItem.class, false, true)
            .add(AuditEntity.id().eq(id)).
            add(AuditEntity.property("changed").eq(true));
    auditResultList.add(queryTwo.getResultList());
    return auditResultList;

}

这是我的 PenaltyProtocolItem 类:

@Audited
public class PenaltyProtocolItem implements Serializable, Comparable<PenaltyProtocolItem>{

private int version;

private static final long serialVersionUID = 2271491886865089185L;

private int id = -1;
private PenaltyProtocol protocol = null;
private RepatriateVehicle repatriateVehicle = null;
private Parking parking = null;
private Violation violation = null;
private PenaltyVehicleTax tax = null;
private Employee createdBy = null;
private Employee modifiedBy = null;
private String remark = null;
private Time rowTime = null;
private String street = null;
private ParkingZoneTypes zone = null;
private Timestamp modifiedTime = null;
private boolean isChanged = false;
}

我的另一个带有 envers 注释的类

@Audited
public class RepatriateVehicle implements Serializable{

private int version;

private static final long serialVersionUID = 84210604965295166L;

private int id = -1;
private VehicleType vehicleType = null;
private VehicleStatus vehicleStatus = null;
private Employee createdBy = null;
private Employee modifiedBy = null;
private String regNum = null;
private VehicleMake make = null;
private String model = null;
private String color = null;
private Timestamp modifiedTime = null;
}

我需要在我的实体(VehicleType、VehicleStatus、Employee、VehicleMake)上使用@Audited还是需要使用@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) 。 当我不更改它时,我只是将其替换为新实体,因此我不需要对此更改进行审计表。当我更改审核表中的 PenaltyProtocolItem 时,PenaltyProtocolItem 具有我所做的所有更改,但是当我首先得到结果时,我的 RepatriateVehicle 所有属性均为空,几天前,当我使用相同的函数时,所有 Prop 都使用 RepatriateVechicle 审核表中的值,但不是现在。从那时起我改变的唯一一件事是我将注释从 @Audit 更改为 @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) 但我不确定我更改了哪一个,现在我悲伤:)。 这是我的审计表的图片: enter image description here enter image description here

还有我的映射 xml:

<class name="RepatriateVehicle" table="repatriate_vehicles">
    <id name="id" type="int">
        <column name="id" />
        <generator class="sequence">
            <param name="sequence">seq_repatriate_vehicle_id</param>
        </generator>
    </id>

    <version name="version" access="field" column="orm_version"/>

    <many-to-one name="vehicleType" class="VehicleType" fetch="join"
        column="vehicle_type_code" />
    <many-to-one name="vehicleStatus" class="VehicleStatus"
        fetch="join" column="vehicle_status_code" />
    <many-to-one name="make" class="VehicleMake" fetch="join"
        column="make_id" /> 
    <many-to-one name="createdBy" class="Employee"
        fetch="join" column="created_by"/>
    <many-to-one name="modifiedBy" class="Employee"
        fetch="join" column="modified_by"/>
    <property name="regNum" type="string" column="reg_num"/>
    <property name="model" type="string" column="model"/>
    <property name="color" type="string" column="color"/>
    <property name="modifiedTime" type="timestamp" column="modified_time"/>
</class>
</hibernate-mapping>

<hibernate-mapping package="bg.infosystem.parkings.hibernate.entities"
schema="parkings">
<class name="PenaltyProtocolItem" table="penalty_protocol_items">
    <id name="id" type="int">
        <column name="id" />
        <generator class="sequence">
            <param name="sequence">seq_penalty_protocol_item_id</param>
        </generator>
    </id>

    <version name="version" access="field" column="orm_version"/>

    <many-to-one name="protocol" class="PenaltyProtocol"
        fetch="join" column="penalty_protocol_id" />
    <many-to-one name="repatriateVehicle" class="RepatriateVehicle"
        fetch="join" column="repatriate_vehicle_id" cascade="save-update"/>
    <many-to-one name="parking" class="Parking" fetch="join"
        column="parking_code" />
    <many-to-one name="violation" class="Violation" fetch="join"
        column="violation_code" />
    <property name="tax" column="tax">
        <type name="bg.infosystem.hibernate.type.EnhancedEnumType">
            <param name="enumClass">bg.infosystem.parkings.hibernate.entities.PenaltyProtocolItem$PenaltyVehicleTax</param>
            <param name="enumProperty">code</param>
            <param name="type">12</param>
        </type>
    </property>
    <many-to-one name="createdBy" class="Employee"
        fetch="join" column="created_by"/>
    <many-to-one name="modifiedBy" class="Employee"
        fetch="join" column="modified_by"/>
    <property name="remark" column="remark" type="string"/>
    <property name="rowTime" column="row_time" type="java.sql.Time"/>
    <property name="street" column="street" type="string"/>
    <property name="modifiedTime" type="timestamp" column="modified_time"/>
    <property name="zone" column="zone">
        <type name="bg.infosystem.hibernate.type.EnhancedEnumType">
            <param name="enumClass">bg.infosystem.parkings.hibernate.entities.PenaltyProtocolItem$ParkingZoneTypes</param>
            <param name="enumProperty">code</param>
            <param name="type">12</param>
        </type>
    </property>
    <property name="changed" column = "is_changed" type = "boolean"/>
</class>

最后是我来自database.xml的envers设置

<property name="hibernateProperties">
    <!-- PostgreSQLDialect -->
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>

            <prop key="hibernate.ejb.event.post-insert">org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener</prop>
            <prop key="hibernate.ejb.event.post-update">org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener</prop>
            <prop key="hibernate.ejb.event.post-delete">org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener</prop>

            <prop key="hibernate.ejb.event.pre-collection-update">org.hibernate.envers.event.AuditEventListener</prop>
            <prop key="hibernate.ejb.event.pre-collection-remove">org.hibernate.envers.event.AuditEventListener</prop>
            <prop key="hibernate.ejb.event.post-collection-recreate">org.hibernate.envers.event.AuditEventListener</prop>

            <prop key="org.hibernate.envers.default_schema">audit</prop>
            <prop key="org.hibernate.envers.audit_table_prefix">v_</prop>
            <prop key="org.hibernate.envers.audit_table_suffix"></prop>
            <prop key="org.hibernate.envers.revision_field_name">rev</prop> 

        </props>
    </property>

当我尝试获取 ((PenaltyProtocolItem)nextObj[0]).getRepatriateVehicle().getMake() 时,出现错误 org.hibernate.ObjectNotFoundException:没有给定标识符的行存在可能是因为它在审计表中查找此类行,但需要从公共(public)模式中获取它。 如果有人可以帮助我,我会节省更多的时间:)。 谢谢

最佳答案

我不敢相信我找到了它。因此,让其他人节省几个小时的搜索时间。我的问题是什么:注释。所以我有一个实体 PenaltyProtocolItem 具有一些不需要审核的属性,但当我将其替换为其他实体时,我需要保存该实体的不同 id,所以我需要 @Audited(targetAuditMode = RelationTargetAuditMode .NOT_AUDITED) 不是 @Audit 因为 @Audit 希望我注释我的 PenaltyProtocolItem 的所有实体属性。 RepatriateVehicle 也是如此,它需要 @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)。我希望这对某些人有帮助,因为我确实花了大约 2 天的时间才找到它。但这是我第一次接触 envers 技术,所以这可能是正常的:)。

关于java - Hibernate envers 获取修订日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35721346/

相关文章:

java - JDBC 删除条目 - 主键/CONCUR_UPDATABLE 问题

java - AutoConfigureMybatis注解是什么意思?

java - Bean 属性 'transactionManagerBeanName' 不可写或具有无效的设置方法

java - Hibernate criteria.list() 在神秘情况下抛出 NullPointerException

java - 在枚举中使用 compareTo

java - 在maven中使用属性文件

java - 从 JSP 访问我的 Spring session

java - 如何从 spring bean 中删除属性

java - Postgres Hibernate 设置 session 变量以实现行级安全性

sql-server - 逆向工程中的 Hibernate 工具 "Duplicate class"错误