这是主意-
1.人员表存储人员的详细信息及其地址,
2.城市和州有各自的表格,
3.地址表包含有关城市以及城市所属州的数据,
4.联系人表包含联系人号码,其中一个人可以具有多个联系人号码。
以下是MySql Table模式设计-

1.人员
java - Java Hibernate表映射-LMLPHP

2.地址
java - Java Hibernate表映射-LMLPHP

3.城市
java - Java Hibernate表映射-LMLPHP

4.状态
java - Java Hibernate表映射-LMLPHP

5.联系方式
java - Java Hibernate表映射-LMLPHP


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

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. AddressDomain

    @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. CityDomain

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

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



4. StateDomain

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

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



5. ContactDomain

    @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中正确映射了关系。

最佳答案

您不能将@ManyToOne用于Set,例如,您曾经使用过的PersonsDomain

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


而不是@ManyToOne,您需要替换为@OneToMany
另外,您还需要从此处删除@JoinColumn,因为在您的情况下,关系是一个方向的(PersonsDomainAddressDomain),并且PersonDomain有很多AddressDomain,因此您不能在这种情况下使用它。
另外,您已经在CityID中使用了引用的未知属性AddressDomain。在@OneToMany mappingBy字段中使用拥有关系的对象。仅在关联的反(非所有权)侧指定此元素。
请从下面将其删除:

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


同样好的做法是使用对象(实体)名称代替数据库ID列来表示对象,因为您已经使用ContactDomain->

 private PersonsDomain personId;


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

我已更新您在描述中提及的实体:
人员域:

@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;

}


CityDomain:

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

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


ContactDomain:

@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;
}


StateDomain:

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

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


另外,citydmoin具有很多statedomin,所以您可以建立这种关系,这里又是另一件事,但这取决于您的要求,因此您需要根据需要更改这些内容。
同样,按照您的mysql模式用户有一个地址,但是您在address中使用Set of persondomin的情况,因此请确保Person在多个地址上有一个地址,更改它会增加您的要求。

07-28 11:55