简体   繁体   中英

Hibernate envers get log of revisions

Hello I try to find answer of my question but no success second day :) so I decide to write my post here. So I use Hibernate envers and get all revisions for current entity by his Id. Then I want to get all changed props of this entity and create new entity with info what is change who is make changes date of changes oldValue newValue. Few days ago my project just work fine after that it is not :). So here is the picture: This is my function that return me result.

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

}

Here is my PenaltyProtocolItem class:

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

My other class with envers annotation

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

Do I need to use @Audited on my entity (VehicleType, VehicleStatus,Employee, VehicleMake) or I need to use @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) . When I do not change it I just replace it with a new entity so I do not need audit table for this changes. When I change PenaltyProtocolItem in the audit table PenaltyProtocolItem has all changes that Im made but when I get result first my RepatriateVehicle all property are null and few days ago when I used the same function all props was with the values from the RepatriateVechicle audit table but not and now. The onlyest thing that I changed since then I change the annotation from @Audit to @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) but Im not sure which one I change and now Im sad :). Here picture of my audit table : enter image description here enter image description here

And my mapping 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>

And last my envers settings from database.xml

<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>

When I try to get ((PenaltyProtocolItem)nextObj[0]).getRepatriateVehicle().getMake() I get error org.hibernate.ObjectNotFoundException: No row with the given identifier exists may be becouse it's look in audit table for such row but it's need to get it from public schema. If some one can help me I will save some more days :). Thanks

I cant believe I found it. So lets save some other man some hours searching. What was my problem : Annotations. So I have one entity PenaltyProtocolItem that have some property which one do not need to be audited but I need to save different id-s of this entity when I replace it with other so I need @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) not @Audit because @Audit want from me to annotate all entity props of my PenaltyProtocolItem . The same is with RepatriateVehicle its need @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) . I hope this is helpful for some one becouse I realy lose around 2 days to find it. But this is my first touch to envers technology so may be it`s normal :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM