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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:59:15+00:00 2026-06-03T17:59:15+00:00

Given a team has many players. Using JPA/Hibernate. Postgres 9.1. GenerationType.IDENTITY (Postgres uses sequences

  • 0

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!

  • 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-03T17:59:16+00:00Added an answer on June 3, 2026 at 5:59 pm

    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.

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

Sidebar

Related Questions

Part of the development team I work with has been given the challenge of
Our team has been given a legacy system for further maintenance and development. As
I work in the Systems & admin team and have been given the task
Given: unsigned int a, b, c, d; I want: d = a * b
My team has been tasked with designing a web application that is workflow driven.
As part of the team developing a custom application framework that will be given
I have a model like this: Client - which has many Locations - which
The CodePlex team has a Slack time policy, and it's worked out very well
The team I work with has recently been faced with the challenge of making
My development team has run into a design issue. I'm hoping someone can help

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.