Hibernate annotation 一对一 外键关联

先创建表结构:

 

CREATE TABLE `user_detail` (
  `id` varchar(32) NOT NULL,
  `user_type` varchar(255) DEFAULT NULL,
  `first_name` varchar(10) NOT NULL,
  `last_name` varchar(20) NOT NULL,
  `email` varchar(30) NOT NULL,
  `id_card` varchar(18) NOT NULL,
  `phone` varchar(13) DEFAULT NULL,
  `mobile_phone` varchar(11) DEFAULT NULL,
  `province` varchar(32) DEFAULT NULL,
  `city` varchar(32) DEFAULT NULL,
  `zone` varchar(32) DEFAULT NULL,
  `address` varchar(120) DEFAULT NULL,
  `user_id` varchar(32) NOT NULL,
  `create_time` datetime NOT NULL,
  `last_update` datetime DEFAULT NULL,
  `deleted` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `user_detail_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `users` (
  `id` varchar(32) NOT NULL,
  `login_name` varchar(50) NOT NULL,
  `password` varchar(128) NOT NULL,
  `last_login_date` datetime DEFAULT NULL,
  `last_login_ip` varchar(15) DEFAULT NULL,
  `locked` tinyint(4) NOT NULL,
  `create_time` datetime NOT NULL,
  `last_update` datetime DEFAULT NULL,
  `deleted` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `login_name` (`login_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 


由于比较简单我就直接贴代码了

 

BaseEntity.java

package com.javaeye.sun2009.job.entity;
 
import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import org.hibernate.annotations.GenericGenerator;

/**
 * 统一定义id的entity基类.
 * 
 * 基类统一定义id的属性名称、数据类型、列名映射及生成策略.
 * 子类可重载getId()函数重定义id的列名映射和生成策略.
 * 
 * @author Administrator
 */
//JPA Entity基类的标识
@MappedSuperclass
public abstract class BaseEntity implements Serializable {

	@Id
	@GenericGenerator(name = "hibernate-uuid", strategy = "uuid")
	@GeneratedValue(generator = "hibernate-uuid")
	private String id;
	
	@Column(updatable = false)
	private Date createTime = new Date();
	
	@Column(insertable = false)
	private Date lastUpdate = new Date();
	
	private Boolean deleted = false;

	public String getId() {
		return id;
	} 

	public void setId(String id) {
		this.id = id;
	}
	
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public Boolean getDeleted() {
		return deleted;
	}

	public void setDeleted(Boolean deleted) {
		this.deleted = deleted;
	}
}

 

 

User.java

package com.javaeye.sun2009.job.entity.user;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.javaeye.sun2009.job.entity.BaseEntity;

/**
 * 用户登录表
 * @author Administrator
 */
@Entity
@Table(name = "users")
public class User extends BaseEntity {

	private static final long serialVersionUID = 2688258413041532400L;

	/**
	 * 登录用户名
	 */
	@Column(updatable = false)
	private String loginName;
	
	/**
	 * 密码
	 */
	private String password;
	
	/**
	 * 最后登录日期
	 */
	@Column(insertable = false)
	private Date lastLoginDate;
	
	/**
	 * 最后登录IP
	 */
	@Column(insertable = false)
	private String lastLoginIp;

	/**
	 * 账户是否被锁定
	 */
	private Boolean locked;
	
	/**
	 * 用户详细信息
	 */
	@OneToOne(mappedBy = "user", cascade = CascadeType.ALL,  optional = true)
	private UserDetail userDetail;
 
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

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

	public Boolean getLocked() {
		return locked;
	}

	public void setLocked(Boolean locked) {
		this.locked = locked;
	}

	public UserDetail getUserDetail() {
		return userDetail;
	}

	public void setUserDetail(UserDetail userDetail) {
		this.userDetail = userDetail;
	}

	public Date getLastLoginDate() {
		return lastLoginDate;
	}

	public void setLastLoginDate(Date lastLoginDate) {
		this.lastLoginDate = lastLoginDate;
	}

	public String getLastLoginIp() {
		return lastLoginIp;
	}

	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}
	
}

 

UserDetail.java

package com.javaeye.sun2009.job.entity.user;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import com.javaeye.sun2009.job.entity.BaseEntity;

/**
 * 用户详细信息表
 * @author Administrator
 *
 */
@Entity
public class UserDetail extends BaseEntity {

	private static final long serialVersionUID = 880651093581151378L;
	
	/**
	 * 用户登录信息
	 */
	@OneToOne
	@JoinColumn(name="user_id")
	private User user;
	
	/**
	 * 用户类型
	 */
	@Enumerated(EnumType.STRING)
	private UserType userType;
	
	/**
	 * 姓
	 */
	private String firstName;
	
	/**
	 * 名
	 */
	private String lastName;
	
	/**
	 * email地址
	 */
	private String email;
	
	/**
	 * 身份证号码
	 */
	private String idCard;
	
	/**
	 * 固定电话
	 */
	private String phone;
	
	/**
	 * 手机
	 */
	private String mobilePhone;
	
	/**
	 * 省
	 */
	private String province;
	
	/**
	 * 市
	 */
	private String city;

	/**
	 * 区
	 */
	private String zone;

	/**
	 * 详细地址
	 */
	private String address;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	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 getZone() {
		return zone;
	}

	public void setZone(String zone) {
		this.zone = zone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public UserType getUserType() {
		return userType;
	}

	public void setUserType(UserType userType) {
		this.userType = userType;
	}
	
	
}

 



之后我试了一下一对一的延时加载,在上面的代码处改了一下,如下:

/**
	 * 用户详细信息
	 */
	@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = true)
	private UserDetail userDetail;
 



却是不能延时加载,后来有网友说一对一不能延时加载,后来我也就放弃了,有朋友知道的请麻烦告诉我下。

有写错的地方,欢迎指正!

你可能感兴趣的:(数据结构,Hibernate,jpa,mobile)