hibernate 当使用@ManyToOne和@OneToMany注解时,将引发stackoverflow异常

vwhgwdsa  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(181)

似乎Hibernate的所有注解都是类中的变量之上的,但是哪天我不能摆脱stackoverflow。我也使用spring-data。
以下是例外情况:

Root Cause

java.lang.StackOverflowError
    java.base/java.lang.Integer.toString(Integer.java:1196)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)
    java.base/java.lang.String.valueOf(String.java:4215)
    java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
    java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
    org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Ticket.toString(Ticket.java:210)
    java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
    com.example.application.model.Material.toString(Material.java:166)

我在调试中看到:

舱位票

@Entity
@Table(name = "tickets")
public class Ticket extends AbstractNamedEntity implements HasId {

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "ticket")
    @OrderBy("name DESC")
    @BatchSize(size = 100)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonIgnore
    private List<Material> materials;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "user_id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @BatchSize(size = 50)
    @JsonIgnore
    private User user;
}

类别材料

@Entity
@Table(name = "materials")
public class Material extends AbstractNamedEntity implements HasId {

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "ticket_id")
  @OnDelete(action = OnDeleteAction.CASCADE)
  @JsonIgnore
  private Ticket ticket;
}

我尝试过这样的请求:

@Transactional(readOnly = true)
  public interface CrudTicketRepository extends JpaRepository<Ticket, Integer> {
     
      @EntityGraph(attributePaths = {"materials"}, type = EntityGraph.EntityGraphType.LOAD)
      @Query("SELECT t FROM Ticket t WHERE t.id=:id")                                      
      Ticket getWithMaterial(@Param("id") int id);
}

然后这个:

@Query("SELECT t FROM Ticket t JOIN FETCH Material m WHERE t.id=:id AND m.ticket.id=:id")
Ticket findById(@Param("id") int id);

我也试过FetchType。EAGER和它不工作,以改变任何东西那里(((帮助。

w46czmvw

w46czmvw1#

在您的@OneToMany属性上尝试:@ToString.Exclude

相关问题