Reputation: 119
I am facing a weird issue where even though all fields are set in the java object, when I save the object hibernate tries to insert null values in the fields.
When I further debugged, I saw that while merging the new entity at this line hibernate generates an empty object and sets to the target instead of setting given entity to the target. This results in insert query with null values.
Am I missing some configuration here? Below are the example entities having associations similar to my case.
class Vehicle {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
@EqualsAndHashCode.Include
private VehicleType vehicleType;
@OneToOne(mappedBy="vehicle", fetch=FetchType.LAZY)
private Car car;
@OneToOne(mappedBy="vehicle", fetch=FetchType.LAZY)
private Truck truck;
}
class Car {
@Id
private Integer id;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@MapsId
@JoinColumn(name = "vehicle_id")
private Vehicle vehicle;
...
}
class Truck {
@Id
private Integer id;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@MapsId
@JoinColumn(name = "vehicle_id")
private Vehicle vehicle;
...
}
Upvotes: 2
Views: 2040
Reputation: 1
I encountered the same problem, in my case I have an application with:
public class Claim extends BaseEntity<Integer> implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "claimdetailsid", referencedColumnName = "id")
private ClaimDetails claimDetails;
@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "beneficiaryid", referencedColumnName = "id")
private Beneficiary beneficiary;
....
}
When I saved the Claim entity, the Claim and ClaimDetails objects were inserted correctly. The other entities had all the fields null, except the id and the creation date.
I tried changing CascadeType.PERSIST to CascadeType.ALL, that solved my insert problem.
But the delete cascade doesn't work now.
Upvotes: 0