gpt4 book ai didi

Java Hibernate 表映射

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

这是想法 -

1. Person 表存储Person的详细信息和他/她的Address,
2. 城市和州有各自的表格,
3. 地址表包含有关城市和城市所属州的数据,
4. Contact表包含Contact Numbers,一个Person可以有多个Contact numbers

以下是 MySql 表架构设计-

<强>1。人物
enter image description here

<强>2。地址
enter image description here

<强>3。城市
enter image description here

<强>4。状态
enter image description here

<强>5。联系方式
enter image description here

我为上述表格创建了以下域 -

<强>1。人员域

    @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PersonID")
private int personId;

@Column(name = "FirstName")
private String firstName;

@Column(name = "Age")
private int age;

@Column(name = "DateOfBirth")
private Date dateOfBirth;

@OneToMany(mappedBy = "PersonID", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@JsonBackReference
private Set<ContactDomain> contactDomain;

@ManyToOne
@JoinColumn(name = "AddressID", nullable = true)
@JsonBackReference
private Set<AddressDomain> addressDomain;


<强>2。地址域

    @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "AddressID")
private int addressId;

@OneToOne(mappedBy = "CityID", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
@JsonBackReference
private CityDomain cityDomain;

@ManyToOne
@JoinColumn(name = "StateID", nullable = true)
@JsonBackReference
private StateDomain stateDomain;


<强>3。城市域

    @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CityID")
private int cityId;

@Column(name = "CityName")
private String cityName;


<强>4。国域

    @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "StateID")
private int stateId;

@Column(name = "StateName")
private String stateName;


<强>5。联系域

    @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ContactID")
private int contactId;

@Column(name = "ContactNumber")
private int contactNumber;

@ManyToOne
@JoinColumn(name = "PersonID", nullable = true)
@JsonBackReference
private PersonsDomain personId;

@Column(name = "Description")
private String description;



我想知道,我是否已正确映射 Hibernate 中的关系。

最佳答案

你不能对 Set 使用 @ManyToOne,例如你使用的 PersonsDomain

 @ManyToOne
@JoinColumn(name = "AddressID", nullable = true)
@JsonBackReference
private Set<AddressDomain> addressDomain;

您需要将 @ManyToOne 替换为 @OneToMany。此外,您还需要从此处删除 @JoinColumn,因为在您的情况下,关系是单向的(PersonsDomainAddressDomain)和 PersonDomain 有很多 AddressDomain,所以您不能将它用于这种情况。此外,您还在 AddressDomain 中使用了未知的引用属性 CityID。在拥有关系的 @OneToMany mappedBy 字段中使用。此元素仅在关联的反向(非拥有)端指定。请从下面删除它:

@OneToOne(mappedBy = "CityID", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
@JsonBackReference
private CityDomain cityDomain;

此外,使用对象(实体)名称而不是数据库 Id 列来表示对象也是一个好习惯,因为您已经使用了 ContactDomain ->

 private PersonsDomain personId;

因此,如果您使用字段名称 personsDomain 而不是 personId 会更好,因为 OOP 编程 personsDomain 会产生很多场景。

我已经更新了您在描述中提到的实体:个人域:

@Entity
public class PersonsDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PersonID")
private int personId;

@Column(name = "FirstName")
private String firstName;

@Column(name = "Age")
private int age;

@Column(name = "DateOfBirth")
private Date dateOfBirth;

@OneToMany(mappedBy = "personId", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private Set<ContactDomain> contactDomain;

@OneToMany
private Set<AddressDomain> addressDomain;
}

地址域:

@Entity
public class AddressDomain {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "AddressID")
private int addressId;

@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
private CityDomain cityDomain;

@ManyToOne
@JoinColumn(name = "StateID", nullable = true)
private StateDomain stateDomain;

}

城市域名:

@Entity
public class CityDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CityID")
private int cityId;

@Column(name = "CityName")
private String cityName;
}

联系域:

@Entity
public class ContactDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ContactID")
private int contactId;

@Column(name = "ContactNumber")
private int contactNumber;

@ManyToOne
@JoinColumn(name = "PersonID", nullable = true)
private PersonsDomain personsDomain;

@Column(name = "Description")
private String description;
}

国域:

@Entity
public class StateDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "StateID")
private int stateId;

@Column(name = "StateName")
private String stateName;
}

这里还有另一件事 citydmoin 有很多 statedomin,所以你可以这个关系,这里还有另一件事,但这取决于你的要求,所以你需要根据你的要求改变这些东西。此外,根据您的 mysql 模式,用户有一个地址,但您在 persondomin 中使用一组 address,因此请确保 Person 在多个地址上有一个地址,并根据您的要求进行更改.

关于Java Hibernate 表映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43305066/

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