【问题标题】:JPA join 2 Entity based upon 1 common entityJPA join 2 Entity based on 1 common entity
【发布时间】:2020-10-09 16:23:13
【问题描述】:

我有 3 个表 - Country、Region 和 CountryRegion。

  1. 国家 - countryId(PK), countryName
  2. 区域 - regionId(PK), regionName
  3. CountryRegion - countryId(FK)、regionId(FK)

我想在获取地区时获取国家/地区列表。喜欢:

*

{
regionId:11
regionName:"abc"
countries: [
{countryId:"67",
countryName:"us"
},
{...}
]
}

不需要 CountryRegion 表,而是使用旧数据库。

我已经实现了:


    package com.example.demo.model;
    
    import javax.persistence.*;
    import java.util.ArrayList;
    import java.util.List;
    
    @Entity
    @Table(name = "country")
    public class Country {
    
        @Id
        @Column(name = "countryid")
        private int countryId;
        @Column(name = "countryname")
        private String countryName;
    
        @OneToMany(fetch = FetchType.EAGER, mappedBy = "country")
        private List<CountryRegion> regions = new ArrayList<>();
    
        public int getCountryId() {
            return countryId;
        }
    
        public void setCountryId(int countryId) {
            this.countryId = countryId;
        }
    
        public String getCountryName() {
            return countryName;
        }
    
        public void setCountryName(String countryName) {
            this.countryName = countryName;
        }
    
        public List<CountryRegion> getRegions() {
            return regions;
        }
    
        public void setRegions(List<CountryRegion> regions) {
            this.regions = regions;
        }
    }

    package com.example.demo.model;
    
    import com.fasterxml.jackson.annotation.JsonIgnore;
    
    import javax.persistence.*;
    import java.util.ArrayList;
    import java.util.List;
    
    @Entity
    @Table(name = "region")
    public class Region {
    
        @Id
        @Column(name = "regionid")
        private int regionId;
        @Column(name = "regionname")
        private String regionName;
    
        @OneToMany(mappedBy = "region")
        private List<CountryRegion> countries = new ArrayList<>();
    
        public int getRegionId() {
            return regionId;
        }
    
        public void setRegionId(int regionId) {
            this.regionId = regionId;
        }
    
        public String getRegionName() {
            return regionName;
        }
    
        public void setRegionName(String regionName) {
            this.regionName = regionName;
        }
    
        public List<CountryRegion> getCountries() {
            return countries;
        }
    
        public void setCountries(List<CountryRegion> countries) {
            this.countries = countries;
        }
    }

    package com.example.demo.model;
    
    
    import com.fasterxml.jackson.annotation.JsonBackReference;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    
    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.List;
    
    @Entity
    @Table(name = "countryregion1")
    public class CountryRegion implements Serializable {
    
        @Id @ManyToOne
        @JoinColumn(name = "regionid", referencedColumnName = "regionId")
        private Region region;
    
        @Id @ManyToOne
        @JoinColumn(name = "countryid", referencedColumnName = "countryId")
        private Country country;
    
        public Region getRegion() {
            return this.region;
        }
    
        public void setRegion(Region region) {
            this.region = null;
        }
    
        public Country getCountry() {
            return this.country;
        }
    
        public void setCountry(Country country) {
            this.country = country;
        }
    }

    package com.example.demo.dao;
    
    import com.example.demo.model.Country;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Repository
    public interface CountryRepository extends CrudRepository<Country, Integer> {
    
        @Override
        List<Country> findAll();
    }

    package com.example.demo.dao;
    
    import com.example.demo.model.Country;
    import com.example.demo.model.Region;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Repository
    public interface RegionRepository extends CrudRepository<Region, Integer> {
    
        @Override
        List<Region> findAll();
    }
  • 控制器和服务公开 api 来获取数据。

上面的代码有问题,因为我只想获取国家/地区列表(或地区,以防国家/地区)

此代码导致 JSON 中的无限循环。尝试过 Json Ignore 等,但在这种情况下,我在列表中没有得到任何价值。

TIA !!

【问题讨论】:

    标签: java database hibernate spring-data-jpa hibernate-mapping


    【解决方案1】:

    首先你应该指定国家和地区之间的关系。我认为您不需要定义 CountryRegion 类。如果关系是 ManyToMany,那么您可以在 Region 类中使用以下代码:

    @ManyToMany(cascade = { CascadeType.ALL })
    @JoinTable(
        name = "Employee_Project", 
        joinColumns = { @JoinColumn(name = "regionid") }, 
        inverseJoinColumns = { @JoinColumn(name = "countryid") }
    )
    Set<Country > countries = new HashSet<>();
    

    如果是 OneToMany,那么您在 Region 类中实现的合理性就足够了。但我认为您希望 ManyToMany 符合您的愿望

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 2021-05-17
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多