我有一个包含预填充数据的现有表Action,并且我试图保存一个对象Stop,该对象具有Action.Id作为外键。以下是我尝试过的代码。但是在Stop表中,即使没有错误,引用也不会得到保存。
public class Stop {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ACTION_ID", updatable = false, insertable = false)
private Action action;
}
public class Action {
@Id
@Column(name = "ACTION_ID")
private String actionId;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ACTION_ID")
private Stop stops;
}
我尝试显式调用Optional<Action> action = actionRepo.findByActionId("ARRIVE")
并将其设置为stop对象。但这并不能与参考一起保存。
答案 0 :(得分:0)
为JPA对象设置新值后,您需要调用save
Optional<Action> action = actionRepo.findByActionId("ARRIVE");
if (action.isPresent()){
actionRepo.save(action.get().setActionName("balal"));
}
答案 1 :(得分:0)
我认为您需要从Action类中删除@JoinColumn批注。通常,您两面都不需要它。
public class Stop {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ACTION_ID", updatable = false, insertable = false)
private Action action;
}
public class Action {
@Id
@Column(name = "ACTION_ID")
private String actionId;
@OneToOne(fetch = FetchType.LAZY)
private Stop stops;
}