1

I am practicing with my first java project... Developing an application using Spring Boot and Spring Data JPA.
The following are entities:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id", nullable = false, insertable = false, updatable = false)
    private Long id;
    
    @Column(name = "FormId", nullable = false, updatable=false)
    private @NotNull Long formId;
    
    @Column(name = "UUID", nullable = false,length = 32)
    private @NotNull String uuid;
    
    @Column(name = "SignLogUUID", nullable = false,length = 32)
    private @NotNull String signLogUUID;
    
    @Column(name = "LanguageCode", nullable = false, length = 10)
    private @NotNull String languageCode;
    
    @Column(name = "ApplyType", nullable = false, length = 1)
    private @NotNull String applyType;
    
    @Column(name = "SignStatus", nullable = false, length = 1)
    private @NotNull String signStatus;
    
    @Column(name = "IsActive", length = 1)
    private String isActive="0";
    
    @Column(name = "ReceiveId")
    private Long receiveId;
    
    @Column(name = "Note", length = 200)
    private String note;
    
    @Column(name = "CreateId", nullable = false)
    private @NotNull Long createId;

    @Column(name = "CreateTime", nullable = false)
    private @NotNull Date createTime;

    @Column(name = "ModifyId", nullable = false)
    private @NotNull Long modifyId;

    @Column(name = "ModifyTime", nullable = false)
    private @NotNull Date modifyTime;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SignLogUUID", referencedColumnName = "SignLogUUID", insertable = false, updatable = false)
    private WebInfo webInfo;

    public WebInfo getWebInfo() {
        if (this.webInfo == null) {
            return null;
        }
        return webInfo;
    }

and now I have a problem with following SQL , do anyone know how to change these into JPA type?

SELECT * FROM webamdb.sign_log 
WHERE 
 id IN (SELECT MAX(id) 
             FROM webamdb.sign_log
             GROUP BY SignLogUUID) 
AND FormId = '24' 
AND SignStatus = '1'
ORDER BY CreateTime;

please help me ,thanks...


2022/06/29 [update]:
Finally,I found a way to solved my problem!!!:))

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

CriteriaQuery<SignLog> criteriaQuery = criteriaBuilder.createQuery(SignLog.class);
Root<SignLog> root = criteriaQuery.from(SignLog.class);
Subquery<Long> subquery = criteriaQuery.subquery(Long.class);
Root<SignLog> subRoot = subquery.from(SignLog.class);

subquery.select(criteriaBuilder.max(subRoot.get("id")));
subquery.groupBy(subRoot.get("signLogUUID"));

List<Predicate> conditions = new ArrayList<>();

conditions.add(criteriaBuilder.and(root.get("id").in(subquery)));
conditions.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("formId"), formId)));
conditions.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("signStatus"), signStatus)));

criteriaQuery.orderBy(criteriaBuilder.desc(root.get("createTime")));

TypedQuery<SignLog> typedQuery = entityManager.createQuery(criteriaQuery);
List<SignLog> result = typedQuery.getResultList();

Jessie
  • 11
  • 3

0 Answers0