JPA初探04---一対一の双方向関連

4670 ワード


  :
    ---      
      

     


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