Java 春靴休息冬眠-获得一个国家的所有城市

Java 春靴休息冬眠-获得一个国家的所有城市,java,hibernate,rest,spring-boot,jpa,Java,Hibernate,Rest,Spring Boot,Jpa,使用此代码: City.java @Entity @Table(name = "cities") public class City extends AuditModel { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotNull @NotBlank @Column(name = "name") @Size(min = 3, ma

使用此代码:

City.java

@Entity
@Table(name = "cities")
public class City extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name")
    @Size(min = 3, max = 250)
    private String name;

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

    // Getters and Setters
    ...
}
@Entity
@Table(name = "countries")
public class Country extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name", unique = true)
    @Size(min = 3, max = 150)
    private String name;

    // Getters and Setters
    ...
}
@Repository
public interface CityRepository extends JpaRepository<City, Long> {
}
@Repository
public interface CountryRepository extends JpaRepository<Country, Long> {
}
@GetMapping("/cities")
public Page<City> getAllCities(Pageable pageable) {
    return cityRepository.findAll(pageable);
}
Country.java

@Entity
@Table(name = "cities")
public class City extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name")
    @Size(min = 3, max = 250)
    private String name;

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

    // Getters and Setters
    ...
}
@Entity
@Table(name = "countries")
public class Country extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name", unique = true)
    @Size(min = 3, max = 150)
    private String name;

    // Getters and Setters
    ...
}
@Repository
public interface CityRepository extends JpaRepository<City, Long> {
}
@Repository
public interface CountryRepository extends JpaRepository<Country, Long> {
}
@GetMapping("/cities")
public Page<City> getAllCities(Pageable pageable) {
    return cityRepository.findAll(pageable);
}
CityRepository.java

@Entity
@Table(name = "cities")
public class City extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name")
    @Size(min = 3, max = 250)
    private String name;

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

    // Getters and Setters
    ...
}
@Entity
@Table(name = "countries")
public class Country extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name", unique = true)
    @Size(min = 3, max = 150)
    private String name;

    // Getters and Setters
    ...
}
@Repository
public interface CityRepository extends JpaRepository<City, Long> {
}
@Repository
public interface CountryRepository extends JpaRepository<Country, Long> {
}
@GetMapping("/cities")
public Page<City> getAllCities(Pageable pageable) {
    return cityRepository.findAll(pageable);
}
@存储库
公共接口CityRepository扩展了JpaRepository{
}
CountryRepository.java

@Entity
@Table(name = "cities")
public class City extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name")
    @Size(min = 3, max = 250)
    private String name;

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

    // Getters and Setters
    ...
}
@Entity
@Table(name = "countries")
public class Country extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name", unique = true)
    @Size(min = 3, max = 150)
    private String name;

    // Getters and Setters
    ...
}
@Repository
public interface CityRepository extends JpaRepository<City, Long> {
}
@Repository
public interface CountryRepository extends JpaRepository<Country, Long> {
}
@GetMapping("/cities")
public Page<City> getAllCities(Pageable pageable) {
    return cityRepository.findAll(pageable);
}
@存储库
公共接口CountryRepository扩展了JpaRepository{
}
我需要获取一个国家的所有城市,使用此代码我可以获取数据库中的所有城市:

CityController.java

@Entity
@Table(name = "cities")
public class City extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name")
    @Size(min = 3, max = 250)
    private String name;

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

    // Getters and Setters
    ...
}
@Entity
@Table(name = "countries")
public class Country extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name", unique = true)
    @Size(min = 3, max = 150)
    private String name;

    // Getters and Setters
    ...
}
@Repository
public interface CityRepository extends JpaRepository<City, Long> {
}
@Repository
public interface CountryRepository extends JpaRepository<Country, Long> {
}
@GetMapping("/cities")
public Page<City> getAllCities(Pageable pageable) {
    return cityRepository.findAll(pageable);
}
@GetMapping(“/cities”)
公共页面getAllCities(可分页){
返回cityRepository.findAll(可分页);
}

但是,对于一个国家的所有城市来说?怎么样?

假设唯一的要求是获得一个国家的所有城市,我将颠倒您当前在城市->国家中的关系


在一个国家和一个城市之间建立一个“一个多城市”关系,这将允许您首先找到一个感兴趣的国家,然后简单地向该国家询问与之相关的所有城市。

假设唯一的要求是获得一个国家的所有城市,我将颠倒您当前与城市->国家的关系


在一个国家和一个城市之间建立一个单一的关系,可以让你首先找到一个感兴趣的国家,然后简单地向该国询问与之相关的所有城市。

我认为你不应该使用
findAll
。它意味着回报一切。根据您的要求,您应该使用命名查询,下面是一个示例

public interface CityRepository extends JpaRepository<City, Long> {
    List<City> findAllCity(Pageable pageable);

}

@Entity
@NamedQuery(
    name = City.findAllCity, query = "select * from city where country= ?"
)
public class City{
    public static final String FIND_ALL_CUSTOM = "City.findAllCity";
}
CityRepository公共接口扩展了JpaRepository{
列表FindLCITY(可分页可分页);
}
@实体
@命名查询(
name=City.findAllCity,query=“选择*国家/地区所在城市=?”
)
公营城市{
公共静态最终字符串FIND\u ALL\u CUSTOM=“City.findAllCity”;
}

我认为你不应该使用
findAll
。它意味着回报一切。根据您的要求,您应该使用命名查询,下面是一个示例

public interface CityRepository extends JpaRepository<City, Long> {
    List<City> findAllCity(Pageable pageable);

}

@Entity
@NamedQuery(
    name = City.findAllCity, query = "select * from city where country= ?"
)
public class City{
    public static final String FIND_ALL_CUSTOM = "City.findAllCity";
}
CityRepository公共接口扩展了JpaRepository{
列表FindLCITY(可分页可分页);
}
@实体
@命名查询(
name=City.findAllCity,query=“选择*国家/地区所在城市=?”
)
公营城市{
公共静态最终字符串FIND\u ALL\u CUSTOM=“City.findAllCity”;
}

根据Spring官方文档,您可以尝试以下方法

@Repository
public interface CityRepository extends JpaRepository<City, Long> {

    public List<City> findByCountryName(String countryName);
}
@存储库
公共接口CityRepository扩展了JpaRepository{
公共列表findByCountryName(字符串countryName);
}

根据Spring官方文档,您可以尝试以下方法

@Repository
public interface CityRepository extends JpaRepository<City, Long> {

    public List<City> findByCountryName(String countryName);
}
@存储库
公共接口CityRepository扩展了JpaRepository{
公共列表findByCountryName(字符串countryName);
}

您正在尝试从国家协会访问城市。因此,

@Repository
public interface CityRepository extends JpaRepository<City, Long> {

List<City> findByCountryName(String name);
Page<City> findByCountryName(String name, Pageable pageable);
List<City> findByCountryId(long id);

}
@存储库
公共接口CityRepository扩展了JpaRepository{
列出findByCountryName(字符串名称);
PageFindByCountryName(字符串名称,可分页);
列出findByCountryId(长id);
}

这将访问country对象并按国家名称查找。这是Spring数据的优雅方式和强大功能。不需要自定义查询或命名查询。:)

您正在尝试从国家协会访问该城市。因此,

@Repository
public interface CityRepository extends JpaRepository<City, Long> {

List<City> findByCountryName(String name);
Page<City> findByCountryName(String name, Pageable pageable);
List<City> findByCountryId(long id);

}
@存储库
公共接口CityRepository扩展了JpaRepository{
列出findByCountryName(字符串名称);
PageFindByCountryName(字符串名称,可分页);
列出findByCountryId(长id);
}

这将访问country对象并按国家名称查找。这是Spring数据的优雅方式和强大功能。不需要自定义查询或命名查询。:)

假设与一个国家相关的城市规模是固定的(适用于任何国家):

所以

因为您需要所有城市都与特定国家相关,所以只需在城市和国家实体之间添加双向一对多和多对一关系 因此,您可以按以下方式更改国家/地区:

 @Entity
@Table(name = "countries")
public class Country extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name", unique = true)
    @Size(min = 3, max = 150)
    private String name;

    @OneToMany(fetch = FetchType.LAZY,mappedBy = "country",nullable = false)
    private Set<City> cities ;

    // Getters and Setters
...
}
@实体
@表(name=“countries”)
公共类国家模型{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@NotNull
@不空白
@列(name=“name”,unique=true)
@尺寸(最小值=3,最大值=150)
私有字符串名称;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“country”,nullable=false)
私人城市;
//接球手和接球手
...
}
但如果citeis的大小不是固定的,并且它是一个大列表(出于性能原因),您应该尝试以下spring数据方法:

@Repository
public interface CityRepository extends JpaRepository<City, Long> {

    Page<City> findByCountry(Country country, Pageable pageable);
}
@存储库
公共接口CityRepository扩展了JpaRepository{
PageFindByCountry(国家/地区,可分页);
}

假设与一个国家相关的城市规模是固定的(适用于任何国家):

所以

因为您需要所有城市都与特定国家相关,所以只需在城市和国家实体之间添加双向一对多和多对一关系 因此,您可以按以下方式更改国家/地区:

 @Entity
@Table(name = "countries")
public class Country extends AuditModel {

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

    @NotNull
    @NotBlank
    @Column(name = "name", unique = true)
    @Size(min = 3, max = 150)
    private String name;

    @OneToMany(fetch = FetchType.LAZY,mappedBy = "country",nullable = false)
    private Set<City> cities ;

    // Getters and Setters
...
}
@实体
@表(name=“countries”)
公共类国家模型{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@NotNull
@不空白
@列(name=“name”,unique=true)
@尺寸(最小值=3,最大值=150)
私有字符串名称;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“country”,nullable=false)
私人城市;
//接球手和接球手
...
}
但如果citeis的大小不是固定的,并且它是一个大列表(出于性能原因),您应该尝试以下spring数据方法:

@Repository
public interface CityRepository extends JpaRepository<City, Long> {

    Page<City> findByCountry(Country country, Pageable pageable);
}
@存储库
公共接口CityRepository扩展了JpaRepository{
PageFindByCountry(国家/地区,可分页);
}

是的,但是这个,只返回一个城市而不是一个包含这个国家所有城市的列表,对吗?@Manu,你可以使用
列表
获取城市列表。是的,但是这个,只返回一个城市而不是包含这个国家所有城市的列表,对吗@