jeudi 23 juillet 2015

Unable to retrieve a @OneToMany relationship in Hibernate

I have this two entities:

@Entity
@Table(name = "PROJECT")
public class Project {

  public Project () {}

  public Project(Long id, Set<Location> locations) {
        this.id = id;
        this.locations= locations;
  }

  @OneToMany(mappedBy = "project", fetch = FetchType.LAZY, cascade =    CascadeType.ALL, orphanRemoval = true)
  private Set<Location> locations= new HashSet<>();

  //others fields, getters and setters follow
}

@Entity
@Table(name = "LOCATION")
public class Location {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_PROJECT", nullable = false)
    private Project project;

    //others fields, getters and setters follow
}

I try to read a list of project with the following code

CriteriaQuery<Project> cq = criteriaBuilder.createQuery(Project.class);
Root<Progetto> r = cq.from(Project.class);

cq.multiselect(r.<Long>get("id"), r.<Location> get("locations"));
TypedQuery<Project> q = entityManager.createQuery(cq);
q.getResultList();

and hibernate executes this query

select project0_.ID as col_0_0_, . as col_1_0_ from PROJECT project0_ inner join LOCATION location1_ on project0_.ID=location1_.ID_PROJECT

which is obviously wrong because of the '. as col_1_0_'. Why does Hibernate produce this wrong query?

Any help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire