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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:12:01+00:00 2026-06-12T21:12:01+00:00

I have two java classes, A & B, with A holding a reference to

  • 0

I have two java classes, A & B, with A holding a reference to B:

public class A {
   private long id;
   private B b;
   ...
}

public class B {
   private long id;
   ...
}

In my database only instances of A are stored, alongside with the ID of the referenced B’s:

$ select * from tableA;
id  | bid | ...
---------------
1   | 42  |
2   | 42  |
3   | 43  |
...

However, B are not stored in the database, but accessible through some implementation of a service layer ServiceB:

public interface ServiceB {
   public B getB(long bid);
}

How can I model that using Hibernate? I’d preferably use an XML-based HBM configuration. I also would like to avoid adding a transient field in A holding bid (if it is possible).

  • 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-12T21:12:03+00:00Added an answer on June 12, 2026 at 9:12 pm

    I’m posting here the solution found for reference.

    You have to create a custom hibernate UserType which is used on the mapping for the bid field in A class. Implementing UserType is already well explained on Internet and is not the point here. Suffice to say that the nullSafeGet() operation convert the database id to a B instance by loading the corresponding B from the service layer; and the nullSafeSet(), the other way around, convert a B instance to it’s internal id to store to the database.

    Linking this custom user type to the service layer can be done several ways depending on the injection framework used (Guice, Spring, custom solution…) but should not be a problem.

    You need a custom UserType for each referenced class B. Using generics may be useful in the case of several referenced classes deriving from a common base class, it’s left as an exercise to the reader.

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

Sidebar

Related Questions

I have two Java-Classes as follows: public class MyClass { ... } public class
I have the following two classes: import java.io.*; import java.util.*; public class User {
I have two java classes as follows App1 without a package: class App1 {
Actually I have two java classes of which one is an activity class. Now
I have two Java classes: B, which extends another class A, as follows :
I have two java classes. Schedule is the main class that uses an array
Thanks !!i have two .java classes as trial.java and CommonActivity.java as: public void onClick(View
Using java and testNG I have two tests in two different classes, test1.class and
I'm making a shopping list mobile application (Java ME) and i have two classes;
How do I do forward declarations in Java? I have two classes, each needs

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.