我有这样的阶级结构
CarElementClasses
Integer id
@OneToMany(mappedBy = "carElementClass")
private List<CarClassJoins> carClassJoins;
CarClassJoins
Integer id
@ManyToOne
@JoinColumn(name = "car_element_class_id", referencedColumnName="id")
private CarElementClasses carElementClass;
@ManyToOne
@JoinColumn(name = "car_param_id", referencedColumnName="id")
private CarParams carParam;
private String default;
CarParams
Integer id
String name
@OneToMany(mappedBy = "carParam")
private List<CarClassJoins> carClassJoins;
CarElementClasses与carparam有多对多的关系,CarClassJoins是一个表关联。
当显示
CarElementClasses
时,用户可以修改、删除CarClassJoins
。我不知道如何管理保存,如果用户添加和删除的价值。
从客户端,
添加新的时,
CarClassJoins
的id为空。。。但其中一个要删除的用户未发送。我需要在
CarElementClasses
上保存。jpa中是否有一种策略,每次都自动地重新创建关系的所有元素?
最佳答案
试试下面的。
CarElementClasses
Integer id
@OneToMany(mappedBy = "carElementClass", cascade = CascadeType.ALL)
private List<CarClassJoins> carClassJoins;
CarClassJoins
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id
@ManyToOne
@JoinColumn(name = "car_element_class_id", referencedColumnName="id")
private CarElementClasses carElementClass;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "car_param_id", referencedColumnName="id")
private CarParams carParam;
private String default;
cascade选项将把对实体所做的操作链接到相关实体。mor信息:https://en.wikibooks.org/wiki/Java_Persistence/Relationships#Cascading
当实体持续存在时,@ID和@GeneratedValue组合将生成ID。