【问题标题】:Subgraphs in hibernate休眠中的子图
【发布时间】:2021-11-24 19:43:30
【问题描述】:

我有 4 个类:Country、Prefecture、District 和 Cities,我有一个要求,即每当我持久化一个 Country 时,该对象都会与它生成所有必要的关系。

这是对象的代码:

@Entity
@Table(name = "countries")
@Getter
@Setter
@AllArgsConstructor
@NamedEntityGraph(name = "graph.prefectures", attributeNodes = @NamedAttributeNode(value = "prefectures"))

@NamedEntityGraph(
        name = "graph.CountryPrefectureDistrictCities",
        attributeNodes = @NamedAttributeNode(value = "prefectures", subgraph = "subgraph.prefectures"),
        subgraphs = {
                @NamedSubgraph(name = "subgraph.prefectures",
                        attributeNodes = @NamedAttributeNode(value = "district", subgraph = "subgraph.district")),
                @NamedSubgraph(name = "subgraph.district",
                        attributeNodes = @NamedAttributeNode(value = "cities"))
        })
public class Country implements Serializable {

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

    @Column(name = "CountryName")
    private String countryName;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH, mappedBy = "country", orphanRemoval = true)
    private Set<Prefectures> prefectures = new HashSet();

    public Country() {
    }
}

@Entity
@Table(name = "prefectures")
@Getter
@Setter
@AllArgsConstructor
public class Prefectures implements Serializable {

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

    @Column(name = "PrefectureName")
    private String prefectureName;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH, mappedBy = "prefectures", orphanRemoval = true)
    private Set<District> district = new HashSet();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "countryID", updatable = false, nullable = false)
    @JsonIgnore
    private Country country;

    public Prefectures() {
    }
}
@Entity
@Table(name = "district")
@Getter
@Setter
@AllArgsConstructor
public class District implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonProperty("districtID")
    private Long districtID;

    @Column(name = "DistrictDescription")
    @JsonProperty("districtDescription")
    private String districtDescription;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH, mappedBy = "district", orphanRemoval = true)
    @JsonProperty("cities")
    private Set<City> cities = new HashSet();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PrefectureID", updatable = false, nullable = false)
    @JsonIgnore
    private Prefectures prefectures;

    public District() {
    }
}
@Entity
@Table(name = "city")
@Getter
@Setter
@AllArgsConstructor
public class City implements Serializable {

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

    @Column(name = "CityDescription")
    private String cityDescription;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DistrictID", updatable = false, nullable = false)
    @JsonIgnore
    private District district;

    public City() {
    }

这是存储库

@Repository
public interface CountryRepository extends JpaRepository<Country, Long>{

    @EntityGraph(value = "graph.CountryPrefectureDistrictCities", type = EntityGraph.EntityGraphType.LOAD)
    Optional<Country> findById(Long id);
}

我的问题是它无法识别 Country 的 @NamedEntityGraph 的子查询中的属性 cities subgraph

Queries

【问题讨论】:

  • 你试过执行代码吗?如果是这样,错误是什么?
  • 项目运行,端点工作,但正如您在新添加的图像中看到的那样,它向数据库发送垃圾邮件以获取城市,因为它无法识别它。

标签: java spring spring-boot hibernate nhibernate


【解决方案1】:

正在识别citiesNamedAttributeNode 值。

实体类在同一个包中,我将org.springframework.boot:spring-boot-starter-data-jpa:2.5.5声明为一个依赖关系,它会传递引入org.hibernate:hibernate-core:5.4.32.Final

【讨论】:

猜你喜欢
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 2017-04-17
  • 2017-04-25
  • 2017-06-22
  • 2011-09-15
  • 1970-01-01
相关资源
最近更新 更多