2016-03-01 83 views
0

你好,我试图找到我的问题的答案,但没有成功的第二天:)所以我决定在这里写我的帖子。 因此,我使用Hibernate envers并通过他的Id获取当前实体的所有修订。 然后我想要获得这个实体的所有改变的道具并且创建带有信息的新实体什么是改变谁在改变改变日期oldValue newValue。 几天前,我的项目工作正常后,它不是:)。 所以这里是图片: 这是我返回结果的函数。休眠envers获得修订日志

@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,并且前几天我使用相同的函数时,所有的props都与RepatriateVechicle审计表的值相关现在。从那时起我改变了最简单的东西,我把注释从@Audit更改为@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED),但林不知道我改变了哪一个,现在我伤心:)。 我的审计表的下面图片: enter image description here enter image description here

我的XML映射:从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> 

<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设置当我尝试获得((PenaltyProtocolItem)nextObj[0]).getRepatriateVehicle().getMake()我收到错误org.hibernate.ObjectNotFoundException: No row with the given identifier exists可能是因为它在审计表中查找这样的行,但它需要从公共架构中获取它。 如果有人能帮助我,我会节省更多的时间:)。 谢谢

回答

0

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