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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:57:40+00:00 2026-05-27T10:57:40+00:00

I have a table with three columns, A, B and C. Two of these

  • 0

I have a table with three columns, “A”, “B” and “C”. Two of these columns (A and B) are the composite primary key for the table. I’ve written a Java class for this table and I’m using Hibernate to map the data in the class to the data in the table. I’ve created another class for the Embedded (i.e., composite) key.

The two classes look like this:

@Entity
@Table(name="SOME_TABLE", schema="SOME_SCHEMA")
public class Outer {
    private Key key;
    private String a;
    private String b;
    private String c;

    @EmbeddedId
    public Key getId() {
        return this.key;
    }

    // Also a setter for Key...

    @Column(name="A")
    public String getA() {
        return this.a;
    }

    public void setA(final String a) {
        this.a = a;
    }

    // Also setters and getters for B and C.
}

.

@Embeddable
public class Key {
    private String a;
    private String b;

    @Column(name="A")
    public String getA() {
        return this.a;
    }

    public void setA(final String a) {
        this.a = a;
    }

    // Also a getter & setter for B.
}

It seems like all the Hibernate documentation glosses over where, exactly, the getters and setters should go. Should both Outer and Key have setters for the values that make up the composite key? If I do have a setter in both places, does Hibernate do the right thing (i.e., set the value on Key) if I set the value for A on Outer? Should Outer.getA/B() and Outer.setA/B() defer to the Key class?

I think it’s bad coding practice to have setters in two places for the same data. It’s ambiguous at the very least. I’m hoping that Hibernate will set the values on the Key class for me, but I’d like to know for sure. Thanks!

-joe

  • 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-27T10:57:41+00:00Added an answer on May 27, 2026 at 10:57 am

    I am not 100% sure with @EmbeddedId but I think it must be handled the same way as regular @Embedded properties, so I think the mapping should be like this:

    @Entity
    @Table(name="SOME_TABLE", schema="SOME_SCHEMA")
    public class Outer {
        private Key key;
        // no fields a and b here
        private String c;
    
        @EmbeddedId
        public Key getId() {
            return this.key;
        }
    
        // Also a setter for Key...
    
        @Transient
        public String getA() {
            return this.key.getA();
        }
    
        public void setA(final String a) {
            this.key.setA(a);
        }
    
        @Transient
        public String getB() {
            return this.key.getB();
        }
    
        public void setB(final String b) {
            this.key.setB(b);
        }
    
        // Also setters and getters for C.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a (simplified) table consisting of three columns: id INT PRIMARY KEY NOT
I have two tables one has a three column composite key. The other needs
I'm using SQL Server 2008 and I have a table with three columns: Length
I have a mysql table with three columns: username, location, timestamp. This is basically
Let's say I have a table of two columns an three rows. Now, In
I have a composite primary key table (Maintenance Items). I would like to create
I have a table in which there are two columns : 1. import type,
I have a table ProductNumberDuplicates_backups, which has two columns named ProductID and ProductNumber. There
I have a table with three columns: user varchar, status varchar , rep int
I have a table with three filled columns named Name, City and Occupation. I

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.