我是靠谱客的博主 跳跃香菇,最近开发中收集的这篇文章主要介绍JPA的多表关联,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

前言:JPA的多表查询分为,一对一,一对多,多对多,这之间又可分为单向和双向,本篇文章写了两种方式,一对一和多对一(双向)

数据库结构人员表(id(主键),zhiyeid(职业表的外键),name(姓名))。职业表(id(主键),gongzuo(职业))

每个人员有一个职业,每个职业有多个人员

1.一对一

人员实体类:

package cn.wiztek.wiztekDemo.entity.userDomain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
/**
* Description:用户实体类
* @author sun_jian
* @date 2019年7月19日
*/
@Entity
@Table(name = "test_user")
public class User implements Serializable{
private static final long serialVersionUID = 1280661092304688998L;
/**
* id
*/
private	Integer id;
/**
* 职业Id
*/
private Integer zhiyeId;
/**
* 姓名
*/
private String name;
/**
* 职业
* @return
*/
private ZhiYe zhiye;
@Id
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "zhiyeId")
public Integer getZhiyeId() {
return zhiyeId;
}
public void setZhiyeId(Integer zhiyeId) {
this.zhiyeId = zhiyeId;
}
//懒加载
@OneToOne(fetch = FetchType.LAZY)
//第一个参数可随便写,第二个参数为关联表的id
@JoinColumn(name = "zhiyeId",referencedColumnName="id")
public ZhiYe getZhiye() {
return zhiye;
}
public void setZhiye(ZhiYe zhiye) {
this.zhiye = zhiye;
}
}

职业实体类:

package cn.wiztek.wiztekDemo.entity.userDomain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.abdera.ext.serializer.annotation.ID;
@Entity
@Table(name = "zhiye")
public class ZhiYe implements Serializable{
private static final long serialVersionUID = -611470410803045277L;
private Integer id;
private String zhiye;
@ID
@Column(name="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="zhiye")
public String getZhiye() {
return zhiye;
}
public void setZhiye(String zhiye) {
this.zhiye = zhiye;
}
}

2.多对一

因为是多对一的关系,所以我们要站在多的角度。因为一个职业对应多个人员,所以人员是多的一方,所以在人员一方写注解@ManyToOne

一对多,因为是一对多所以要在一的一方写@OneToMany注解。

人员实体类,@JoinColumn(name = "zhiye_id")中name的意思为 "职业实体类的table名"_"id字段"

package cn.wiztek.wiztekDemo.entity.userDomain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
/**
* Description:用户实体类
* @author sun_jian
* @date 2019年7月19日
*/
@Entity
@Table(name = "test_user")
public class User implements Serializable{
private static final long serialVersionUID = 1280661092304688998L;
/**
* id
*/
private	Integer id;
/**
* 职业
*/
private ZhiYe zhiye;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 性别
*/
private String sex;
/**
* 手机号
*/
private String phone;
/**
* 身份证id
*/
private String idNumber;
/**
* 更新时间
*/
private String time;
/**
* 职业
*/
private String occupation;
/**
* 省份
*/
private String provinceDic;
/**
* 地市
*/
private String cityDic;
/**
* 城市
*/
private String city;
/**
* 头像
*/
private String headPortrait;
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "genUser")
@TableGenerator(name = "genUser", table = "test_user_strategy", pkColumnName = "keyID",
valueColumnName = "keyValue", pkColumnValue = "test_user", allocationSize = 1, initialValue = 1)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "age")
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Column(name = "sex", length = 32)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Column(name = "phone", length = 32)
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Column(name = "idNumber", length = 32)
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
@Column(name = "time")
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
@Column(name = "occupation")
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
@Column(name = "provinceDic")
public String getProvinceDic() {
return provinceDic;
}
public void setProvinceDic(String provinceDic) {
this.provinceDic = provinceDic;
}
@Column(name = "cityDic")
public String getCityDic() {
return cityDic;
}
public void setCityDic(String cityDic) {
this.cityDic = cityDic;
}
@Column(name = "city")
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Column(name = "headPortrait")
public String getHeadPortrait() {
return headPortrait;
}
public void setHeadPortrait(String headPortrait) {
this.headPortrait = headPortrait;
}
@ManyToOne
@JoinColumn(name = "zhiye_id")
public ZhiYe getZhiye() {
return zhiye;
}
public void setZhiye(ZhiYe zhiye) {
this.zhiye = zhiye;
}
}

3.职业实体类,这其中的mappedBy的值为职业类的@Table注解中的值

package cn.wiztek.wiztekDemo.entity.userDomain;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.apache.abdera.ext.serializer.annotation.ID;
@Entity
@Table(name = "zhiye")
public class ZhiYe implements Serializable{
private static final long serialVersionUID = -611470410803045277L;
private Integer id;
private String gongzuo;
private List<User> users;
@ID
@Column(name="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="gongzuo")
public String getGongzuo() {
return gongzuo;
}
public void setGongzuo(String gongzuo) {
this.gongzuo = gongzuo;
}
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="zhiye")
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}

 

 

最后

以上就是跳跃香菇为你收集整理的JPA的多表关联的全部内容,希望文章能够帮你解决JPA的多表关联所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部