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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:20:51+00:00 2026-05-28T16:20:51+00:00

I should start out by saying that I am fairly new to Java EE

  • 0

I should start out by saying that I am fairly new to Java EE and that I do not have a strong theoretical background in Java yet.

I’m having trouble grasping how to use JPA together with interfaces in Java. To illustrate what I find hard I created a very simple example.

If I have two simple interfaces Person and Pet:

public interface Person
{
    public Pet getPet();
    public void setPet(Pet pet);
}

public interface Pet
{
    public String getName();
}

And an Entity PersonEntity which implements Person as well as a PetEntity which implements Pet:

@Entity
public class PersonEntity implements Person
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private PetEntity pet;

    @Override
    public void setPet(Pet pet)
    {
        /* How do i solve this? */
    }
}

@Entity
public class PetEntity implements Pet
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    /* Getters and Setters omitted */

}

How do I properly handle the case in the setPet method in which I want to persist the relationships between the two entities above?

The main reason I want to use interfaces is because I want to keep dependencies between modules/layers to the public interfaces. How else do I avoid getting a dependency from e.g. my ManagedBean directly to an Entity?

If someone recommends against using interfaces on entities, then please explain what alternatives methods or patterns there are.

  • 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-28T16:20:53+00:00Added an answer on May 28, 2026 at 4:20 pm

    You can use targetEntity property in the relationship annotation.

    @Entity
    public class PersonEntity implements Person {
        private Long id;
    
        private Pet pet;
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        @Override
        @OneToOne(targetEntity = PetEntity.class)
        public Pet getPet() {
            return pet;
        }        
    
        public void setPet(Pet pet) {
            this.pet = pet;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me start out by saying that I am new to Java/JSP web development,
Let me start out by saying that I feel like there should be a
I'll start out by saying that I'm totally new to CSS and HTML so
OK, I should start this off by saying I'm not sure this is necessarily
I'll start out by saying I have no idea what so ever about what
Let me start out by saying I have tried this and looked at this
I'm going to start this off by saying that I am new to GLSL
I read a lot of posts that convinced me I should start writing unit
So any custom data attribute that I use should start with data-: <li class=user
Just want to start out by saying this seems like a great site, hope

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.