Java JPA基于1个公共实体加入2个实体
我有三张表——国家、地区和国家地区Java JPA基于1个公共实体加入2个实体,java,database,hibernate,spring-data-jpa,hibernate-mapping,Java,Database,Hibernate,Spring Data Jpa,Hibernate Mapping,我有三张表——国家、地区和国家地区 国家-国家ID(主键),国家名称 Region-regionId(PK),regionName CountryRegion-countryId(FK)、regionId(FK) 我想在获取区域时获取国家列表。比如: * 不需要CountryRegion表,只需要使用遗留数据库 我已实施: package com.example.demo.model; import javax.persistence.*; import
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();
}
包com.example.demo.model;
导入javax.persistence.*;
导入java.util.ArrayList;
导入java.util.List;
@实体
@表(name=“country”)
公营国家{
@身份证
@列(name=“countryid”)
私人国际身份证;
@列(name=“countryname”)
私有字符串countryName;
@OneToMany(fetch=FetchType.EAGER,mappedBy=“country”)
私有列表区域=新的ArrayList();
public int getCountryId(){
返回countryId;
}
public void setCountryId(int countryId){
this.countryId=countryId;
}
公共字符串getCountryName(){
返回国家名称;
}
public void setCountryName(字符串countryName){
this.countryName=countryName;
}
公共列表getRegions(){
返回区;
}
公共区域(列出区域){
这个区域=区域;
}
}
包com.example.demo.model;
导入com.fasterxml.jackson.annotation.JsonIgnore;
导入javax.persistence.*;
导入java.util.ArrayList;
导入java.util.List;
@实体
@表(name=“region”)
公共类区域{
@身份证
@列(name=“regionid”)
私有区域ID;
@列(name=“regionname”)
私有字符串regionName;
@OneToMany(mappedBy=“region”)
私有列表国家=新的ArrayList();
public int getRegionId(){
返回区域ID;
}
公共无效setRegionId(int regionId){
this.regionId=regionId;
}
公共字符串getRegionName(){
返回regionName;
}
public void setRegionName(字符串regionName){
this.regionName=regionName;
}
国家/地区公共列表(){
返回国;
}
国家/地区(列表国家/地区){
这个国家=国家;
}
}
包com.example.demo.model;
导入com.fasterxml.jackson.annotation.JsonBackReference;
导入com.fasterxml.jackson.annotation.JsonIgnore;
导入javax.persistence.*;
导入java.io.Serializable;
导入java.util.List;
@实体
@表(name=“countryregion1”)
公共类CountryRegion实现了可序列化{
@Id@ManyToOne
@JoinColumn(name=“regionid”,referencedColumnName=“regionid”)
私人区域;
@Id@ManyToOne
@JoinColumn(name=“countryid”,referencedColumnName=“countryid”)
私人国家;
公共区域getRegion(){
返回此区域;
}
公共区域(区域){
this.region=null;
}
公共国家/地区{
返回这个国家;
}
国家/地区(国家/地区){
这个国家=国家;
}
}
包com.example.demo.dao;
导入com.example.demo.model.Country;
导入org.springframework.data.repository.crudepository;
导入org.springframework.stereotype.Repository;
导入java.util.List;
@存储库
公共接口CountryRepository扩展了Crudepository{
@凌驾
列出findAll();
}
包com.example.demo.dao;
导入com.example.demo.model.Country;
导入com.example.demo.model.Region;
导入org.springframework.data.repository.crudepository;
导入org.springframework.stereotype.Repository;
导入java.util.List;
@存储库
公共接口区域存储库扩展了Crudepository{
@凌驾
列出findAll();
}
- 控制器和服务公开api以获取数据
蒂娅 首先,您应该指定国家和地区之间的关系。我认为您不需要定义CountryRegion类。如果关系是ManyToMany,那么您可以在Region类中使用以下代码:
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
name = "Employee_Project",
joinColumns = { @JoinColumn(name = "regionid") },
inverseJoinColumns = { @JoinColumn(name = "countryid") }
)
Set<Country > countries = new HashSet<>();
@ManyToMany(cascade={CascadeType.ALL})
@可接合(
name=“Employee\u项目”,
joinColumns={@JoinColumn(name=“regionid”)},
inverseJoinColumns={@JoinColumn(name=“countryid”)}
)
Set countries=new HashSet();
如果是一个区域,您在区域类中实现的关系就足够了。但我认为你想要很多符合你愿望的东西
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
name = "Employee_Project",
joinColumns = { @JoinColumn(name = "regionid") },
inverseJoinColumns = { @JoinColumn(name = "countryid") }
)
Set<Country > countries = new HashSet<>();