Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JPA连接表?_Java_Jpa_Entity - Fatal编程技术网

Java 如何使用JPA连接表?

Java 如何使用JPA连接表?,java,jpa,entity,Java,Jpa,Entity,我有四张桌子 ZipCode zipCode(PK) 城市代码 城镇 城市代码(PK) 城镇名称 城市代码 城市 城市代码(PK) 城市名称 区号 县 地区代码(PK) 县名 我只想获取zipCode搜索的所有参数。 现在,我写了这段代码 @Entity public class ZipCode extends GenericModel { @Id @Column(length = 7, nullable = false) public String zipCode; @Column(lengt

我有四张桌子

ZipCode

zipCode(PK)

城市代码

城镇

城市代码(PK)

城镇名称

城市代码

城市

城市代码(PK)

城市名称

区号

地区代码(PK)

县名

我只想获取zipCode搜索的所有参数。 现在,我写了这段代码

@Entity
public class ZipCode extends GenericModel {
@Id
@Column(length = 7, nullable = false)
public String zipCode;
@Column(length = 8, nullable = false)
public String townCode;
@ManyToMany(targetEntity=Town.class)
@JoinTable(name="town")
@JoinColumn(referencedColumnName="townCode",insertable=true,table="town",name="townCode")
public Set<Town> towns;

@Entity
public class Town extends GenericModel {
@Id
@Column(length = 8, nullable = false)
public String townCode;
@Column(length = 255, nullable = false)
public String townName;
@Column(length = 5, nullable = false)
public String cityCode;
@ManyToMany(mappedBy="townCode")
public Set<ZipCode> zipCode;
@实体
公共类ZipCode扩展了GenericModel{
@身份证
@列(长度=7,可空=false)
公共字符串zipCode;
@列(长度=8,可空=false)
公共字符串代码;
@许多(targetEntity=Town.class)
@JoinTable(name=“town”)
@JoinColumn(referencedColumnName=“townCode”,insertable=true,table=“town”,name=“townCode”)
公共集镇;
@实体
公共类Town扩展了GenericModel{
@身份证
@列(长度=8,可空=false)
公共字符串代码;
@列(长度=255,可空=false)
公共字符串名称;
@列(长度=5,可空=false)
公共字符串cityCode;
@许多(mappedBy=“townCode”)
公共集zipCode;
我执行这个

List zipcodes=ZipCode.find(“ZipCode=?”,ZipCode.fetch()

表状态是

ZipCode zipCode(PK)|镇码 1111111 | 123

城镇 城市代码(PK)|城市名称|城市代码 123 |东京| 12345

我想得到所有参数,但响应只有ZipCode状态

请告诉我,如何获取所有参数


帮帮我!

就像您定义了zipCode和Town之间的映射一样,您需要提供Town和City之间以及City和Estation之间的映射

一旦你做到了这一点,你将能够获取所有城镇的zipcode,从城镇你将能够获得城市,从城市,你将能够获得县 此外,看看你的表,我看不到ZIPCODE和表之间的多对多关系,所以你可以考虑把它变成多个到一个/P>
public class Zipcode {
// all the other attributes 
// Provide the mappings via annotation
public Town town ;

}

public class Town {
// all the other attributes 
// provide the mappings via annotation
public City city ;
}

public class City {
// all the other attributes 
// Provide the mappings via annotation
public Prefecture prefecture ;

}

public class Prefecture {
// all the other attributes 

}
最后

List zipcodes = ZipCode.find("zipcode = ? ", zipcode).fetch();
foreach(Zipcode zip : zipcodes) {
  zip.getTown() // to get the town 
  zip.getTown().getCity() // to get the city 
  zip.getTown().getCity().getPrefecture()  // to get the Prefecture 
} 

如果您不打算每次都使用所有详细信息,建议使用延迟加载。

您是否为城市和地区定义了实体类?所有参数的含义是什么?抱歉,城市和地区尚未定义。所有参数都是zipCode townCode townName cityCode cityName PerfectureCode PerfectureName这些参数。谢谢Rocky先生!!但是我不知道如何使用注释…你能告诉我我应该使用什么注释吗?你可以在这里阅读如何使用注释来映射实体-你需要使用多对一映射非常感谢Rocky先生!!JPA对我来说太难了这个案子(在ZipCode和Town之间),谁是主人?Dindt很明白你的意思,你说的主人是什么意思?对不起,我错把一个公司当成了很多公司。