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 8361513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:43:05+00:00 2026-06-09T11:43:05+00:00

Consider an entity like so public class SomeEntity { @ManyToOne @JoinColumn(name = company_fk) private

  • 0

Consider an entity like so

public class SomeEntity {
    @ManyToOne
    @JoinColumn(name = "company_fk")
    private CompanyEntity company;
}

The company_fk column is an integer. In my application I am frequently find myself in a situation where I have the company_fk integer but I don’t necessarily have the CompanyEntity.

What i want to do is to create a new instance of SomeEntity and associate it with CompanyEntity so I end up having to read the CompanyEntity from the db just so I can establish the relationship. This is not very good because I am going to the db even though I don’t need to do so.

Is there a way to avoid having to load an entity just to establish a relationship? I am using JPA 2 with Hibernate and I am open to using hibernate only mapping to make this work if there is way.

  • 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-06-09T11:43:06+00:00Added an answer on June 9, 2026 at 11:43 am

    That’s exactly why em.getReference() exists. It assumes the entity with the given ID exists, and returns a proxy to this entity:

    // get a reference to the company without going to the DB:
    CompanyEntity company = em.getReference(CompanyEntity.class, companyId);
    SomeEntity e = new SomeEntity();
    e.setCompany(company);
    

    Hibernate’s native Session class has the same method, except it’s called load().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: @Entity @Table(name = a) public class A implements Serializable {
consider the following class and struct public class Entity { public IdType Id {get;set;}
Consider a class (an ORM entity): public class MyEntity { Long id; MyOtherEntity assoc;
To illustrate my question consider the following example: @Entity public class Box implements Serializable
Let's say I have an entity that looks like this: public class Album() {
Consider the following entity model: public class Agreement : Entity { public int AgreementId
Consider the following hibernate configuration: <class name=Person> <id name=id column=personId> <generator class=native/> </id> <set
Consider two entity classes CD and Track : public class CD { public long
Consider following class public class AccountGroup : Entity<int> { public AccountGroup() { Accounts =
Hi I was thinking about such case: public class MyService { private IList<Entity> data;

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.