gpt4 book ai didi

java - 在hibernate中使用主键和外键组合映射三个表

转载 作者:行者123 更新时间:2023-12-01 12:11:31 25 4
gpt4 key购买 nike

我必须在 hibernate 中映射这些表,事实已经尝试了很多方法,但对我来说没有用,如果我可以帮助解决这个问题,我不会粘贴我的代码,因为我认为它不正确,我变化很大。

餐 table 国家

idCountry(PK)

国家名称

表区域

idRegion(PK)

idCountry(PK、FK)

区域名称

表省份

idProvinces (PK)

idRegion(PK、FK)

省份名称

希望您能帮助我,非常感谢

最佳答案

这是表映射:

@Entity
@Table(name = "COUNTRY")
public class Country implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idCountry", unique = true, nullable = false)
private int countryId;

@Column(name = "countryName", nullable = false)
private String countryName;

@OneToMany(mappedBy = "regionPk.country", cascade = CascadeType.ALL)
private Set<Region> regions = new HashSet<Region>(0);

// Generate Getters, Setters, hashCode() and equals()

}

@Entity
@Table(name = "REGION")
public class Region implements Serializable {

@EmbeddedId
private RegionPk regionPk; // RegionPk contains the composite PK

@Column(name = "RegionName", nullable = false)
private String regionName;

@OneToMany(mappedBy = "provincePk.region", cascade = CascadeType.ALL)
private Set<Province> provinces = new HashSet<Province>(0);

// Generate Getters, Setters, hashCode() and equals()

}

@Embeddable
public class RegionPk implements Serializable {

@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idRegion", unique = true, nullable = false)
private int regionId;

@ManyToOne
@JoinColumn(name = "idCountry", nullable = false)
private Country country;

// Generate Getters, Setters, hashCode() and equals()

}

@Entity
@Table(name = "PROVINCES")
public class Province implements Serializable {

@EmbeddedId
private ProvincePk provincePk; // ProvincePk contains the composite PK

@Column(name = "ProvinceName", nullable = false)
private String provinceName;

// Generate Getters, Setters, hashCode() and equals()

}

@Embeddable
public class ProvincePk implements Serializable {

@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idProvinces", unique = true, nullable = false)
private int provinceId;

@ManyToOne
@JoinColumn(name = "idRegion", nullable = false)
private Region region;

// Generate Getters, Setters, hashCode() and equals()

}

关于java - 在hibernate中使用主键和外键组合映射三个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27239857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com