hibernate annotation one-to-one

1.共享主键

CREATE TABLE `person` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `personName` varchar(45) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `card` (
  `id` bigint(20) NOT NULL,
  `cardName` varchar(45) NOT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `FK_card_1` FOREIGN KEY (`id`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;


@Entity
@Table
public class Person implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6396702070077903027L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {return id;}
	public void setId(Long id) {this.id = id;}
	private Long id;
	
	@Column(length=45,nullable=false)
	public String getPersonName() {return personName;}
	public void setPersonName(String personName) {this.personName = personName;}
	private String personName;
	
	@OneToOne(cascade=CascadeType.ALL)
	@PrimaryKeyJoinColumn
	public Card getCard() {return card;}
	public void setCard(Card card) {this.card = card;}
	private Card card;
	
	

}
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table
public class Card implements Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2647551188609130315L;

	@Id
	@GenericGenerator(name ="pkGenerator",strategy="foreign" ,parameters={@Parameter(name = "property", value = "person")})  
    @GeneratedValue(generator="pkGenerator")  
	public Long getId() {return id;}
	public void setId(Long id) {this.id = id;}
	private Long id;
	
	@Column(length=45,nullable=false)
	public String getCardName() {return cardName;}
	public void setCardName(String cardName) {this.cardName = cardName;}
	private String cardName;
	
	@OneToOne(mappedBy="card")
	public Person getPerson() {return person;}
	public void setPerson(Person person) {this.person = person;}
	private Person person;
	

}


2.增加一列外键字段

CREATE TABLE `person` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `personName` varchar(45) NOT NULL,
  `card_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK8E4887752C8A757D` (`card_id`),
  CONSTRAINT `FK8E4887752C8A757D` FOREIGN KEY (`card_id`) REFERENCES `card` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `card` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cardName` varchar(45) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
import java.io.Serializable;
import javax.persistence.CascadeType;
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.OneToOne;
import javax.persistence.Table;


@Entity
@Table
public class Person implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6396702070077903027L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {return id;}
	public void setId(Long id) {this.id = id;}
	private Long id;
	
	@Column(length=45,nullable=false)
	public String getPersonName() {return personName;}
	public void setPersonName(String personName) {this.personName = personName;}
	private String personName;
	
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn
	public Card getCard() {return card;}
	public void setCard(Card card) {this.card = card;}
	private Card card;
	
	

}


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.OneToOne;
import javax.persistence.Table;

@Entity
@Table
public class Card implements Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2647551188609130315L;
	
	@Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)  
	public Long getId() {return id;}
	public void setId(Long id) {this.id = id;}
	private Long id;
	
	@Column(length=45,nullable=false)
	public String getCardName() {return cardName;}
	public void setCardName(String cardName) {this.cardName = cardName;}
	private String cardName;
	
	@OneToOne(mappedBy="card")
	public Person getPerson() {return person;}
	public void setPerson(Person person) {this.person = person;}
	private Person person;
	

}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值