views:

325

answers:

1

Hello experts!

As the title states I have some issues with my hql statement. I have two entities:
SecondEntity:

public class SecondEntity implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    @JoinColumn(name = "firstEntityId")
    private FirstEntity firstEntity;

    @NotNull(message = "the blocked attribute should not be null")
    private Boolean blocked;

    /**
     * Default constructor.
     */
    public SecondEntity() {

}

    public FirstEntity getFirstEntity() {
        return firstEntity;
    }

    public void set(FirstEntity firstEntity) {
        this.firstEntity = firstEntity;
    }

    public Boolean isBlocked() {
        return blocked;
    }

    public void setBlocked(Boolean blocked) {
        this.blocked = blocked;
    }
}

FirstEntity:

public class FirstEntity implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @Valid
    @OneToMany(mappedBy = "firstEntity", cascade = { CascadeType.ALL }, orphanRemoval = true)
    private List<SecondEntity> seconds;

    public List<SecondEntity> getSeconds() {
    if (this.seconds == null) {
        this.seconds = new ArrayList<SecondEntity>();
       }
    return seconds;
    }

   /**
   * 
   * @param seconds
   */
   public void setSeconds(List<SecondEntity> seconds) {
       this.seconds = seconds;
   }

}

In my database I have three SecondEntitys, one of them is blocked. They are all connected to one FirstEntity When i perform this hql statement:

final StatelessSession session = HibernateStatelessSessionHelper.currentSession();

final String hql = "select distinct first from FirstEntity first left join fetch first.seconds as seconds where seconds.blocked = false";

(List<FirstEntity>) session.createQuery(hql).list();

I receive a list of FirstEntitys which holds two(?!) of the same FirstEntity. The SecondEntitys list in the FirstEntitys are correct and holds two SecondEntitys(one of the three was blocked).

Why?! Both of the FirstEntitys in the list have same id, shouldn't distinct only hold one occurence of the FirstEntity?? Please help me with this statement.

// Jakob

A: 

I think you haven't put the real HQL you are using.

"select distinct first from FirstEntity first left join fetch first.seconds as seconds where seconds.blocked = false)";

there is a unmatched closing paranthesis.

Normally distinct should eliminate duplicated entries in result. I bet you have something wrong in your HQL.

Adrian Shum
Hi! Thank you for your answer, actually somehow it worked with the unmatched closing paranthesis. I have removed it, but the issue still remains.
jakob