JPA学习总结(二)--JPAOneToOne双向关联

步骤1.新建java项目:JPAOneToOne,将hibernate包以及mysql驱动包导入

 

步骤2.在类路径下新建xml文件persistence.xml,将其放入META-INF文件夹下,其代码如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
	version="1.0">
	<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
		<properties>
		
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8" />
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>	
			<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" />
			<property name="hibernate.connection.username" value="root" />
			<property name="hibernate.connection.password" value="root" />
			<property name="hibernate.max_fetch_depth" value="3" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>
</persistence>

 

 

步骤3.编写实体Bean类

        

首先创建Person:

 

package cn.itcast.bean;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Person {
	private Integer id;
	private String name;
	private IDCard idcard;
	
	public Person(){}
	
	public Person(String name) {
		this.name = name;
	}
	/*
	 * @OneToOne 标注映射的一些属性
	 * cascade=CascadeType.ALL 设置所有的级联操作,包括级联查询,级联保存,级联更新,级联删除
	 * optional=false 设置该属性是否为必须的,false为必须
	 * fetch=FetchType.EAGER 设置加载策略为立即加载,该属性可以省略
	 * @JoinColumn(name="idcard_id")设置外键值为idcard_id
	 */
	@OneToOne(cascade=CascadeType.ALL,optional=false,fetch=FetchType.EAGER)
	@JoinColumn(name="idcard_id")
	public IDCard getIdcard() {
		return idcard;
	}
	public void setIdcard(IDCard idcard) {
		this.idcard = idcard;
	}
	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(length=12,nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

 

 

创建IDCard:

 

 

package cn.itcast.bean;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class IDCard {
	private Integer id;
	private String cardno;
	private Person person;
	
	public IDCard(){}
	
	public IDCard(String cardno) {
		this.cardno = cardno;
	}
	/*
	 * @OneToOne 标注映射的一些属性
	 * cascade=CascadeType.ALL 设置所有的级联操作,包括级联查询,级联保存,级联更新,级联删除
	 * optional=false 设置该属性是否为必须的,false为必须
	 * fetch=FetchType.EAGER 设置加载策略为立即加载,该属性可以省略
	 * mappedBy="idcard" 指明由idcard来维护这个关系
	 */
	@OneToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH}
	,mappedBy="idcard")
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	@Column(length=18,nullable=false)
	public String getCardno() {
		return cardno;
	}
	public void setCardno(String cardno) {
		this.cardno = cardno;
	}
	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	
}

 

 

 

步骤4.创建junit测试

 

package junit.test;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.BeforeClass;
import org.junit.Test;

import cn.itcast.bean.IDCard;
import cn.itcast.bean.Person;

public class OneToOneTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test public void save(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		Person person = new Person("老张");
		person.setIdcard(new IDCard("123456789987654321"));
		
		em.persist(person);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
}

 

 

看能将表创建出来,并插入数据即可。

你可能感兴趣的:(bean,mysql,Hibernate,jpa,JUnit)