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;

/**
 * 

* B505信息科学研究所 *

* * @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 userid; private String username; private String password; private String telephone; private String truename; private String schoolname; private String classnumber; private String studeynumber; // 外键 // @OneToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL) // referencedColumnName:参考列名,默认的情况下是列表的主键 // nullable=是否可以为空, // insertable:是否可以插入, // updatable:是否可以更新 // columnDefinition=列定义, // foreignKey=外键 // @JoinColumn(name="open_id",referencedColumnName="id",nullable=false) // private SNSUserInfo sNSUserInfo; public int getUserid() { return userid; } public void setUserId(int userid) { this.userid = userid; } 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.JoinColumn;
import javax.persistence.OneToOne;
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;
	@OneToOne
	@JoinColumn(name="userid")
	private Userinfo userinfo;
	
	public Userinfo getUserinfo() {
		return userinfo;
	}
	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}
	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
			
		}
}

 spring boot项目于的dao层 和service层这里就不在过多的去写了,直接写一个controller层,实现一对一存储信息的操作。

package com.b505.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.b505.entity.SNSUserInfo;
import com.b505.entity.Userinfo;
import com.b505.service.SNSUserinfoService;
import com.b505.service.UserinfoService;

/**
 * 

b505信息科学研究所

* @Description 保存注册用户信息,并回应ajax的success * @Creat date 2018-9-10 20:04 * @author yulin * */ @Controller public class RegisterClController { @Autowired private UserinfoService userinfoService; @Autowired private SNSUserinfoService sNSUserinfoService; @ResponseBody @RequestMapping(value = "/Register/aa", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public int getByJSON( @RequestBody JSONObject jsonParam) { // 直接将json信息打印出来 System.out.println("---------开始-----------"); // 将获取的json数据封装一层,然后在给返回 /*JSONObject result = new JSONObject(); result.put("msg", "ok"); result.put("method", "json"); result.put("data", jsonParam); return result.toString();*/ Userinfo userinfo=new Userinfo(); //用户名 userinfo.setUsername(jsonParam.getString("username")); //用户密码 userinfo.setPassword(jsonParam.getString("password")); //用户真实姓名 userinfo.setTruename(jsonParam.getString("truename")); //用户学校名称 userinfo.setSchoolname(jsonParam.getString("schoolname")); //用户班级 userinfo.setClassnumber(jsonParam.getString("classnumber")); //用户学号 userinfo.setStudeynumber(jsonParam.getString("studynumber")); //用户手机号 userinfo.setTelephone(jsonParam.getString("telephone")); System.out.println(userinfo); SNSUserInfo sNSUserInfo=new SNSUserInfo(); long i=sNSUserinfoService.count(); int id; id=(int)i; System.out.println("******************"+id); sNSUserInfo=sNSUserinfoService.findById(id); System.out.println("--------------------sNSUserInfo="+sNSUserInfo); sNSUserInfo.setUserinfo(userinfo); userinfoService.save(userinfo); return 1; } }

 

你可能感兴趣的:(jpa,spring,boot)