one to one - Hibernate One-To-One mapping error with reference key was null -


i have 2 class customer , passport 1-1 relationship. want passport table store it's owner id (customer_id). after save database customer_id alway null, wrong configure.

customer class

@entity public class customer {     private int id;     private string name;     private passport passport;      @id     @generatedvalue(strategy = generationtype.identity)     public int getid() {         return id;     }      @onetoone(cascade = cascadetype.all, mappedby="customer")     public passport getpassport() {         return this.passport;     } } 

passport class

@entity public class passport {      private int id;     private string name;      private customer customer;     @id     @generatedvalue(strategy=generationtype.identity)     public int getid() {         return id;     }      @onetoone     @joincolumn(name="customer_fk")     public customer getcustomer() {         return customer;     } } 

unit test class

public class customertest extends basetest {     @autowired     private icustomerservice customerservice;      @test     public void savetest() {         customer customer = new customer();         customer.setname("nguyễn thành trung");         passport passport = new passport();         passport.setname("trung passport");         customer.setpassport(passport);         customerservice.save(customer);     } }  hibernate: insert public.customer (name) values (?) hibernate: insert public.passport (customer_fk, name) values (?, ?) 

and result in postgresql tables

customer table

|--id---|--------name--------|
|---1---|-nguyễn thành trung-|

passport table

|--id---|--------name--------|----customer_fk---|
|---1---|-nguyễn thành trung-|------------------|

you can see customer_fk null , broblem. sorry bad english.

your unit test needs

passport.setcustomer(customer); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -