JPA初探04---一对一的双向关联

关系:
公民  ---  身份证 
一对一的关系

数据库设计


create table t_citizen (
	cid int primary key,
	cname varchar,
	cardid int
);

create table t_card(
	cid int primary key,
	cardno varchar(45)
);

【Java类设计】

package oneToOne;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
/**
 * 
 * @author 青山
 * @date 2014年10月19日
 * @file name: Citizen.java
 * 
 * 注意:我们决定公民是关系的维护方,那么外键的字段就这公民一方
 */
@Entity
@Table(name="t_citizen")
public class Citizen {
	@Id
	@Column(name="cid")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int wid;
	
	@Column(name="cname")
	private String name;
	
	@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY,optional=true)
	//optional这里为true,表示有公民不一定一定会有身份证;optional英文意思:是否是可选项
	@JoinColumn(name="cardid")//指定外键使用JoinColumn
	private Card card;
	
	public int getWid() {
		return wid;
	}

	public void setWid(int wid) {
		this.wid = wid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}
	
}

===========================================================
package oneToOne;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
/**
 * 身份证类
 * @author 青山
 * @date 2014年10月19日
 * @file name: Card.java
 * 
 * 注意: 这是关系的被维护方法
 */
@Entity
@Table(name="t_card")
public class Card {
	@Id
	@Column(name="cid")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int hid;
	
	
	private String cardno;
	//mappedBy一出现在一对一的关系中就是关系的被维护方法,mappedBy的值是对方哪一个属性维护这个关系
	@OneToOne(mappedBy="card",fetch=FetchType.LAZY,cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH},
			optional=false)
	//optional为false表示有了身份证一定要有一个公民对应,即有了关系的被维护方一定要有关系的维护方
	//配偶的编号
	private Citizen citizen;
	
	
	
	public int getHid() {
		return hid;
	}
	public void setHid(int hid) {
		this.hid = hid;
	}
	
	public String getCardno() {
		return cardno;
	}
	public void setCardno(String cardno) {
		this.cardno = cardno;
	}
	public Citizen getCitizen() {
		return citizen;
	}
	public void setCitizen(Citizen citizen) {
		this.citizen = citizen;
	}

}
===========================================================

package oneToOne;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import util.JPAManagerFactory;

public class OneToOneTest {
	public static void main(String[] args) {
		EntityManager manager = JPAManagerFactory.getMFInstance().getManager();
		EntityTransaction t = manager.getTransaction();
		t.begin();
		
		
//		Card card = new Card();
//		card.setCardno("69364376247382");
//		Citizen w = new Citizen();
//		w.setName("张上哪疯");
//		w.setCard(card);
//		manager.persist(w);
		
//		Card car = manager.find(Card.class, 1);
//		System.out.println(car.getCardno());
//		System.out.println(car.getCitizen().getName());
		
		Card car = new Card();
		car.setCardno("234324321111");
		manager.persist(car);
		
//		Card car = manager.find(Card.class, 2);
//		Citizen c = manager.find(Citizen.class, 1);
//		c.setCard(car);
		
		t.commit();
		manager.close();
	}
}

 

你可能感兴趣的:(jpa)