Majid
Majid

Reputation: 664

Insert using java hibernate

I've got a client

@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;

Whenever I create a new client this way:

Client c = new Client();
c.setCityId(new City());

and persist it. The city isn't persisted also if it doesn't exist. Wouldn't the city be persisted also unless the City object has got an Id ?

Upvotes: 0

Views: 61

Answers (1)

cporte
cporte

Reputation: 2191

You have to enable cascading :

@JoinColumn(name = "vl_id", referencedColumnName = "vl_id")
@ManyToOne(cascade=CascadeType.PERSIST)
private City cityId;

Upvotes: 2

Related Questions