endless hibernate query in thymeleaf for classes @OneToMeny and @ManyToOne

Tags: , ,



everybody, I’ve built myself two classes that are circulating.

additive class

@Entity
@Table(name = "additive", schema = "dbo")
@Data
@NoArgsConstructor
public class Additive{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "additive_id")
    private Integer id;

    @ManyToOne(targetEntity = AdditiveType.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "additive_type_id")
    private AdditiveType additiveType;

    private String description;
    private BigDecimal price;
}

and class with types

@Entity
@Table(name = "additive_type", schema = "dbo")
@Data
@NoArgsConstructor
public class AdditiveType{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "additive_type_id")
    private Integer id;

    @OneToMany(targetEntity = Additive.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "additive_type_id")
    private Set<Additive> additives;

    private String description;
}

when I try to display the code using thymeleaf

<div th:each="aType:${types}">
    <h2 th:text="${aType.description}">type</h2>
    <div>
        <div th:each="additive:${aType.additives}">
            <div>
                <img alt="${additive.description}"
                     src="...">
                <div>
                    <h5 th:text="${additive.description}">additive description</h5>
                </div>
                <div>
                    <form method="post"
                          th:action="...">
                        <input name="id" th:value="${additive.id}" type="hidden"/>
                        <button type="submit">select this</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

I get an endless queries to the database once from one table additive once from another additive_type, even when fetch type is marked as lazy fetch = FetchType.LAZY

How do you remedy that?

UPDATE

Unfortunately, neither @JsonIgnoreProperties nor @JsonIgnore has led to a break in the circular inquiry. I used these annotations in the rest service and it worked there. Here at thymeleaf it didn’t bring the desired effect.

Answer

I’ve gone over the problem by adding a class to the same table with a list of additions of this type. From the base class I threw out the reference to the list of add-ons.

Additive class and thymeleaf code without changes.

I copied the AdditiveType class to AdditiveTypeFull. I threw out a list of additions from the AdditiveType class. The service returns the class AdditiveTypeFull

@Entity
@Table(name = "additive_type", schema = "dbo")
@Data
@NoArgsConstructor
public class AdditiveType{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "additive_type_id")
    private Integer id;

    private String description;
}

AdditiveTypeFull class

@Entity
@Table(name = "additive_type", schema = "dbo")
@Data
@NoArgsConstructor
public class AdditiveTypeFull{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "additive_type_id")
    private Integer id;

    @OneToMany(targetEntity = Additive.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "additive_type_id")
    private Set<Additive> additives;

    private String description;
}

I realize it’s not an elegant solution, but it works.

As if somebody knew the legal solution, I’d be happy to get used to it.



Source: stackoverflow