简体   繁体   中英

"NULL not allowed for column 'id'" even though log says it had value bound

So, I've been bashing my head on this for hours!

The difference is that running it in JBoss a different persistence.xml is used (maven main\/test catalogue structure) that connects to the PostgreSQL server through a JTA-connection.

@Entity
@Table(name = "systemuser")
public class SystemUser extends BaseOldStyleEntity implements Serializable {
private static final long serialVersionUID = -1L;
@Id
@SequenceGenerator(name = "SYSTEMUSER_SYSTEMUSERIDNR_GENERATOR", sequenceName = "systemuser_pk_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SYSTEMUSER_SYSTEMUSERIDNR_GENERATOR")
@Column(name = "systemuseridnr", unique = true, nullable = false)
@BusinessKey
private Integer id;
...

it occured to me another possible solution for creating a correct CREATE TABLE statement.

http://mrbool.com/how-to-create-database-table-using-hibernate/28269

The great thing is that:

<property name="hbmdl.auto">update</property>

With this solution, your code will generate a statement itself. Try it!

The problem turned out to be an @Entity with a @UniqueConstraint that someone had missed completing before committing the code.

As it turns out this gave us a plethora of different behaviors and errors when generating tables for H2 & postgres through both Hibernate and EclipseLink during test phase!

We finally caught the problem by remote-debugging the the maven surefire plugin through Eclipse.

@Entity
@Table(name = "dyn_bin_constraint_group", schema = "public", 
        uniqueConstraints = { @UniqueConstraint(columnNames = {}) })
public class BinConstraintGroup implements Serializable {

The solution was simply completing the @UniqueConstraint!

@Entity
@Table(name = "dyn_bin_constraint_group", schema = "public", 
        uniqueConstraints = { @UniqueConstraint(columnNames = {BinConstraintGroup.NAME}) })
public class BinConstraintGroup implements Serializable {

Your ID column is "systemuseridnr".

  1. You'd like to fill it out automatically as you annotated in your entity with using sequence "systemuser_pk_seq" - Have you got this sequence in the DB?
  2. All the same, you'd like to fill it out directly with value "-1" - In an ID column you must not use negative.

Changing the dialect to org.hibernate.dialect.PostgreSQLDialect is solved the issue. This what recommended from h2database

When using such a compatibility mode, use the Hibernate dialect for the corresponding database instead of the H2Dialect; but please note H2 does not support all features of all databases.

https://h2database.com/html/tutorial.html?highlight=dialect&search=dialect#using_hibernate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM