簡體   English   中英

JPA:在三個實體之間使用多個多對多關系后,冗余的SQL插入

[英]JPA: redundant sql insertions after using multiple many-to-many relations between three entities

我有用戶,角色和權限實體,並將嵌入式數據庫用於開發目的。 用戶和角色具有多對多關系,並且角色和權限也具有多對多關系:1)用戶實體:

@Entity
@Table(name = "usr")
@JsonIgnoreProperties(ignoreUnknown = true)
public class User implements Serializable {

    private static final long serialVersionUID = 818129969599480161L;
    /**
     * Unique id for the User. "@Id" declare the parameter as the primary key
     * "@GeneratedValue" indicates JPA 2 (and behind Hibernate) which strategy
     * to use for creating a new value.
     * "GenerationType.AUTO" value allow JPA implementation to use the better way depending to the RDBMS used.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    /**
     * Login of the user. No annotation here, the parameter will be automatically mapped in the table.
     */
    private String login;
    /**
     * Password of the user. No annotation here, the parameter will be automatically mapped in the table.
     */
    private String password;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "USER_ROLES",
            joinColumns =
            @JoinColumn(name = "user_id"),
            inverseJoinColumns =
            @JoinColumn(name = "role_id"))
    private Set<Role> roles = new HashSet<>();
.....(getters and setters)

2)角色實體:

@Entity
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String roleName;

    @ManyToMany(mappedBy = "roles", fetch=FetchType.EAGER)
    private Set<User> users = new HashSet<>();

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "ROLE_PERMISSION",
            joinColumns =
            @JoinColumn(name = "ROLE_ID"),
            inverseJoinColumns =
            @JoinColumn(name = "PERMISSION_ID"))
    private Set<Permission> permissions = new HashSet<>();
    .....(getters and setters)

3)許可實體:

    @Entity
    public class Permission {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        private String permissionName;

        @ManyToMany(mappedBy = "permissions", fetch=FetchType.EAGER, cascade = {})
        private Set<Role> roles = new HashSet<>();
    .....(getters and setters)

並因為我的測試填充器:

public void afterPropertiesSet() throws Exception {
    _logger.debug("setting test uses data");

    Permission permission = new Permission();
    permission.setPermissionName("PERM_SAVE_PRODUCT");

    permissionRepository.save(permission);

    Role role = new Role();
    role.setRoleName("ROLE_ADMIN");
    Set<Permission> permissions = new HashSet<>();
    permissions.add(permission);
    role.setPermissions(permissions);

    roleRepository.save(role);

    User user = new User();
    user.setLogin("dmitro");
    user.setPassword("2424");
    user.setStatus(UserStatus.ACTIVE);
    Set<Role> roles = new HashSet<Role>();
    roles.add(role);
    user.setRoles(roles);

    userRepository.save(user);
}

我希望創建一個用戶記錄, 一個角色記錄和一個權限記錄,並將其插入到詳細信息存儲庫中。 一切看起來都很干凈(沒有任何異常),但是clien告訴我,我有兩個角色記錄和三個權限記錄,例如:

{
"links": [
],
"content": [
    {
        "links": [
            {
                "rel": "roles.Role.permissions",
                "href": "http://localhost:8080/admin/roles/1/permissions"
            },
            {
                "rel": "self",
                "href": "http://localhost:8080/admin/roles/1"
            },
            {
                "rel": "roles.Role.users",
                "href": "http://localhost:8080/admin/roles/1/users"
            }
        ],
        "roleName": "ROLE_ADMIN"
    },
    {
        "links": [
            {
                "rel": "roles.Role.permissions",
                "href": "http://localhost:8080/admin/roles/2/permissions"
            },
            {
                "rel": "self",
                "href": "http://localhost:8080/admin/roles/2"
            },
            {
                "rel": "roles.Role.users",
                "href": "http://localhost:8080/admin/roles/2/users"
            }
        ],
        "roleName": "ROLE_ADMIN"
    }
],
"page": {
    "size": 20,
    "totalElements": 2,
    "totalPages": 1,
    "number": 1
}

}

在日志中,我看到所有內容都已創建,但是為什么我還不知道為什么,請提供幫助。 日志在這里: https : //www.dropbox.com/s/orx3c9p023bxmti/log.txt?dl=0

您正在分別保存用戶,角色,權限。 這將導致多個記錄。 由於您已給出Cascade,因此所有保存的用戶對象也將保存角色對象和權限對象。

這樣嘗試

public void afterPropertiesSet() throws Exception {
_logger.debug("setting test uses data");

User user = new User();
user.setLogin("dmitro");
user.setPassword("2424");
user.setStatus(UserStatus.ACTIVE);
Set<Role> roles = new HashSet<Role>();
Role role = new Role();
role.setRoleName("ROLE_ADMIN");
Set<Permission> permissions = new HashSet<>();
Permission permission = new Permission();
permission.setPermissionName("PERM_SAVE_PRODUCT");
permissions.add(permission);
role.setPermissions(permissions);
roles.add(role);
user.setRoles(roles);
userRepository.save(user);

}

在這里看到有關層疊的信息 希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM