您好,我试图找到我的问题的答案,但是第二天没有成功:),所以我决定在这里写我的帖子。
因此,我使用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具有Im所做的所有更改,但是当我首先获得结果时,我的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>




最后我的envers设置来自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>


当我尝试获取((PenaltyProtocolItem)nextObj[0]).getRepatriateVehicle().getMake()时出现错误org.hibernate.ObjectNotFoundException: No row with the given identifier exists可能是因为它在审计表中查找了此类行,但需要从公共架构中获取它。
如果有人可以帮助我,我会节省更多时间:)。
谢谢

最佳答案

我不敢相信我找到了。因此,让我们节省几个小时的搜索工作。我的问题是:注释。所以我有一个实体
PenaltyProtocolItem具有一些不需要审核的属性,但是当我用另一个替换该实体时,我需要保存该实体的不同id,因此我需要@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)而不是@Audit,因为@Audit想要我注释我的PenaltyProtocolItem的所有实体道具。对于RepatriateVehicle,它的需求@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)也是如此。我希望这对我有所帮助,因为我真的在2天左右的时间里找不到它。但这是我首次接触技术,所以可能是正常的:)。

07-24 09:19