Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

spring boot - JPA/Hibernate. How to get child objects contained in a list of Parent object using createQuery method

I have Certificate class that contains list of Tag classes

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "gift_certificate")
public class Certificate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String description;
    private BigDecimal price;
    private Integer duration;
    @Column(name = "create_date")
    private LocalDateTime createDate;
    @Column(name = "last_update_date")
    private LocalDateTime lastUpdateDate;
    @ManyToMany
    @JoinTable(name = "gift_certificate_tag",
            joinColumns = @JoinColumn(name = "tag_id"),
            inverseJoinColumns = @JoinColumn(name = "gift_certificate_id")
    )
    private List<Tag> tags;
    
getters and setters and other code...

....

import javax.persistence.*;
import java.util.Objects;

@Entity
@Table(name = "tag")
public class Tag {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

getters and setters and other code...

And I'm trying to get List of Certificate from DB using JPA/Hibernate. I'm using EntityManager

public List<Tag> getCertificateTags(Long certificateId) {
    return entityManager.createQuery("select c.tags from Certificate c where c.id=:id")
            .setParameter("id", certificateId)
            .getResultList();
}

And it works, but I get just list, not List and IDEA is warning Unchecked assignment: 'java.util.List' to 'java.util.List<Tag>'. And when I use createQuery with second parameter Tag.class like this:

entityManager.createQuery("select c.tags from Certificate c where c.id=:id", Tag.class)
                .setParameter("id", certificateId)
                .getResultList();

I get java.lang.IllegalArgumentException: Type specified for TypedQuery [Tag] is incompatible with query return type [interface java.util.Collection]

How can I fix it?

question from:https://stackoverflow.com/questions/65879693/jpa-hibernate-how-to-get-child-objects-contained-in-a-list-of-parent-object-usi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try to change the query this way

select t from Certificate c join c.tags t where c.id=:id

The reason is that select c.tags means every result row contains a list of tags. But when you select t from Certificate c join c.tags t every row contains one tag


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...