即使jpa子项存在,尝试重新注册时出错

gpnt7bae  于 4个月前  发布在  Spring
关注(0)|答案(1)|浏览(75)
// Service
@Transactional
    public Account joinByInputJoinAccount(InputJoinAccount inputJoinAccount) {

        Account.AccountBuilder accountBuilder = Account.builder()
                .userid(inputJoinAccount.getUserid())
                .password(passwordEncoder.encode(inputJoinAccount.getPassword()))
                .name(inputJoinAccount.getName())
                .phoneNumber(inputJoinAccount.getPhoneNumber());

        if(StringUtils.isNotEmpty(inputJoinAccount.getBirthDate())) {
            accountBuilder.birthDate(LocalDate.parse(inputJoinAccount.getBirthDate()));
        }
        accountBuilder.activated(true);

        Account account = accountBuilder.build();
        account.addAuthorities(Set.of(AccountAuthority.builder().authority(Authority.builder().authorityName("ROLE_INSTITUTE").build()).build()));

        return accountRepository.save(account);
    }


// Acount
@Entity
@Table(name = "account")
@Getter
@NoArgsConstructor
public class Account extends BaseEntity {
    ...
    @OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
    private Set<AccountAuthority> authorities = new HashSet<>();
    ...

    public void addAuthorities(Set<AccountAuthority> authorities) {
        this.authorities = authorities;
        authorities.forEach(accountAuthority -> accountAuthority.setAccount(this));
    }
}

// AccountAuthority
@Entity
@Table(name = "account_authority")
@Getter
@NoArgsConstructor
public class AccountAuthority extends BaseEntity {

    @EmbeddedId
    private AccountAuthorityId id = new AccountAuthorityId();

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

    @ManyToOne
    @JoinColumn(name = "authority_name")
    @MapsId("authorityName")
    private Authority authority;

    @Builder
    public AccountAuthority(Account account, Authority authority) {
        this.account = account;
        this.authority = authority;
    }

    @Embeddable
    @Getter
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode
    public static class AccountAuthorityId implements Serializable {
        private Long accountId;
        private String authorityName;
    }
}

// Authority 
@Entity
@Table(name = "authority")
@Getter
@NoArgsConstructor
public class Authority extends BaseEntity implements Serializable {

    @Id
    @Column(name = "authority_name", length = 50)
    private String authorityName;

    @OneToMany(mappedBy = "authority")
    private Set<AccountAuthority> accountAuthorities;

    @Builder
    public Authority(String authorityName) {
        this.authorityName = authorityName;
    }
}

字符串
enter image description here
即使jpa子项存在,尝试重新注册时出错
org.postgresql.util.PSQLException:错误代码:重复键值违反唯一约束“authority_pkey”详细信息:Key(authority_name)=(ROLE_INSTITUTE)已存在。
虽然我有jpa权限密钥,但我在尝试注册时遇到问题。我不知道为什么。请帮助我

2jcobegt

2jcobegt1#

您正在尝试插入一个新的Authority,其authorityName与数据库中已有的authorityName相同。您可以从数据库中检索一个jpa引用或实体,而不是构建一个新对象。因此,

Authority authority = Authority.builder().authorityName("ROLE_INSTITUTE").build()
account.addAuthorities(Set.of(authority));

字符串
您应该执行以下操作之一

Authority authority = entityManager.getReference(Authority.class, "ROLE_INSTITUTE");
account.addAuthorities(Set.of(authority));


Authority authority = authorityRepository.findById("ROLE_INSTITUTE");
account.addAuthorities(Set.of(authority));

相关问题