如何配置Hibernate Envers以避免在实体修订查询中使用某些集合(联接表)

问题描述 投票:2回答:1

我正在使用Hibernate Envers 4.2,我想仅从实体中某些集合的详细信息中获取实体的修订版。

但是我可以看到为实体中的所有集合触发了休眠查询,从而导致性能问题。

[也不能在集合中使用@NotAudited注释以避免在此特定查询中使用,因为在其他情况下需要对那些集合进行审核。

例如,如果我的受审核实体将这两个联接作为集合。但是我希望获取我的实体修订版的查询仅获取地址信息而忽略订单信息,这可能吗?我不想用@NotAudited注释订单信息,因为在其他情况下可能需要历史记录信息。

@ManyToOne
@JoinColumn(name="ADDR_ID")
public Address getAddress() { return address; }

@OneToMany
@JoinColumn(name="CUST_ID") 
public Set<Order> getOrders() {return orders;}
java hibernate orm audit hibernate-envers
1个回答
0
投票

this Hibernate documentation example中一样,如果我有一个具有Customer关联的Address

@Audited( withModifiedFlag = true )
@Entity(name = "Customer")
public class Customer {

    ...

    @ManyToOne(fetch = FetchType.LAZY)
    private Address address;

}

并且我使用this query查询修订版本:

List<Customer> customers = AuditReaderFactory
.get( entityManager )
.createQuery()
.forRevisionsOfEntity( Customer.class, false, true )
.add( AuditEntity.id().eq( 1L ) )
.add( AuditEntity.property( "lastName" ).hasChanged() )
.getResultList();

仅对Customers而不是Address进行查询:

select
    c.id as id1_3_0_,
    c.REV as REV2_3_0_,
    defaultrev1_.REV as REV1_4_1_,
    c.REVTYPE as REVTYPE3_3_0_,
    c.REVEND as REVEND4_3_0_,
    c.created_on as created_5_3_0_,
    c.createdOn_MOD as createdO6_3_0_,
    c.firstName as firstNam7_3_0_,
    c.firstName_MOD as firstNam8_3_0_,
    c.lastName as lastName9_3_0_,
    c.lastName_MOD as lastNam10_3_0_,
    c.address_id as address11_3_0_,
    c.address_MOD as address12_3_0_,
    defaultrev1_.REVTSTMP as REVTSTMP2_4_1_
from
    Customer_AUD c cross
join
    REVINFO defaultrev1_
where
    c.id = ?
    and c.lastName_MOD = ?
    and c.REV=defaultrev1_.REV
order by
    c.REV asc

-- binding parameter [1] as [BIGINT]  - [1]
-- binding parameter [2] as [BOOLEAN] - [true]

除非您导航这样的关联:

AuditQuery innerJoinAuditQuery = AuditReaderFactory
.get( entityManager )
.createQuery()
.forEntitiesAtRevision( Customer.class, 1 )
.traverseRelation( "address", JoinType.INNER );

Hibernate Envers不应将它们包括在SQL版本查询中。

© www.soinside.com 2019 - 2024. All rights reserved.