【问题标题】:JPA mapping table could not deleteJPA 映射表无法删除
【发布时间】:2017-08-08 08:08:52
【问题描述】:

我有实体 Account、Role、AccountRole。

@Entity
public class Account {
    @Id
    private String loingId;
    private String username;
    private String password;
    private String email;
    private boolean enable;

    @OneToMany(mappedBy = "account", orphanRemoval = true)
    private List<AccountRole> accountRoles = new ArrayList<>();

    public String getLoingId() {
        return loingId;
    }
    public void setLoingId(String loingId) {
        this.loingId = loingId;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public boolean isEnable() {
        return enable;
    }
    public void setEnable(boolean enable) {
        this.enable = enable;
    }
    public List<AccountRole> getAccountRoles() {
        return accountRoles;
    }
    public void setAccountRoles(List<AccountRole> accountRoles) {
        this.accountRoles = accountRoles;
    }
    public void addAccountRoles(AccountRole accountRoles) {
        if (this.accountRoles == null){
            this.accountRoles = new ArrayList<>();
        }
        this.accountRoles.add(accountRoles);
        accountRoles.setAccount(this);
    }
    public void removeAccountRoles(){
        this.accountRoles = null;
    }
}

@Entity
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String description;
    private boolean enable;

    @OneToMany(mappedBy = "role")
    private List<AccountRole> accountRoles = new ArrayList<>();

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public boolean isEnable() {
        return enable;
    }
    public void setEnable(boolean enable) {
        this.enable = enable;
    }
    public List<AccountRole> getAccountRoles() {
        return accountRoles;
    }
    public void setAccountRoles(List<AccountRole> accountRoles) {
        this.accountRoles = accountRoles;
    }
}


@Entity
public class AccountRole implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "account_id")
    private Account account;

    @ManyToOne
    @JoinColumn(name = "role_id")
    private Role role;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
    public Role getRole() {
        return role;
    }
    public void setRole(Role role) {
        this.role = role;
    }
}

创建具有角色的帐户是可以的。 更新有问题。

我想删除现有角色,仅在帐户角色更改时添加更改后的角色。但是,现有数据不会从 AccoutRole 表中删除。

我该如何解决这个问题?

springBootVersion = '1.5.3.RELEASE'

java 1.8

gradle 依赖

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')

    testCompile('org.springframework.boot:spring-boot-starter-test')

    runtime ('org.mariadb.jdbc:mariadb-java-client')
}

【问题讨论】:

  • 你想如何删除/更新数据?
  • 如何对角色进行更改?你能提供一个样本吗?
  • @XtremeBaumer 如果角色发生变化,我想删除现有角色并添加新角色。
  • @Brian public void update(Account account, Role role) { if (role != null){ AccountRole accountRole = new AccountRole(); accountRole.setAccount(account); accountRole.setRole(role); account.getAccountRoles().remove(account.getAccountRoles()); account.getAccountRoles().add(accountRole); accountRoleRepository.save(accountRole); }else{ accountRepository.save(account); } }
  • 为什么不直接更新呢?

标签: java spring jpa spring-boot mariadb


【解决方案1】:

几个想法:

想法一:尝试使用级联

是的,JPA 2.0 应该使用orphanRemoval = true 处理这个问题,但让我们看看它是否有效。我认为这不是因为您没有在这里创建孤儿。从关系的角度来看,映射仍然是“有效的”。

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL) // or CascadeType.REMOVE
private List<AccountRole> accountRoles = new ArrayList<>();

想法 2:先尝试将帐户角色设置为空的 hashmap:

account.setAccountRoles(new HashMap<AccountRole>());
account.getAccountRoles().add(accountRole);;

【讨论】:

  • 我解决了这个问题。我写@Transactional 我的测试用例。所以插入是回滚.....
猜你喜欢
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 2013-07-16
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多