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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:47:26+00:00 2026-05-17T22:47:26+00:00

there is a situation. For example, I am designing simple blog. There are articles

  • 0

there is a situation. For example, I am designing simple blog. There are articles and photographies. Users can add their comment to both of them. So when I write it in Java, it looks like this:

public interface Commentable { ... }

public class Article implements Commentable { ... }

public class Photo implements Commentable { ... }

public class Comment { 
   ...
   private Commentable commentTo; 
}

This is clear and I hope that design is correct. But now I would like to persist it in database and I want to use JPA annotations. Primarily I have to use JPA 1.0 but if there is not solution I would like to know how to do it in JPA 2.0. I found out that there is a way with classic inheritance but I think that Commentable shouldn’t be a parent of these object, it is only extension in light of design.

Is there any way how to persist it without changing of desing, please? Thanks a lot

  • 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-17T22:47:26+00:00Added an answer on May 17, 2026 at 10:47 pm

    Is there any way how to persist it without changing of design, please? Thanks a lot

    JPA doesn’t really support your design (to be precise, you can map a relation pointing to an interface if there is a single implementation, but that’s not your case). Your JPA provider might have some support for this though (e.g. EclipseLink has a @VariableOneToOne annotation).

    With standard JPA, the usual approach would be to use an abstract class (with or without your interface) and to use inheritance. For example (keeping the interface):

    public class Comment { 
       ...
       @OneToOne(targetEntity=MyAbstractBaseClass.class)
       private Commentable commentTo; 
    }
    

    Where both Article and Photo would extend MyAbstractBaseClass and implment the interface.

    The JPA wikibook has two good sections on this topic, see below.

    Resources

    • JPA Wikibook:
      • My relationship target is an interface
      • Variable and Heterogeneous Relationships
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will demonstrate the problem on a real example so the situation: there is
Is there any situation in which notify() can cause deadlock, but notifyAll() - never?
Situation: There are several entities in a simulated environment, which has an artificial notion
I have this situation: There are a login page with a login form (form
I have the following situation: There is one custom view inside of the first
There is a situation where I need to have a suspend kind of behavior
There is the situation - i made some math modelling app on C#(WPF), showing
Suppose there is a situation like : statement 1; statement 2; statement 3; statement
Situation is like this: There are independent 5 services. Each service consists of a
is there any way how in this situation insert and update DB with single

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.