1. 程式人生 > >Spring date jpa onetoone的問題,有關一對一外來鍵的寫法

Spring date jpa onetoone的問題,有關一對一外來鍵的寫法

 最近在寫spring boot專案的時候遇到了有關spring date jpa的一些問題,其中對@OneToOne,一對一外來鍵的設定不怎麼理解,百度了很多都不成功,最後通過查閱資料看到了如何來寫這個一對一的外來鍵,話不多說,直接上程式碼。

package com.b505.entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
 * <p>
 * B505資訊科學研究所
 * </p>
 * 
 * @Description 使用者個人資訊實體
 * @Creat date 2018-9-5 21:18
 * @author yulin
 */
@Entity
@Table(name = "userinfo")
public class Userinfo {
	@Id
	@GeneratedValue(generator = "a_native")
	// 指定主鍵生成策略,預設為自增
	@GenericGenerator(name = "a_native", strategy = "native")
	private int id;
	private String username;
	private String password;
	private String telephone;
	private String truename;
	private String schoolname;
	private String classnumber;
	private String studeynumber;
	// 外來鍵
	@OneToOne
	@JoinColumn(name="open_id",referencedColumnName="id")
	private SNSUserInfo sNSUserInfo;



	public SNSUserInfo getsNSUserInfo() {
		return sNSUserInfo;
	}

	public void setsNSUserInfo(SNSUserInfo sNSUserInfo) {
		this.sNSUserInfo = sNSUserInfo;
	}


	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getTruename() {
		return truename;
	}

	public void setTruename(String truename) {
		this.truename = truename;
	}

	public String getSchoolname() {
		return schoolname;
	}

	public void setSchoolname(String schoolname) {
		this.schoolname = schoolname;
	}

	public String getClassnumber() {
		return classnumber;
	}

	public void setClassnumber(String classnumber) {
		this.classnumber = classnumber;
	}

	public String getStudeynumber() {
		return studeynumber;
	}

	public void setStudeynumber(String studeynumber) {
		this.studeynumber = studeynumber;
	}

	// @OneToOne(mappedBy="snsuser_info")
	// @JoinColumn(name="open_id")

	@Override
	public String toString() {

		return "username= " + username + "password= " + password
				+ "telephone= " + telephone + "truename=" + truename
				+ "schoolname=" + schoolname + "classnumber=" + classnumber
				+ "studynumber=" + studeynumber;

	}

}
package com.b505.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
 * 通過網頁授權查詢使用者的資訊
 * @author yulin
 * @date 2018-4-4 21:46
 */

@Entity
@Table(name="snsuser_info")
public class SNSUserInfo {
	@Id
	@GeneratedValue(generator = "a_native") //指定主鍵生成策略,預設為自增
	@GenericGenerator(name = "a_native",strategy= "native")
	private int id;
	//使用者標識
	private String openId;
	//使用者暱稱
	private String nickname;
	//性別 (1是男性 2是女性 3是未知)
	private int sex;
	//國家
	private String country;
	//省份
	private String province;
	//城市
	private String city;
	//使用者頭像連結
	private String headImgUrl;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public String getOpenId() {
		return openId;
	}
	public void setOpenId(String openId) {
		this.openId = openId;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public int getSex() {
		return sex;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getHeadImgUrl() {
		return headImgUrl;
	}
	public void setHeadImgUrl(String headImgUrl) {
		this.headImgUrl = headImgUrl;
	}
     
	     @Override
	     public String toString(){
	    	 
	    	 return "openId="+openId+"nickname="+nickname+"sex="+sex+"country="+country+"province="+province+"city="+city+"headImgUrl="+headImgUrl;
	     }
	    public SNSUserInfo(String openId, String nickname, int sex, String country,
					String province, String city, String headImgUrl
					) {
				super();
				this.openId = openId;
				this.nickname = nickname;
				this.sex = sex;
				this.country = country;
				this.province = province;
				this.city = city;
				this.headImgUrl = headImgUrl;
			}
		     public SNSUserInfo(){
		    	 
		     }
		public void setSex(int int1) {
			// TODO Auto-generated method stub
			
		}
}