JPAのマッピング

3271 ワード

jpaのマッピングをまとめます.
これらのマッピングは、双方向に関連付けられ、メンテナンス・エンドを設定し、複数対の1次元保護エンドを複数のセグメントに設定できます.
その他は状況に応じて設定します.メンテナンス側の設定被メンテナンス側の設定(mappedBy)は次のコードを参照してください.
1対1:oneToone
一対多/多対一:manytoone
複数対複数:manytomany:
 
               :one-one   ,        ,    person
package cn.itcast.bean;

import java.util.Date;

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.Temporal;
import javax.persistence.TemporalType;

@Entity
public class IDCard {
	private Integer cardid;
	private String cardno;
	private Date birthday = new Date();
	private Person person;
	public IDCard(){}
	
	public IDCard(String cardno) {
		this.cardno = cardno;
	}	
//  :mappedBy     person   card   ,cascade           ,
	@OneToOne(mappedBy="card",cascade=CascadeType.REFRESH)
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)//Id         GeneratedValue        
	public Integer getCardid() {
		return cardid;
	}
	public void setCardid(Integer cardid) {
		this.cardid = cardid;
	}
	
	@Column(length=18,nullable=false)
	public String getCardno() {
		return cardno;
	}
	public void setCardno(String cardno) {
		this.cardno = cardno;
	}
	
	@Temporal(TemporalType.DATE) @Column(nullable=false) //      nullable       
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}

 
package cn.itcast.bean;

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;

@Entity
public class Person {
	private Integer id;
	private String name;
	private IDCard card;
	
	@OneToOne(cascade=CascadeType.ALL,optional=false) //optional false  card  null
	@JoinColumn(name="card_id") //        ,      jpa     。
	public IDCard getCard() {
		return card;
	}
	public void setCard(IDCard card) {
		this.card = card;
	}
	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(length=10,nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}