LazyInitializationException-ManyToOne包含Eager,无法添加到OneToMany

时间:2018-08-18 15:46:40

标签: java hibernate spring-data-jpa

我将向您显示相同的代码,然后问一个问题。

AccountService.java

@Service
public class AccountService implements IAccountService, StandardizeService<Account>{

  @Autowired
  private AccountRepository accountRepository;

  @Autowired
  private AccessRoleRepository accessRoleRepository;

  @Autowired
  private ActivationCodeRepository activationCodeRepository;

  @Autowired
  private ActivationCodeService activationCodeService;

  @Override
  public Account addNew(Account account){

    if(!validateAccount(account))
      return null;

    if(!accountRepository.findByUsernameOrEmail(account.getUsername(), account.getEmail()).isEmpty())
      return null;

    account.setEnabled(false);
    account.setAccessRole(accessRoleRepository.getAccessRoleById(3));
    account.setCreationTime(new Timestamp(new Date().getTime()));
    account.setPassword(this.hashPassword(account.getPassword()));

    Account newAccount = accountRepository.save(account);
    if(newAccount == null)
      return null;

    ActivationCode activationCode = activationCodeService.addNew(newAccount, 1);
    if(activationCode == null)
      return null;

    newAccount.setActivationCodes(activationCodeRepository.getActivationCodeById(activationCode.getId()));

    return newAccount;
  }
//other methods

ActivationCodeRepository.java

public interface ActivationCodeRepository extends CrudRepository<ActivationCode, Long> {

  List<ActivationCode> getActivationCodeById(int id);
}

Account.java

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

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

  @Column(name = "username")
  @NotNull
  @Length(min = 6, max = 15)
  private String username;

  @Column(name = "email")
  @NotNull
  @Length(min = 6, max = 100)
  private String email;

  @Column(name = "password")
  @NotNull
  @Length(min = 8, max = 100)
  private String password;

  @Column(name = "register_no")
  @NotNull
  private Integer register_no;

  @ManyToOne(fetch = FetchType.EAGER, optional = false)
  @JoinColumn(name = "id_access_role")
  @OnDelete(action = OnDeleteAction.NO_ACTION)
  @NotNull
  private AccessRole accessRole;

  @Column(name = "enabled")
  @NotNull
  private boolean enabled;

  @Column(name = "firstname")
  @NotNull
  @Length(min = 2, max = 30)
  private String firstname;

  @Column(name = "lastname")
  @NotNull
  @Length(min = 2, max = 30)
  private String lastname;

  @Column(name = "creation_time", updatable = false)
  @NotNull
  private Timestamp creationTime;

  @OneToMany(mappedBy = "account")
  private List<ActivationCode> activationCodes;

  @OneToMany(mappedBy = "account")
  private List<GroupMember> groupMembers;

}

AccessRole.java

@Table(name = "access_roles")
@Entity
@NoArgsConstructor
@Data
@AllArgsConstructor
public class AccessRole {

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

  @Column(name = "role")
  @NotNull
  private String role;

  @JsonIgnore
  @OneToMany(mappedBy = "accessRole")
  private List<Account> accounts;

  @JsonIgnore
  @OneToMany(mappedBy = "accessRole")
  private List<GroupMember> groupMembers;
}

ActivationCode.java

@Table(name = "activation_codes")
@Entity
@NoArgsConstructor
@Data
@AllArgsConstructor
public class ActivationCode {

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

  @ManyToOne(fetch = FetchType.EAGER, optional = false)
  @JoinColumn(name = "id_account")
  @OnDelete(action = OnDeleteAction.CASCADE)
  @NotNull
  private Account account;

  @Column(name = "type")
  @NotNull
  private int type;
  //1 - for activation account (enabled -> true)

  @Column(name = "code")
  @NotNull
  private String code;
}

我尝试使用 AccountService 中的 addNew 方法。已添加新帐户han,但我遇到了问题:

  

newAccount.setActivationCodes(activationCodeRepository.getActivationCodeById(activationCode.getId()));

控制台显示错误:

  

org.hibernate.LazyInitializationException:无法延迟初始化   角色的集合:com.goode.business.AccessRole.accounts,可以   未初始化代理-没有会话

此错误并不完全将我定向到上面显示的行,但此行未执行。

该错误与获取懒惰(AccessRole.accounts)有关。我尝试将其更改为:

AccessRole.java

  @JsonIgnore
  @OneToMany(fetch = FetchType.EAGER, mappedBy = "accessRole")
  private List<Account> accounts;

但是现在控制台显示循环错误:

  

org.springframework.web.util.NestedServletException:处理程序调度   失败嵌套的异常是java.lang.StackOverflowError

原因:

  

位于com.goode.business.AccessRole.toString(AccessRole.java:21)   java.base / java.lang.String.valueOf(String.java:2788)在   java.base / java.lang.StringBuilder.append(StringBuilder.java:135)在   com.goode.business.Account.toString(Account.java:26)在   java.base / java.lang.String.valueOf(String.java:2788)

可以给我一些建议,我该如何修理?

1 个答案:

答案 0 :(得分:2)

@Data注释告诉我您正在使用Lombok生成getter-setter和toString。我们应该从toString中排除复杂对象或任何集合的字段,因为Lombok生成的toString方法将调用当前对象所有属性/字段的toString,等等。

在要从toString中排除的属性上使用@ ToString.Exclude。在您的情况下,对AccessRole.java中的帐户和groupMembers使用排除。

有关更多详细信息,请参阅https://projectlombok.org/features/ToString。