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;
}