🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
需要两张表:客户表(主表),联系人表(从表),一个客户对应多个联系人 ***** ### 1.`Customer `实体类 package net.youworker.onetomany.domain; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import javax.persistence.*; import java.util.HashSet; import java.util.Set; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-01-07 10:37 */ @Entity @DynamicUpdate(value = false) @DynamicInsert(value = false) @Table(name = "cst_customer") public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "cust_id", columnDefinition = "bigint(20) unsigned") private Long custId; // 客户名称 @Column(name = "cust_name", nullable = false, columnDefinition = "varchar(100) default '' comment '客户名称' ") private String custName = ""; // 客户来源 @Column(name = "cust_source", nullable = false, columnDefinition = "varchar(100) default '' comment '客户来源' ") private String custSource = ""; //客户级别 @Column(name = "cust_level", nullable = false, columnDefinition = "varchar(100) default '' comment '客户级别' ") private String custLevel = ""; //客户所属行业 @Column(name = "cust_industry", nullable = false, columnDefinition = "varchar(100) default '' comment '客户所属行业' ") private String custIndustry = ""; // 客户的联系方式 @Column(name = "cust_phone", nullable = false, columnDefinition = "varchar(12) default '' comment '客户的联系方式' ") private String custPhone = ""; // 客户地址 @Column(name = "cust_address", nullable = false, columnDefinition = "varchar(100) default '' comment '客户地址' ") private String custAddress = ""; /** * 配置客户和联系人之间的关系(一对多关系) * <p> * 使用注解的形式配置多表关系 * 1.声明关系 * * @OneToMany:配置一对关系 targetEntity:对方对象的字节码对象 * 2.配置外键(中间表) * @JoinColumn:配置外键 name:外键字段名称 * referencedColumnName:参照的是主表的主键字段名称 * <p> * 在客户实体类上(一的一方)添加了外键配置,所以对客户而言,也具备了维护外键的作用 */ // @OneToMany(targetEntity = LinkMan.class) // @JoinColumn(name = "lkm_cust_id",referencedColumnName = "cust_id") /** * 放弃外键维护权 * mappedBy:对方配置关系的属性名称 * cascade:级联配置(可以配置到主从表的映射关系的注解上) * CascadeType.ALL 所有 * MERGE 更新 * PERSIST 保存 * REMOVE 删除 */ @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL) private Set<LinkMan> linkMans = new HashSet<>(); public Long getCustId() { return custId; } public void setCustId(Long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public Set<LinkMan> getLinkMans() { return linkMans; } public void setLinkMans(Set<LinkMan> linkMans) { this.linkMans = linkMans; } } ~~~ ***** ### 二.`LinkMan`实体类 ~~~ package net.youworker.onetomany.domain; import lombok.Data; import javax.persistence.*; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-01-08 9:58 */ @Entity @Table(name = "cst_linkman") @Data public class LinkMan { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "lkm_id", columnDefinition = "bigint(20) unsigned") private Long lkmId; //联系人姓名 @Column(name = "lkm_name", nullable = false, columnDefinition = "varchar(100) default '' comment '联系人姓名' ") private String lkmName = ""; //联系人性别 @Column(name = "lkm_gender", nullable = false, columnDefinition = "varchar(100) default '' comment '联系人性别' ") private String lkmGender = ""; //联系人办公电话 @Column(name = "lkm_phone", nullable = false, columnDefinition = "varchar(100) default '' comment '联系人办公电话' ") private String lkmPhone = ""; //联系人手机 @Column(name = "lkm_mobile", nullable = false, columnDefinition = "varchar(11) default '' comment '联系人手机' ") private String lkmMobile = ""; //联系人邮箱 @Column(name = "lkm_email", nullable = false, columnDefinition = "varchar(100) default '' comment '联系人邮箱' ") private String lkmEmail = ""; //联系人职位 @Column(name = "lkm_position", nullable = false, columnDefinition = "varchar(100) default '' comment '联系人职位' ") private String lkmPosition = ""; //联系人备注 @Column(name = "lkm_memo", nullable = false, columnDefinition = "text comment '联系人备注' ") private String lkmMemo = ""; // @Column(name = "lkm_cust_id") // private Long lkmCustId=0l; /** * 配置联系人到客户的多对一关系 * 使用注解的形式配置多对一关系 * 1.配置表关系 * * @ManyToOne:配置一对多关系 targetEntity:对方对象的字节码对象 * 2.配置外键(中间表) * @JoinColumn:配置外键 name:外键字段名称 * referencedColumnName:参照的主表的主键字段名称 */ @ManyToOne(targetEntity = Customer.class) @JoinColumn(name = "lkm_cust_id", columnDefinition = "bigint(10) unsigned not null default 0 comment '所属客户id'", referencedColumnName = "cust_id") private Customer customer; } ~~~