【发布时间】:2019-05-06 18:06:00
【问题描述】:
想要保存一对一的主值,如下所示。 有一个地址类,其中包含一对一的城市类。 但是城市是我的主值,当我保存地址时我不想更新它。只需从 UI 的下拉列表中选择城市并将对象设置为地址并保存地址。但同时保存低于错误。
org.hibernate.TransientPropertyValueException: 对象引用了一个未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.app.fd.entity.Address.city -> com.app.fd.entity.City
@Entity
public class Address extends BaseEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CONTACT_SEQ_GEN")
@SequenceGenerator(name="CONTACT_SEQ_GEN", sequenceName = "CONTACT_SEQ", allocationSize=5)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Size(max = 50)
@NotNull
private String address1;
@Size(max = 50)
private String address2;
@Size(max = 15)
@NotNull
private String state;
@Size(max = 10)
@NotNull
private String pin;
@Size(max = 255)
private String landmark;
private Boolean deleted;
@OneToOne()
@JoinColumn(name = "city_id", insertable=false,updatable=false)
private City city;
}
@Entity
public class City {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Size(max = 30)
@Column(name = "name",unique = true)
private String name;
@Size(max = 5)
@Column(name = "code",unique = true)
private String code;
}
repository.save(address); // TransientPropertyValueException error
【问题讨论】: