我有一个客户
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "cl_id")
private Long clId;
@JoinColumn(name = "vl_id", referencedColumnName = "vl_id")
@ManyToOne
private City cityId;
每当我以这种方式创建新客户时:
Client c = new Client();
c.setCityId(new City());
并坚持下去。如果不存在,这个城市也不会持久存在。 除非City对象有Id,否则这座城市不会被持久化吗?
答案 0 :(得分:2)
您必须启用cascading:
@JoinColumn(name = "vl_id", referencedColumnName = "vl_id")
@ManyToOne(cascade=CascadeType.PERSIST)
private City cityId;