Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6023405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:00:00+00:00 2026-05-23T04:00:00+00:00

This is a somewhat contrived example meant to show a point. Here are two

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:00:01+00:00Added an answer on May 23, 2026 at 4:00 am

    @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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a somewhat contrived example, but I'm left scratching my head.
This is somewhat of a follow-up to an answer here . I have a
This is somewhat asp.net MVC related only for example purposes but I was hoping
This is a somewhat low-level question. In x86 assembly there are two SSE instructions:
This is somewhat of a multipart question, but.. I am looking to query a
This is somewhat of a follow-up question to this question . Suppose I have
I know that this is somewhat subjective, but I wonder if there is a
I know this is somewhat of a server question, but I wanted to ask
This is a somewhat bizarre question. My objectives are to understand the language design
[This question is somewhat related to this question , but the answers are not...]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.