Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes @Loader work correctly on annotations (using JPA)?
    primarykey
    data
    text
    <p>This is a somewhat contrived example meant to show a point. Here are two entity classes linked by a relationship and a named query:</p> <pre><code>@Entity @NamedNativeQuery(name = "loadFoo", query = "select * from foos where bar_id = ?", resultClass = Foo.class) public class Bar { @Id private int id; @ManyToOne @Loader(namedQuery = "loadFoo") private Foo foo; public void setId(int id) { this.id = id; } public int getId() { return id; } public void setFoo(Foo foo) { this.foo = foo; } public Foo getFoo() { return foo; } } </code></pre> <p>And then:</p> <pre><code>@Entity @Table(name="foos") public class Foo { @Id @GeneratedValue private int id; @Column(name="bar_id") private int barId; public void setBarId(int barId) { this.barId = barId; } public int getBarId() { return barId; } public void setId(int id) { this.id = id; } public int getId() { return id; } } </code></pre> <p>The odd thing is that the named query works just fine in unit testing:</p> <p>Here's the setup:</p> <pre><code> Bar bar = new Bar(); entityManager.persist(bar); entityManager.flush(); Foo foo = new Foo(); foo.setBarId(bar.getId()); entityManager.persist(foo); entityManager.flush(); entityManager.clear(); </code></pre> <p>And this correctly returns Foo, so there does not appear to be a problem with the named query:</p> <pre><code> Foo foo2 = (Foo) entityManager.createNamedQuery("loadFoo").setParameter(1, bar.getId()).getSingleResult(); </code></pre> <p>But the load does not:</p> <pre><code> Bar bar2 = entityManager.find(Bar.class, bar.getId()); Foo foo3 = bar2.getFoo(); assertEquals(foo2.getId(), foo3.getId()); </code></pre> <p>There are no errors thrown. The returned object is simply null. The @Loader annotation is in fact being processed by the persistence unit, because if I make the named query invalid, an exception will be thrown when getFoo() is called. But it never returns a correct value.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload