JPA Using GenerationType.IDENTITY @onetomany Postgres Child (many) table insert fails: Not Null Constraint Error On Parent FK

StackOverflow https://stackoverflow.com/questions/10525650

Question

Given a team has many players. Using JPA/Hibernate. Postgres 9.1. GenerationType.IDENTITY (Postgres uses sequences on int columns like Oracle). On a new insert when I em.persist() I get a not-null error on the child table (player table in my very, very slightly obfusicated code... the names were changed to protect the innocent. :) Error info below.

Parent Table

@Entity
@Table(name = "team")
public class Team implements Serializable {
    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column(name = "teamid")
    private Long teamid;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "teamid", fetch = FetchType.LAZY)
    private Collection<Player> playerList;

Child Table

@Entity
@Table(name = "player")
@XmlRootElement
public class Player implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "playerid")
    private Long playerid;
    @JoinColumn(name = "teamid", referencedColumnName = "teamid")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Team teamid;

persistence.xml for good measure:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="ELS_Soulard_PU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/ElsDev1Pool</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

Error Info:

INFO: THROWABLE INFO: org.postgresql.util.PSQLException: ERROR: null value in column "teamid" violates not-null constraint
INFO: Team Controller persistence exception THROWABLE MESSAGE: could not insert: [com.mydomainblahblahblah.persistence.entity.Player]

Note that if I leave the player out of the equation, i.e. in the 'Team' class the value of the player attribute is set to null (I comment out the code that adds the player list to the attribute), data is inserted successfully into the team table. The problem is all on the players. (Just ask the owners of the Toronto Maple Leafs. :p).

Could it be the GenerationType.IDENTITY causing the problem? i.e. The teamid from the parent table is not being returned before the transaction ends so it can't be placed in the player table as the FK? If so, are there any remedies, other than using a different generation type? I like to use the database sequence (more control imho).

Would a sequence generationtype be better or the same issue? But I'm sure there has to be some way to do this. Isn't there? :/


Changed the IDs to be like this (i.e. let hibernate generate them):

@Id
@Column(name = "teamid")
private Long teamid;

Now I'm getting this error:

INFO: EXCEPTION CLASS NAME: javax.persistence.PersistenceException
INFO: THROWABLE CLASS NAME: org.hibernate.id.IdentifierGenerationException

ugh!

Était-ce utile?

La solution

So the only thing wrong here was my understanding on how table joins and JPA type ORM works (I previously worked with MyBatis so still learning JPA). It turns out even though intuitively you would think that I could add data to my team class, including an arraylist of my players and then persist it, that you can't. You have to first persist the team, then add the team to each of the players in turn and then persist them one at a time. So in essence, there is nothing wrong with this code. The problem lay between the chair and the computer. :/ It has since been corrected.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top