Hibernate envers获取修订日志

时间:2016-03-01 11:11:54

标签: java spring hibernate java-ee

您好我试着找到我的问题的答案,但第二天没有成功:)所以我决定在这里写我的帖子。 所以我使用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所有属性都为null,而且几天前当我使用相同的函数时,所有道具都是来自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可能是因为它在审核表中查找了这样的行,但是需要从公共模式中获取它。 如果有人可以帮助我,我会节省更多的时间:)。 感谢

1 个答案:

答案 0 :(得分:1)

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