【问题标题】:Hibernate envers get log of revisionsHibernate envers 获取修订日志
【发布时间】:2016-03-01 11:11:53
【问题描述】:

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

@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;
}

我需要使用@Audited on my entity (VehicleType, VehicleStatus,Employee, VehicleMake) 还是需要使用@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)。 当我不更改它时,我只是用一个新实体替换它,所以我不需要审计表来进行这些更改。当我在审计表中更改 PenaltyProtocolItem 时,PenaltyProtocolItem 具有我所做的所有更改,但是当我首先获得结果时,我的 RepatriateVehicle 所有属性都为空,几天前,当我使用相同的函数时,所有道具都具有来自 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: No row with the given identifier exists 可能是因为它在审计表中查找此类行,但它需要从公共模式中获取。 如果有人可以帮助我,我将节省更多时间:)。 谢谢

【问题讨论】:

    标签: java spring hibernate jakarta-ee


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-05-24
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 2017-07-28
      相关资源
      最近更新 更多