How to use one to one/zero mapping in hibernate

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to use one to one/zero mapping in hibernate



Suppose I have two tables: Person And Vehicle



Person record can exist independently , that means, a person may or may not associated with a vehicle. But the Vehicle record can not exist without person.
In the nutshell,



Vehicle table has Person_Id has foreign key, where Person_Id is the primary key of the person table.



Now to define this relationship in hibernate I have used one to one mapping as follows:


public class Person

{

private int person_id;

@OnetoOne(mappedBy = "person", cascade = CascadeType.ALL)
private Vehicle vehicle;
}

public class Vehicle {

private int vehicle_id;

@OnetoOne(fetch = FetchType.LAZY)
@JoinColumn(name = "person_id")
private Person person;

}



Now, when I try to save the Person who has Vehicle, the person objects gets saved successfully. I just use session.save(person) and both the objects gets saved successfully.
On the other hand, if I try to save a Person, who does not have a Vehicle , i.e, the vechile object is null for that Person object. Then an exception is thrown i.e


session.save(person)



ConstraintVoilationException cannot insert null into the
Vehicle.Person_Id column.



Please help me to understand, how can I save a person object when my vehicle object is null.



This seems to be a 1 to 1/0 mappping.





Do you have NOT NULL constraint in Person_id Column?
– Shivam
17 hours ago





Yes, the database tables were created through sql scripts and not via hibernate. and there is not null constraint on person_id column.
– R Ram
13 hours ago





It is not possible to insert null in NOT NULL columns you have to remove that constraint.
– Shivam
13 hours ago




2 Answers
2



If your database schema is generated from annotations, please use a optional parameter in you OneToOne mapping in Person.



If your databse is created by hand, allow null on database side.



From: javadocs





The JavaDoc states: Whether the association is optional. If set to false then a non-null relationship must always exist. and then states: Default: true. So if they haven't defined it, surely nulls would be allowed?
– Draken
16 hours ago


Whether the association is optional. If set to false then a non-null relationship must always exist.


Default: true



Please add this annotation in vehicle class person attribute


@NotFound(action=NotFoundAction.IGNORE)





I used this, but still it is throwing the same exception
– R Ram
13 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Makefile test if variable is not empty