Cannot make @ManyToOne relationship nullable

35,514

Solution 1

I found the solution to my problem. The way the primary key is defined in entity Customer is fine, the problem resides in the foreign key declaration. It should be declared like this:

@ManyToOne
@JoinColumn(columnDefinition="integer", name="customer_id")
private Customer customer;

Indeed, if the attribute columnDefinition="integer" is omitted the foreign key will by default be set as the source column: a not-null serial with its own sequence. That is of course not what we want as we just want the to reference the auto-incremented ID, not to create a new one.

Besides, it seems that the attribute name=customer_id is also required as I observed when performing some testing. Otherwise the foreign key column will still be set as the source column. This is a strange behavior in my opinion. Comments or additional information to clarify this are welcome!

Finally, the advantage of this solution is that the ID is generated by the database (not by JPA) and thus we do not have to worry about it when inserting data manually or through scripts which often happens in data migration or maintenance.

Solution 2

I came across this problem but I was able to solve it this way:

@ManyToOne
@JoinColumn(nullable = true)
private Customer customer;

Maybe the problem emerged from declaring @ManyToOne(optional = true)

Share:
35,514

Related videos on Youtube

vcattin
Author by

vcattin

Updated on December 10, 2020

Comments

  • vcattin
    vcattin over 3 years

    I have a many-to-one relationship that I want to be nullable:

    @ManyToOne(optional = true)
    @JoinColumn(name = "customer_id", nullable = true)
    private Customer customer;
    

    Unfortunately, JPA keeps setting the column in my database as NOT NULL. Can anyone explain this? Is there a way to make it work? Note that I use JBoss 7, JPA 2.0 with Hibernate as persistence provider and a PostgreSQL 9.1 database.

    EDIT:

    I found the cause of my problem. Apparently it is due to the way I defined the primary key in the referenced entity Customer:

    @Entity
    @Table
    public class Customer { 
        @Id
        @GeneratedValue
        @Column(columnDefinition="serial")
        private int id;
    }
    

    It seems that using @Column(columnDefinition="serial") for the primary key automatically sets the foreign keys referencing it to NOT NULL in the database. Is that really the expected behavior when specifying the column type as serial? Is there a workaround for enabling nullable foreign keys in this case?

    Thank you in advance.

  • vcattin
    vcattin about 11 years
    That's indeed a solution I have considered. Unfortunately this does not enable ID generation from the database itself, and it's a functionality I need when inserting data manually (using hibernate's import.sql for instance). Luckily I guess I am about to find a solution. I am just doing some testing and if it's successful I'll post the solution. Thanks for your help.
  • Andremoniy
    Andremoniy almost 9 years
    nullable = false?? false? Why false, not true?
  • elquimista
    elquimista over 7 years
    definitely it should be nullable = true
  • Michał Stochmal
    Michał Stochmal over 5 years
    Nullable is set to true by default, there is no need to define it.
  • Antonino
    Antonino over 5 years
    there is always a workaround in life, would be nice to know how too
  • Antonino
    Antonino over 5 years
    Nice to see the beginning of your edits in place, I had flagged the previous answer ["there is a workaround"] as not an answer. Thanks for helping but better if you do the job all at once next time to avoid flags and downvotes. Ciao!
  • stackoverflow
    stackoverflow over 5 years
    i'm a new contributor. ok . will do