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

  • Home
  • SEARCH
  • 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 4038858
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:31:34+00:00 2026-05-20T12:31:34+00:00

What would be the best practice to implement the following situation with Hibernate. We

  • 0

What would be the best practice to implement the following situation with Hibernate.

We define an abstract class that will form the base of any object that we want to persist in our database. It contains the id etc etc…

public abstract class ModelObject {
    protected int id;

    ...
}

Now we subclass our base clase for special cases where more than one entity will have a similar fields.

public abstract class ModelObjectWithNose extends ModelObject {
    ...
    protected Nose nose;
}

Now, for all classes that we want to have a Nose:

public class Person extends ModelObjectWithNose { ... }

public class Animal extends ModelObjectWithNose { ... }

The real problem we have at the moment now is that this relationship needs to be bi-directional. Each concrete class needs to know which Nose is theirs, but each Nose also needs to know to what object it belongs.

public class Nose {
    ...
    private ModelObjectWithNose owner;
}

For our example we need a @OneToOne relationship, as each Person can only have one Nose and each Nose can only belong to one Person.

What we have tried is doing the following:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class ModelObjectWithNose extend ModelObject { ... }

and then:

@Entity
public class Person extends ModelObjectWithNose { ... }

…etc etc etc.

1) How do I define the relationship between the Nose and the ModelObjectWithNose? Is it as simple as:

@Entity
public class Nose {
    ...
    // this causes: 'Basic' attribute should not be 'Persistence Entity' 
    ModelObjectWithNose owner;
}

2) Is there a better / more preferred way? Would the inheritance strategy work properly?

Thanks!

EDIT

So I have tried a few ways which all seem to potentially work. Using any of the three Inheritance schemes works fine if you set up the annotations as in the question. Unfortunately this has a huge impact on our existing database and code, so we opted to (for now) simply index the Nose object’s id field that is kept on our current objects that would have extended ModelObjectWithNose and simply write helper methods to do point queries to find the entries.

  • 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-20T12:31:35+00:00Added an answer on May 20, 2026 at 12:31 pm

    Annotate your Nose property in ModelObjectWithNose as @OneToOne and in the Nose entity you do the same with ModelObjectWithNose owner. Set the mappedBy attribute of the annotation of the Nose entity, so JPA knows on which table the FK should be inserted (In your model the ModelObjectWithNose should have the FK to Nose, not other way around. You set it on the inverse end of the one-to-one relationship, ie. the one that has the PK to use.)

    BTW your Entities must implement Serializable (you can do so on the ModelObject class to enforce it on inheriting classes). The id property of ModelObject should be annotated as @Id.

    Cheers,
    Kai

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

Sidebar

Related Questions

Would it be considered best practice to use a single template that changes it's
What's the best practice of making a variable that would be accessible by almost
Which is the best practice in this situation? I would like an un-initialized array
What would be best practice to localize your ASP.NET MVC application? I would like
What would be best practice for allowing the user to store data for my
Hey all, what would be best practice for clipping the bottom borders of a
What would you consider best practice for organizing JUnit tests in a project, and
What would be the best practice for sharing localization object (in this case ResourceBundle,
I was wondering what would be the best programming practice for jQuery. Most jquery
When parsing text, I frequently need to implement mini-state-machines, in the generic form following

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.