我有4张桌子:
我有一个实体,其中包含Set<Modul>
-> Modul
与User
链接,例如@OneToMany
,Modul
包含{{ 1}} -> 也是Set<ModuleRule>
。
我尝试使用@OneToMany
来将Spring-Data-JPA-Mapping
与Users
和Modules
链接起来:
Modules Rules
,模块实体:
@Entity
@Table(name = "Users")
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private int id;
@OneToMany
@JoinTable(
name = "j_Users_Modules_Rule",
joinColumns = {
@JoinColumn(name="id_Module",referencedColumnName="id"),
},
inverseJoinColumns = {
@JoinColumn(name="id_rule",referencedColumnName="id")
}
)
@ElementCollection(fetch = FetchType.EAGER)
private Set<Module> authorities;
// Getters and Setters
}
和ModuleRule实体:
@Entity
@Table(name = "Modules")
public class Module implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private long id;
@Column(name = "name", nullable = false)
private String name;
@OneToMany
@JoinTable(
name = "j_Users_Modules_Rule",
joinColumns = {
@JoinColumn(name="id_Module",referencedColumnName="id"),
},
inverseJoinColumns = {
@JoinColumn(name="id_rule",referencedColumnName="id")
}
)
@ElementCollection(fetch = FetchType.EAGER)
private Set<ModuleRule> moduleRules;
//Getters and Setters
}
我(根据业务要求)正确地使用 Modules 和 ModuleRules 获得了 User ,但是当我保存@Entity
@Table(name = "Modules_access_rules")
public class ModuleRule implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private long id;
@Column(name = "name", nullable = false)
private String name;
// Getters and Setters
}
时,结果是 j_Users_Modules_Rule 表中的错误:
我了解我两次连接了表( User -> Module , Module -> ModuleRule ),但是我不知道 j_Users_Modules_Rule 表中的User
值如何正确。
我还没有找到解决方案,请使用
save
(使用Spring JdbcTemplate
处理结果集)。
查询:
ResultSetExtractor