• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Value for JoinColumn in ManyToOne is getting inserted as Null

 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Q1) Null value is getting inserted for a @JoinColumn in case @ManytoOne RelationShip.

Relation:One User can have Many Vehicles;



Vehicles.java





UserDetail.Java






HibernateTests.java





UserDetail table output

userId User
1 First User




vehicles table output

VehicleId VehicleName user_id
1 Car Null
2 Jeep Null


Q2)Please help me why null is getting is inserted?





 
ntumba lobo
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess would be that this due to the fact that you save Vehicule and UserDetail in the same transaction while both are still transient objects.
Hibernate resolves the associations with persistent objects.

What I would suggest is for you to persist first UserDetail and flush or commit.
After attach to your new Vehicule the persistent UserDetail and then persist vehicule flush/commit.

in theory that should work
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic