首页 > 解决方案 > JPA 仅获取一级关联

问题描述

我在尝试获取存储在我的应用程序中的所有“折扣”对象时遇到问题。“折扣”是由以下类表示的对象

@Entity
@NoArgsConstructor
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "store")
public class Discount {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

// some fields like a description or a percentage providing info about a discount

@ManyToOne
@JoinColumn(name = "id_store")
private Store store;

@ManyToOne
@JoinColumn(name = "id_brand")
@JsonManagedReference
private Brand brand;

// getters, setters, equals and hashcode overriden below

如您所见,折扣对象可以属于商店或品牌。我确保在我的代码和数据库中都只设置了一个字段,并带有触发器。在我的应用程序中,折扣可以由品牌提供(因此我不必为该品牌的每个商店创建折扣)或特定商店可以提供自己的折扣。我的班级商店看起来像这样

@Entity
@NoArgsConstructor
public class Store extends Entite {

@OneToMany(mappedBy = "store", cascade = CascadeType.ALL)
private Set<Discount> discounts;

@ManyToOne
@JoinColumn(name = "id_brand", nullable = false)
private Brand brand;

// getters, setters, equals and hashcode overriden below

如您所见,商店有一组折扣并属于一个品牌。它扩展了 Entite 类,因为在我的应用程序中我也有 Sport Clubs 并且他们共享一些信息,但这与我的问题无关。最后,这是品牌类

@Entity
@NoArgsConstructor
public class Brand {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(mappedBy = "brand", cascade = CascadeType.ALL)
private Set<Store> stores;

@OneToMany(mappedBy = "brand", cascade = CascadeType.ALL)
@JsonBackReference
private Set<Discount> discounts;

// getters, setters, equals and hashcode overriden below

我的品牌类然后有一套商店和一套折扣。

我的问题是我无法通过简单的 FindAll 从我的interface DiscountRepository扩展中获取所有折扣,JpaRepository<Discount, Long>因为某些折扣是在关联中检索的。我从 FindAll 方法得到的答案如下

[
{
    // discount fields such as percentage, ruling, etc
    "store": {
        "id": 75,
        "name": "Nike Store",
        "discounts": [
            75
        ],
        "brand": {
            "name": "Nike",
            "store": [
                75
            ]
        }
    },
    "brand": null
},
{
    // discount fields such as percentage, ruling, etc
    "store": null,
    "brand": {
        "name": "Uniqlo",
        "stores": [
            90
        ]
    }
},
{
    // discount fields such as percentage, ruling, etc
    "store": null,
    "brand": {
        "name": "FNAC",
        "stores": [
            {
                "id": 76,
                // some store fields
                // problem here because Discount are retrieved here and therefore not displayed in the list, only here
                "discounts": [
                    {
                        // discount fields such as percentage, ruling, etc 
                        // for an offer that isn't present in the original list returned 
                        // by the find all method but only here, associated to this Store object
                        "store": 76,
                        "brand": null
                    }
                ],
                "brand": {
                    "name": "FNAC",
                    "stores": [
                        76
                    ]
                }
            }
        ]
    }
},
76
]

这里的问题是有一个带有 Store 的 Discount 提取,因此这个 Discount 不在方法返回的原始列表中,而是在与 Discount 关联的 Store 对象中。我尝试添加FetchType.LAZY到我的商店和折扣关联,但这并不能解决问题。因此有没有办法获取“只有一级关联”?我的意思是,我想检索我的折扣对象,但只能使用品牌和商店信息,而不是使用商店或品牌及其所有关联(如其折扣集)。我希望我已经清楚了,不要犹豫,问我任何问题。

标签: javahibernatespring-bootspring-data-jpaone-to-many

解决方案


推荐阅读