Reputation: 65
Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@Column(name = "ID")
@GeneratedValue
private int addressid;
@OneToOne
@JoinColumn(name = "CITY_ID",nullable = true,insertable=false, updatable=false)
private City city = new City();
@OneToOne
@JoinColumn(name = "DISTRICT_ID",nullable = true,insertable=false, updatable=false)
private District district = new District();
}
Hi there, I have a class called Address and I cannot save this class in hibernate with DISTRICT_ID = null.
The error is
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: form.District
I just don't know what I am missing....
Upvotes: 1
Views: 1686
Reputation: 1536
Maybe you should consider having city_id and city_name in the same object and if you want the City and District table to be read only then you don't your city and district to be persisted.
Upvotes: 1
Reputation: 24732
If you want to save District with null DISTRICT_ID you can't use it as primary key and consequently you can't annotate it with @Id. You will have to choose different id for the District entity.
Upvotes: 0
Reputation: 242786
If you want district
to be null
, it should be actually null
rather than pointing to the new transient instance:
@OneToOne
@JoinColumn(name = "DISTRICT_ID",nullable = true,insertable=false, updatable=false)
private District district = null;
Upvotes: 2
Reputation: 10154
Is District annotated with @Entity
?
You probably need to specify cascade
on @OneToOne
Example: @OneToOne(cascade={CascadeType.PERSIST})
Upvotes: 0