以下是侦听器类的类定义,该类也是数据库实体。它与TLS实体有关系。

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "LISTENER")
@IdClass(EntityId.class)

public class Listener implements MultitenantEntity, AuditedEntity {

    @Id
    @Column(name = "LISTENER_ID")
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    public String id;

    @Id
    @Column(name = "LISTENER_INSTANCE_ID")
    private String instanceId;

    @OneToMany(mappedBy="listener", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @Fetch(value= FetchMode.SELECT)
    private List<Path> paths;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns ({
        @JoinColumn(name="TLS_NAME"),
        @JoinColumn(name="INSTANCE_ID")
    })
    @Fetch(value=FetchMode.SELECT)
    private TlsDescriptor tlsDescriptor;

    @Embedded
    @Builder.Default private AuditMetadata audit = new AuditMetadata();
}


以下是TLS的类定义,它也是一个数据库实体:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "TLS_DESCRIPTOR")
@IdClass(EntityId.class)
public class TlsDescriptor implements MultitenantEntity, AuditedEntity        {
    @Id
    @Column(name = "TLS_NAME")
    public String id;

    @Id
    @Column(name = "INSTANCE_ID")
    private String instanceId;

    @Column(name = "TLS_PROTOCOL")
    public String tlsProtocol;

    @Column(name = "CIPHERS")
    public String ciphers;

    @Lob
    @Column(name = "CERTIFICATE", nullable = false)
    public String certificate;

    @Lob
    @Column(name = "PRIVATE_KEY", nullable = false)
    public String privateKey;

    @Embedded
    @Builder.Default private AuditMetadata audit = new AuditMetadata();
}


在这里,AuditMetadata是一个类,其中包含诸如createdTimestamp和ModifyedTimestamp之类的属性,本质上跟踪与之关联的对象的创建时间和修改时间。

现在,我编写了下面的Hibernate查询,该查询尝试为其获取侦听器对象:


在中提到的时间之后,听众本身会更新
“时间戳记”已过去
或相关路径在更新后
“时间戳记”中提到的时间已过
或者相关的tlsDescriptor在“时间戳记”中提到的时间过去之后已更新

  public List<Listener> findAll(long timestamp, long now, Pageable pageable) {
return super.find(currentSession()
                .createCriteria(Listener.class, "listener")
                .createAlias("listener.paths", "path")
                .add(Restrictions.or(
                        Restrictions.between("audit.modifyTimestamp", new Date(timestamp), new Date(now)),
                        Restrictions.between("path.audit.modifyTimestamp", new Date(timestamp), new Date(now)),
                        Restrictions.between("listener.tlsDescriptor.audit.modifyTimestamp", new Date(timestamp), new Date(now))))
                .add(Restrictions.isNotEmpty("listener.paths")),
        pageable);


}


现在的问题是,path是必填字段,而tlsDescriptor是可选字段。因此,当存在tlsDescriptor时,该查询工作正常,但如果不存在,则会出现以下错误:

org.hibernate.QueryException: could not resolve property: tlsDescriptor.audit.modifyTimestamp of:Listener


我试图尝试使用Hibernate的合取,析取,AND和OR语句解决此问题,但此错误不断重复!

最佳答案

对带有JoinType.LEFT_OUTER_JOIN的tlsDescriptor使用别名(因此,如果相关模型为null,则结果不会为空):

public List<Listener> findAll(long timestamp, long now, Pageable pageable) {
return super.find(currentSession()
                .createCriteria(Listener.class, "listener")
                .createAlias("listener.paths", "path")
                .createAlias("listener.tlsDescriptor", "tls", JoinType.LEFT_OUTER_JOIN)
                .add(Restrictions.or(
                        Restrictions.between("audit.modifyTimestamp", new Date(timestamp), new Date(now)),
                        Restrictions.between("path.audit.modifyTimestamp", new Date(timestamp), new Date(now)),
                        Restrictions.between("tls.audit.modifyTimestamp", new Date(timestamp), new Date(now)))),
        pageable);

10-05 18:16