0

I want an ask for you. I wrote a JPQL query like this (I write the part that I want to bring to your attention):

SELECT DISTINCT
    e  
FROM  
    MyEntity e  
    JOIN e.myEntityTwo s
    ...
    WHERE  
    s.id = :id
    ...

MyEntity is something like this:

@Entity
@Table(name="DBTABLE")
public class MyEntity implements Serializable {
    ...

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="ID_TABLE")
    private MyEntityTwo myEntityTwo;

    ...

    //getter and setter
}

So, when I execute query, this returns me also myEntityTwo object for some id filter (but..it was not lazy?!) and does not return any myEntityTwo object for others one. The strange thing is that the sql generated query (I see it from the stacktrace) looks the same in both cases! I'm using Oracle and JPA (Hibernate provider) I can not explain why?!

Fra83
  • 511
  • 1
  • 5
  • 15
  • So are you saying that the above JPQL returns `myEntityTwo` object as well, but only for a particular `id` of `myEntityTwo` and not for all. If so, may be there is no associated `myEntityOne` record for that `myEntityTwo`.Also can you try with `LEFT JOIN` instead of `JOIN`. One a different note, did you wanted this to be `e.id = :id` instead of `s.id = :id`. – Madhusudana Reddy Sunnapu Apr 02 '16 at 17:36
  • Hi Madhusudana Reddy Sunnapu, there are elements that respect the relationship between MyEntity and MyEntityTwo and this relationship is filtered by id values. I saw full list after query execution. The solution is to replace `JOIN e.myEntityTwo s` with `JOIN FETCH e.myEntityTwo s` to also have MyEntityTwo object, but I can't explain why this happened. :( – Fra83 Apr 02 '16 at 17:45
  • When we use `JOIN` it just joins the entities but doesn't populate the associated entities unless we specify `JOIN FETCH`. This link might help http://stackoverflow.com/questions/17431312/difference-between-join-and-join-fetch-in-hibernate – Madhusudana Reddy Sunnapu Apr 02 '16 at 17:52
  • I knew this. In fact, I expected that I never find myEntityTwo objects into MyEntity. So why in some cases I find it? – Fra83 Apr 02 '16 at 17:56
  • 1
    Does it depend on whether or not it was in some cache? Is it important? – K.Nicholas Apr 03 '16 at 02:32

0 Answers0