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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:23:41+00:00 2026-06-05T05:23:41+00:00

I am trying to map the following table CREATE TABLE Person ( p_id varchar(255)

  • 0

I am trying to map the following table

CREATE TABLE Person (
    p_id varchar(255) not null,
    p_name varchar(255 not null, 
    p_post_code varchar(12) not null,
    primary key (p_id, p_name),
);

Usually when i map an Entity to the above table i would do something like this (for single column primary keys):

private     int     p_id;   
private     String  p_name;
private     String  p_post_code;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="p_id")
public Long getPId() {
    return p_id;
}

public void setPId(int p_id) {
    this.p_id = p_id;
}

@Column(name="p_name")
public String getPname() {
    return p_name;
}

public void setPname(String string) {
    this.p_name = string;
}

@Column(name="p_post_code")
public String getPostCode() {
    return p_post_code;
}

public void setPostCode(String string) {
    this.p_post_code = string;
}   

The above works if the primary key is a single column (i.e. p_id) and the value for this column is generated in the database.
How would i modify the above to map it so that both p_id and p_name are the primary key.

Also, how would this work, if the composite key is a foreign key in another table.

I am trying to google for some examples but i cant find a simple example and most seem to be using the XML based configuration.

  • 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-05T05:23:43+00:00Added an answer on June 5, 2026 at 5:23 am

    When using composite keys with JPA you need to use an embedded class as an id.

    In your case you would have a person class and a primary key class for person:

    @entity
    public class Person
    {
        @EmbeddedId
        private PersonPK key;
    
        @Column(name="p_post_code", nullable = false)
        private String  p_post_code;
    
        //....
    }
    
    @Embeddable
    public class PersonPK
    {
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name="p_id");
        private Long p_id;
    
        @Column(name="p_name")
        private String p_name;
    
        public PersonPK(String name)
        {
            p_name = name;
        }
    
        //....
    }
    

    Using a class for the person’s name (so the name is also a foreign key):

    @entity
    public class Person
    {
        @EmbeddedId
        private PersonPK key;
    
        @MapsId(value="p_name_id")
        @ManyToOne
        @JoinColumn(name = "p_name_id", referencedColumnName = "id")
        private Name p_name;
    
        @Column(name="p_post_code", nullable = false)
        private     String  p_post_code;
    
        //....
    }
    
    @Embeddable
    public class PersonPK
    {
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name="p_id");
        private Long p_id;
    
        @Column(name="p_name_id")
        private Long p_name_id;
    
        public PersonPK(Name name)
        {
            p_name_id = name.getId();
        }
    
        //....
    }
    
    @Entity
    public class Name
    {
        @Id
        @GeneratedValue(some generation strategy here)
        @Column(name="id")
        private Long id;
    
        @Column(name="name")
        private String name;
    
        //....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to map the following model with Castle ActiveRecord Contact (a person represented
Trying to map the following schema using the Entity Framework. A Customer can have
I'm trying to map a Dictionary containing Lists. I have the following set of
I'm trying to map a table, where one of the column is a binary
I am trying to map a table frp, a SQL Server 2005 DB to
I am trying to create a REST API for my app. The map.connect_resource :book
i am trying to create a persistence client object in the following way: var
I'm trying to group data from the following three tables to create a new
I am trying to create a table using CGI.pm. The table would contain results
I'm trying to create a hexagon world map for my PHP browser based strategy

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.