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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:32:20+00:00 2026-05-26T19:32:20+00:00

I have a JPA Entity StatsEntity which has a composite primary key that is

  • 0

I have a JPA Entity StatsEntity which has a composite primary key that is also as foreign key to another Entity Roster. This is setup as a @OneToOne relationship using @JoinColumns({@JoinColumn…}) annotations.

StatsEntity extends another entity CoreStatsEntity which is setup as @MappedSuperClass where as RosterEntity extends another entity CoreRoster using SINGLE_TABLE inheritance strategy.

@Entity
@Table(name = "Stats")
@IdClass(value = StatsEntity.Key.class)
public class StatsEntity extends CoreStatsEntity implements
    Stats {

@Id
private Integer competitionId;

@Id
private Integer playerId;

@Id
private Integer teamId;

@OneToOne
@JoinColumns({
        @JoinColumn(name = "competitionId", referencedColumnName = "competitionId", insertable = false, updatable=false),
        @JoinColumn(name = "playerId", referencedColumnName = "personId", insertable = false, updatable=false),
        @JoinColumn(name = "teamId", referencedColumnName = "teamId", insertable = false, updatable=false) })
private RosterEntity roster;


....

}

StatsEntity.Key

 @Embeddable
public static class Key implements Serializable {

    private static final long serialVersionUID = -7349082038890396790L;

    @Column(name = "competitionId", insertable = false, updatable = false)
    private Integer competitionId;

    @Column(name = "playerId", insertable = false, updatable = false)
    private Integer playerId;

    @Column(name = "teamId", insertable = false, updatable = false)
    private Integer teamId;

    public Key() {
        super();
    }

    public Key(int competitionId, int playerId, int teamId) {
        this.competitionId = Integer.valueOf(competitionId);
        this.playerId = Integer.valueOf(playerId);
        this.teamId = Integer.valueOf(teamId);
    }

    public int getTeamId() {
        return teamId.intValue();
    }

    public void setTeamId(int teamId) {
        this.teamId = Integer.valueOf(teamId);
    }

    public int getPlayerId() {
        return playerId.intValue();
    }

    public void setPlayerId(int playerId) {
        this.playerId = Integer.valueOf(playerId);
    }

    public int getCompetitionId() {
        return competitionId.intValue();
    }

    public void setCompetitionId(int CompetitionId) {
        this.competitionId = Integer.valueOf(CompetitionId);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object object) {
        if (object == this) {
            return true;
        }
        if (!(object instanceof Key)) {
            return false;
        }

        Key other = (Key) object;
        return Utils.equals(other.getTeamId(), this.getTeamId())
                && Utils.equals(other.getPlayerId(), this.getPlayerId())
                && Utils.equals(other.getCompetitionId(),
                        this.getCompetitionId());
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return Utils.hashCode(this.teamId, this.playerId,
                this.competitionId);

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return Utils.toString("CompetitionPlayerStatsEntity.Key",
                this.teamId, this.playerId, this.competitionId);
    }
}

CoreStatsEntity.java

@MappedSuperclass
public abstract class CoreStatsEntity
{}

RosterEntity

 @Entity
 @DiscriminatorValue("20")
  public class RosterEntity extends
    CoreRosterEntity  {

    //.... attributes, getters, setters

    }

CoreRosterEntity.java

@Entity
@DiscriminatorValue("0")
@Table(name="Roster")
@IdClass(CoreRoster.Key.class)           

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator", discriminatorType=DiscriminatorType.INTEGER)
public class CoreRosterEntity  {

        private static final long serialVersionUID = 1521639115446682871L;

@Id
private Integer competitionId;

@Id
private Integer teamId;

@Id
private Integer playerId;

//.. getters, setters and other attributes

}

CoreRoster.Key.class inside CoreRoster.java

@Embeddable
public static class Key implements Serializable {

    private static final long serialVersionUID = 2L;

    @Column(name="competitionId", nullable=false)
    private Integer competitionId;

    @Column(name="teamId", nullable=false)
    private Integer teamId;

    @Column(name="personId", nullable=false)
    private Integer playerId;

    public Key() {
        super();
    }

    public Key(int competitionId, int teamId, int playerId) {
        this.competitionId = Integer.valueOf(competitionId);
        this.teamId = Integer.valueOf(teamId);
        this.playerId = Integer.valueOf(playerId);
    }

    public int getPlayerId() {
        return playerId.intValue();
    }

    public void setPlayerId(int playerId) {
        this.playerId = Integer.valueOf(playerId);
    }

    public int getTeamId() {
        return teamId.intValue();
    }

    public void setTeamId(int teamId) {
        this.teamId = Integer.valueOf(teamId);
    }

    public int getCompetitionId() {
        return this.competitionId.intValue();
    }

    public void setCompetitionId(int competitionId) {
        this.competitionId = Integer.valueOf(competitionId);
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object object) {
        if (object == this) { return true; }
        if (!(object instanceof Key)) { return false; }

        Key other = (Key) object;
        return Utils.equals(other.getCompetitionId(), this.getCompetitionId()) &&
               Utils.equals(other.getTeamId(), this.getTeamId()) &&
               Utils.equals(other.getPlayerId(), this.getPlayerId());
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return Utils.hashCode(this.competitionId, this.teamId,
                              this.playerId);
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return Utils.toString("CoreRoster.Key",
                              this.competitionId, this.teamId,
                              this.playerId);
    }
}

When I persist StatsEntity, it gets persisted. But when I try to find it using the primary key it gives me an error:

    StatsEntity playerStats = new StatsEntity();
    //set all values
this.persist(playerStats);              
entityManager.find(StatsEntity.class, playerStats.getId()); //getId returns the composite primary key

 java.lang.IllegalArgumentException: Provided id of the wrong type for class   com.sports.RosterEntity. Expected: class com.sports.CoreRoster$Key, got class com.espn.sports.StatsEntity$Key

My first question here is, is the @OneToOne mapping I have given correct or not?
If it is correct then why this error appears when I try to find the entity using primarykey.

  • 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-26T19:32:21+00:00Added an answer on May 26, 2026 at 7:32 pm

    You haven’t posted full source code, especially of your primary key class, but you’ve mapped foreign key as read-only, which is required when single column is mapped more than once.

    I see however that you id columns are exactly the same 3 columns that are foreign key to RosterEntity, rights? In that case this RosterEntity should be your ID, which would simplify your design.

    What is the return type of your getId() method? The problem is propably with definition or usage of IdClass.

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

Sidebar

Related Questions

I have a JPA entity class with a composite primary key (uid,lid) that in
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have an entity (JPA annotations with Hibernate query interface) which has a composite
A have a JPA entity that has timestamp field and is distinguished by a
We have an JPA @Entity class (say User) which has a @ManyToOne reference (say
I have a service that gets a JPA entity from outside code. In this
both also used for annotating foreign key in jpa entity right? I do understand
I would like to have a JPA Entity that has a GSon Object as
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
We have a project that uses JPA/Hibernate on the server side, the mapped entity

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.