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

标签 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 方法。新帐户已添加,但我遇到问题:

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

控制台显示错误:

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

此错误并不完全引导我到上面向您展示的行,但该行不会执行。

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

AccessRole.java

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

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

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

造成的原因:

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)

您能给我一些建议吗?我该如何修复它?

最佳答案

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

对要从 toString 中排除的属性使用 @ToString.Exclude。对于您的情况,请在 AccessRole.java 中对帐户和组成员使用排除。

请参阅 https://projectlombok.org/features/ToString 了解更多详细信息。

关于java - LazyInitializationException - ManyToOne 包含 Eager,无法添加到 OneToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910248/

相关文章:

java - 简单的 Java 应用程序可以与 Ant 配合使用,但不能与 Maven 配合使用。我该怎么做才能纠正这个问题?

java - EntityManagerFactory 应该在应用程序关闭时关闭吗?

java - createNativeQuery 映射到 POJO(非实体)

java - 我试图避免彩票行中出现重复,即如果数字 35 位于第一彩票行中,那么它一定不能位于第二行中

java - 无法从Java客户端创建HBase模式

java - Hibernate:是否可以只保留一个 child ,这也会导致 parent 的坚持

java - 用于更新查询的 JPA @Query 注释(oracle sql 开发人员)——org.hibernate.exception.GenericJDBCException : could not execute query

java - 如何在@Query注释中获取特定城市及其所有相关学校

java - 在 JpaRepository 中,如果一个参数对于许多查询方法来说是通用的,那么如何在 Repository 和 Service 中尽可能地做到最好

java - Java中查找最大值的所有者