开发者

Does @Loader work correctly on annotations (using JPA)?

开发者 https://www.devze.com 2023-03-13 03:21 出处:网络
This is a somewhat contrived example meant to show a point. Here are two entity classes linked by a relationship and a named query:

This is a somewhat contrived example meant to show a point. Here are two entity classes linked by a relationship and a named query:

@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; }
}

And then:

@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; }
}

The odd thing is that the named query works just fine in unit testing:

Here's the setup:

    Bar bar = new Bar();
    entityManager.persist(bar);
    entityManager.flush();

    Foo foo = new Foo();
    foo.setBarId(bar.getId());

    entityManager.persist(foo);
    entityManager.flush();
    entityManager.clear();

And this correctly returns Foo, so there does not appear to be a problem with the named query:

    Foo foo2 = (Foo) entityManager.createNamedQuery("loadFoo").setParameter(1, bar.getId()).getSingleResult();

But the load does not:

    Bar bar2 = entityManager.find(Bar.class, bar.getId());
    Foo foo3 = bar2.getFoo();       
    assertEquals(foo2.getId(), foo3.getId());

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.


@Loader can only apply on a class or on a collection, not on a @*ToOne. In your case, you would need to put the @Loader annotation on top of class Foo.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号