简体   繁体   中英

LazyInitializationException - ManyToOne contains Eager, cannot add to OneToMany

I will show you same code then I will ask a question.

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;
}

I've tried use addNew method from AccountService . New account han been added but I have a problem in line:

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

Console shows me error:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.goode.business.AccessRole.accounts, could not initialize proxy - no Session

This error not exactly directs me to line which I showed you above but this line doesn't execute.

The error is about fetching Lazy (AccessRole.accounts). I tried change it to:

AccessRole.java

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

But now console shows loop error:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError

caused by:

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

May you give me some advices how can I repair it?

The @Data annotation tell me that you are using Lombok to generate getter-setter and toString. We should exclude fields which are complex object or any collection from toString, cause the toString method generated by Lombok will call toString of all properties/fields of current object and so on..

Use @ToString.Exclude on a property you want to exclude from toString. In your case, use exclude for accounts and groupMembers in AccessRole.java.

Refer https://projectlombok.org/features/ToString for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM