单向删除子JPA / Hibernate

时间:2016-08-12 15:59:53

标签: java hibernate jpa

我想我有一个问题,其他人已经问了一些问题,但是我无法找到这个问题。

我有一个拥有n个权限的用户。现在我希望能够删除一个权限,结果应该是用户现在有(n-1)个权限。

    <xp:button
        id="buttonCancelRestore"
        value="Cancel Restore Values">
        <xp:eventHandler
            event="onclick"
            submit="true"
            refreshMode="partial"
            refreshId="panelRefresh"
            execMode="partial"
            execId="buttonCancelRestore">
            <xp:this.action><![CDATA[#{javascript:
                var docOld = document1.getDocument();
                document1.setValue("A", docOld.getItemValueString("A"));
                document1.setValue("B", docOld.getItemValueString("B"));
        }]]></xp:this.action>
        </xp:eventHandler>
    </xp:button>

删除操作如下:

@Entity
@Table(name = "user")
public class User implements Serializable {

// id is also there ...

@JsonIgnore
   @ManyToMany(fetch = EAGER)
   @JoinTable(name = "user_authority", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
              inverseJoinColumns = {@JoinColumn(name = "authority_id", referencedColumnName = "id")})
   private Set<Authority> authorities = new HashSet<>();
}


@Entity
@Table(name = "authority")
public class Authority implements Serializable {

   @Id
   @NotNull
   @Column(columnDefinition = "char(36)")
   private String id;

   @Column(length = 50)
   private String name;
}

但我得到关于参照完整性的错误(抱歉,错误信息是德语):

public void deleteById(final String authorityId) {
      final Optional<Authority> authorityToDelete = authorityRepository.findById(authorityId);
      authorityToDelete.ifPresent(a -> authorityRepository.delete(a));
   }

有人可以帮忙吗?非常感谢你!

0 个答案:

没有答案