JPA一对一关系实例配置

在JPA中双向一对一关系,是对等关系,双方都可以作为关系维护端,这里使用Person和IdCard,指定Person为关系维护端

src下在面的META-INF/persistence.xml配置如下:

<?xml version="1.0"?>
<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.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.connection.url"
				value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>
</persistence>

 

关系维护端:Person类

@Entity
@Table(name = "t_persons")
public class Person{
	private Integer id;
	private String name;
	private IdCard idCard;
	/**关系维护端,可能进行级联持久化,级联合并,级联刷新与级联删除*/
	@OneToOne(cascade={CascadeType.ALL},optional=false)
	/**外键字段为idcard_id*/
	@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 = 10, nullable = false)
	public String getName() {
		return name;
	}

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

	public Person() {
		super();
	}

	public Person(String name) {
		super();
		this.name = name;
	}
	
}

 

关系被维护端IdCard类:

@Entity
@Table(name = "t_idcards")
public class IdCard  {
	private Integer id;
	private String cardNo;
	private Person person;
	/**关系被维护端,级联持久化,级联合并,级联刷新*/
	@OneToOne(mappedBy = "idCard", cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, optional = false)
	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(length = 18, nullable = false)
	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public IdCard() {
		super();
	}

	public IdCard(String cardNo) {
		super();
		this.cardNo = cardNo;
	}
	
}

 测试类:

public class OneToOneTest {
	static EntityManagerFactory factory = null;
	EntityManager em = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		factory = Persistence.createEntityManagerFactory("itcast");
	}

	@Test
	public void save() {
		em = factory.createEntityManager();
		em.getTransaction().begin();
		Person p = new Person("小群");
		IdCard idCard = new IdCard("430682198806121528");
		p.setIdCard(idCard);// person关联idcard
		idCard.setPerson(p);// idcard关联person
		em.persist(p);// 关系维护端进行持久化操作
		em.getTransaction().commit();

	}

}

 

MySql数据表:

 
JPA一对一关系实例配置_第1张图片
 一对一关系映射图:


JPA一对一关系实例配置_第2张图片
 

 

你可能感兴趣的:(jpa,一对一,onetoone,一对一关系)