开发者

TransientObjectException

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....


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;


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.


Is District annotated with @Entity?
You probably need to specify cascade on @OneToOne Example: @OneToOne(cascade={CascadeType.PERSIST})


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜