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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:01:29+00:00 2026-05-12T14:01:29+00:00

Take a look at the following classes (UNIdirectional @OneToMany) @Entity public class Team {

  • 0

Take a look at the following classes (UNIdirectional @OneToMany)

@Entity
public class Team {

    private Integer id = -1;

    @Id
    @GeneratedValue
    public Integer getId() {
        return this.id;
    }

    private List<Player> playerList;


    @OneToMany
    @Cascade(CascadeType.SAVE_UPDATE)
    @JoinColumn(name="TEAM_ID", insertable=false, updateable=false)
    public List<Player> getPlayerList() {
        return this.playerList;
    }

    public void addPlayer(Player player) {
        player.setId(new PlayerId(getId(), getPlayerList().size()));

        playerList.add(player);
    }

}

Notice addPlayer: team’s id and player’s teamId references the same instance so

AssertEquals(team.getId(), player.getPlayerId().getTeamId());

should work, don’t ?

Hibernate will not save -1 value because i have declared a generator strategy (default to auto) If an identifier generator is declared, Hibernate takes care of the primary key value assignment. I have assined -1 to Team’s id IN ORDER TO SHARE ITS REFERENCE in Player’s id (when calling addPlayer method) as follows

@Embeddable
public class PlayerId implements Serializable {

    private Integer teamId;

    private Integer playerIndex;

    // required no-arg constructor
    public PlayerId () {}

    public PlayerId(Integer teamId, Integer playerIndex) {
        this.teamId = teamId;
        this.playerIndex = playerIndex;
    }

    @Column(name="TEAM_ID", nullable=false)
    public Integer getTeamId() {
        return this.teamId;
    }

    @Column(name="PLAYER_INDEX", nullable=false)
    public Integer getPlayerIndex() {
        return this.playerIndex;
    }

    // getters and setters

    public boolean equals(Object o) {
        if(o == null)
            return false;

        if(!(o instanceof PlayerId))
            return false;

        PlayerId other = (PlayerId) o;
        if(!(getTeamId().equals(other.getTeamId()))
            return false;

        if(!(getPlayerIndex().equals(other.getPlayerIndex()))
            return false;

        return true;
    }

}

But when i call (Take a special look at addPlayer)

Team team = new Team();

Player player = new Player();

// teamId property in PlayerId references Team id
team.addPlayer(player);

session.save(team);

And i see the database i get

PLAYER
TEAM_ID     PLAYER_INDEX
-1          0

TEAM
ID
1

And when i compare after saving

AssertEquals(team.getId(), player.getPLayerId().getTeamId()); // It does not works! why ?

log outputs

INSERT INTO TEAM VALUES()

// SELECT BEFORE SAVING OR UPDATING WHEN USING A COMPOUND PRIMARY KEY
SELECT * FROM PLAYER WHERE TEAM_ID = ? AND PLAYER_INDEX = ?

INSERT INTO PLAYER (TEAM_ID, PLAYER_INDEX) VALUES (?, ?)

So do you know why Hibernate lost Team’s id reference in Player’s teamId property after saving a team and its players ?

  • 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-12T14:01:29+00:00Added an answer on May 12, 2026 at 2:01 pm

    I’m not sure what you mean by “maintain references”? I’m also not quite clear on what you’re trying to do here…

    Did you expect Hibernate to use the TEAM_ID of newly created Team entity (e.g. 1 in this case) to insert Player instances? If so, you’ve specifically prevented that from happening by specifying insertable / updatable as false in @JoinColumn declaration – you need to move them to getTeamId() method within Player class instead.

    Can you clarify what you’re trying to achieve here?

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

Sidebar

Related Questions

take a look at this example code: public class Comment { private Comment() {
Please take a look at following code: public class SomeEntity { public int Id
Please take a look at the following simple code: class Foo { public: Foo(){}
Take a look at this code: public class Test { public static void main(String...
Please take a look on the following example: class Base { protected: int m_nValue;
Please take a look at the following code: Public Sub Method(Of TEntity As EntityObject)(ByVal
probably a no-brainer, but please take a look at the following classes / Interfaces:
please take a look at the following snippet: public IEnumerable<T> Query(Expression<Func<T, bool>> filter) {
Take a look at the following program: class Test { List<int> myList = new
I have the following class: class IndexItem { private String word; private HashMap<String, Integer>

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.