简体   繁体   中英

JPA: How can I Select @Embeddable Class

I will select all the columns from @Embeddable Class Certification. But i cann't select it. how can i do select the Embeddable class.

@Entity
public class Department implements Serializable {


  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String name;

  @ElementCollection
  @CollectionTable(name = "Certification", joinColumns = {@JoinColumn(name="user_id")})
  private List<Certification> certifications = new ArrayList<Certification>();

  public List<Certification> getCertifications() {
      return certifications;
  }

  public void setCertifications(List<Certification> certifications) {
      this.certifications = certifications;
  }


  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  .....

@Embeddable Class Certification

@Embeddable
public class Certification{

  private String name;
  private String certArt;

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }



  public String getCertArt() {
      return certArt;
  }

  public void setCertArt(String certArt) {
      this.certArt = certArt;
  }

  ......

If i run the ResultService i get the following exception:

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Error compiling the query [select c from Certification c]. Unknown entity type [Certification].

How can i select the @Embeddable Class?

You would need to retrieve Certification via the real Entity class ie Department . One example query could be as below:

   select cer from Department dep join dep.certifications cer

Alternatively, you may want to retrieve qualifying Department entities and then fetch certifications using them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM