JPA基础

<?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.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
		     <property name="hibernate.hbm2ddl.auto" value="update"/>
		     <property name="hibernate.show_sql" value="true"/>
		     
		      <property name="hibernate.format_sql" value="true" />
			<!-- sqlServer的驱动器 -->
			<property name="hibernate.connection.driver_class"
				value="com.mysql.jdbc.Driver" />
			<!-- 设置sqlServer路径以及数据库名 -->
			<property name="hibernate.connection.url"
				value="jdbc:mysql://localhost/jpa" />
			<!-- 数据库的用户名 -->
			<property name="hibernate.connection.username" value="root" />
			<!-- 数据库的密码 -->
			<property name="hibernate.connection.password" value="root" />
		</properties>
	</persistence-unit>
</persistence>


package cn.itcast.bean;

public enum Gender {
	MALE,FEMALE
}


package cn.itcast.bean;

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="t_person")
public class Person {

	private Integer id;
	private String name;
	private Date birthday;
	private Gender gender = Gender.MALE;
	private String info;  //大文本
	public Byte[] file;
	private String imagepath;
	
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}	
	@Column(length=10,nullable=false,name="personName")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Temporal(TemporalType.DATE)
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Enumerated(EnumType.STRING)
	@Column(length=6,nullable=false)
	public Gender getGender() {
		return gender;
	}
	public void setGender(Gender gender) {
		this.gender = gender;
	}
	@Lob
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	@Lob
	@Basic(fetch=FetchType.LAZY)
	public Byte[] getFile() {
		return file;
	}
	public void setFile(Byte[] file) {
		this.file = file;
	}
	@Transient					//在数据库中不映射
	public String getImagepath() {
		return imagepath;
	}
	public void setImagepath(String imagepath) {
		this.imagepath = imagepath;
	}
	
}



package com.bjsxt.test;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.BeforeClass;
import org.junit.Test;

import cn.itcast.bean.Person;


public class PersonTest {

	@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.setName("test");
		person.setBirthday(new Date());
		em.persist(person);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
	
	@Test
	public void getPerson() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		Person person = em.find(Person.class,1);
		System.out.println(person.getName());
		em.close();
		factory.close();
	}
	
	@Test
	public void getPerson2() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		Person person = em.getReference(Person.class,1);
		System.out.println(person.getName());
		em.close();
		factory.close();
	}
	
	@Test
	public void updatePerson() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Person person = em.find(Person.class,1);
		person.setName("老刁");
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
	//new
	//managed 托管
	//游离(脱管)
	//删除
	@Test
	public void updatePerson2() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Person person = em.find(Person.class,1);
		em.clear();
		person.setName("老张");
		em.merge(person);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
	
	@Test
	public void deletePerson() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Person person = em.find(Person.class,1);
		em.remove(person);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

		@Test
	public void query() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		Query query = em.createQuery("select o from Person o where o.id = ?1");
		query.setParameter(1, 2);
		Person person = (Person)query.getSingleResult();
		System.out.println(person.getName());
		em.close();
		factory.close();
	}
	
}

你可能感兴趣的:(jpa)