Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate sets ID to null
    primarykey
    data
    text
    <p>I have a problem where Hibernate resets the IDs for entities even though I explicitly set them.</p> <p>Here are my two entities:</p> <pre><code>@Entity @Table(name="game") public class Game implements Serializable { private String token; private Set&lt;DealersCard&gt; dealersCards; @Id @Size(min=36, max=36) @Column(name="token") public String getToken() { return token; } public void setToken(String token) { this.token = token; } @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL}) public Set&lt;DealersCard&gt; getDealersCards() { return dealersCards; } public void setDealersCards(Set&lt;DealersCard&gt; dealersCards) { this.dealersCards = dealersCards; } } </code></pre> <p>and </p> <pre><code>@Entity @Table(name="dealers_card") public class DealersCard implements Serializable { private String token; private int id; public void setVisible(boolean visible) { this.visible = visible; } @ManyToOne @MapsId("token") @JoinColumn(name="token", referencedColumnName="token") public Game getGame() { return game; } public void setGame(Game game) { this.game = game; } @Id @Size(min = 36, max = 36) public String getToken() { return token; } public void setToken(String token) { this.token = token; } @Id public int getId() { return id; } public void setId(int id) { this.id = id; } } </code></pre> <p>When I create an instance of Game with a two DealersCard assigned through EntityManager::persist the objects are created in the database correctly and I can retrieve them later, but if I retrieve an instance of Game, add another instance of DealersCard and try to update the database with EntityManager::merge, Hibernate, for some reason, sets the token field to null and id to 0 and I get </p> <blockquote> <p>javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: ERROR: null value in column "token" violates not-null constraint Detail: Failing row contains (null, 0).</p> </blockquote> <p>I tried MySQL and PostgreSQL but I still encounter the same error. I am running Glassfish 3.1.2.2, Hibernate Entity Manager 4.1.2, Java EE 6.</p> <p>Any help is appreciated.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload