java - 如何只插入连接表而不插入 JPA Hibernate 中的引用表

标签 java mysql spring hibernate jpa

我有两个实体 UserRole。一个用户可以有多个角色,每个角色可以关联多个用户。即,用户角色具有多对多关系

例如:用户 RamRobJohn 是管理员

我正在用户和角色之间创建一个连接表来存储信息。

@Entity
@Table(name = "account")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id")
    private int id;

    @Column(unique = true)
    private String username;

    @Transient
    @JsonIgnore
    private String password;

    @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;

    @Column
    private boolean deleted;

}

和Role.java

@Entity
@Table(name = "role")
@Data
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="role_id")
    private int id;
    @Column(name="role",unique=true)
    private String role;

}

在 spring boot 中执行时,将创建以下表:accountuser_rolerole

因为我的角色数量是固定的。我手动插入了它们

INSERT INTO `role` VALUES (1,'ADMIN');
INSERT INTO `role` VALUES (2,'USER');

现在从我的 java spring boot 应用程序中,我尝试在帐户表中插入一个新条目。还尝试在 Role 表中插入一个新条目...我只想将其插入到 accountuser_role 表中,但是不在角色表中...

谁能告诉我怎么做?

更新:

这里是账户插入部分(创建账户代码)

//创建账户

public Account createAccount(AccountDto account) {

        Account newAccount = new Account();
        newAccount.setPassword(account.getPassword());
        newAccount.setUsername(account.getUsername());
        Set<Role> roles = new HashSet<Role>();
        Role role = new Role();
        role.setRole(account.getRole());
        roles.add(role);
        newAccount.setRoles(roles);
        Account savedAccount = save(newAccount);
        return savedAccount;
    }

//更新账户

public Account updateAccount(String oldUserName, AccountDto accountDto) {
        Account account = accountRepository.findByUsername(oldUserName);
        if (account != null) {
            Set<Role> roleset = new HashSet<Role>();
            if(accountDto.getRole() != null && !accountDto.getRole().isEmpty()){
                Role role = roleRepository.findByRole(accountDto.getRole());
                if (role == null) {
                    roleset.add(role);
                }
            }
            account.setRoles(roleset);
            account.setUsername(accountDto.getUsername());
            account = accountRepository.save(account);
            return account;
        } else
            return null;
    }

最佳答案

尝试在 Account 类中完成 CascadeType。

@ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.)

您还需要为帐户实体添加链接。

@ManyToMany(fetch = FetchType.EAGER,
        cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
        },
        mappedBy = "role")
private Set<Account> accounts = new Set<Account>();

您可以在这里找到不错的教程 https://www.callicoder.com/hibernate-spring-boot-jpa-many-to-many-mapping-example/

关于java - 如何只插入连接表而不插入 JPA Hibernate 中的引用表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51788433/

相关文章:

java - 部署依赖于独立应用程序的 Web 应用程序

mysql - 是否可以默认将数据从 MySQL Workbench 预加载到目标数据库?

php - 复杂的条件 SQL 语句

java - 使用 BATCH 从文件夹运行 WebApp

java - 如何检查空数组java

java - AspectJ - 针对 Controller 请求方法不起作用的建议

java - 计算最短马距离的算法(国际象棋)

mysql - SQL-Query 在应该返回一个的地方返回 2 个返回值

Java Spring 3.0.5 安全性 - 只读用户

spring - 何时在 Springs 中使用应用程序上下文。