JPA入门

JPA入门_第1张图片
JPA入门_第2张图片
JPA入门_第3张图片

JPA入门_第4张图片
JPA入门_第5张图片

JPA入门_第6张图片
JPA入门_第7张图片
JPA入门_第8张图片

persistence.xml:在这里插入图片描述

JPA入门_第9张图片

persistence.xml



	
		
		org.hibernate.ejb.HibernatePersistence
	
		
		com.ldp.jpa.bean.Customer
		
		ENABLE_SELECTIVE
	
		
			
			
			
			
			
			
			
			
			
			
		
	

Customer实体类

package com.ldp.jpa.bean;
//定义一个持久化类

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor;

@Table(name="customer")
@Entity
public class Customer {

	private Integer id;
	private String lastName;
	private String email;
	private Integer age;
	
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Id
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	@Column(name="last_name")
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", lastName=" + lastName + ", email=" + email + ", age=" + age + "]";
	}
}

测试JPA的操作步骤:

@Test
	public void test01() {
		//操作步骤JPA
		//1.创建EntityManagerFactory
		String persistenceUnitName="jpa-1";
		EntityManagerFactory entityManagerFactory=
				Persistence.createEntityManagerFactory(persistenceUnitName);
		//2.获取EntityManager对象
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		//3.开启事务
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
		//4.进行持久化操作
		Customer customer=new Customer();
		customer.setAge(22);
		customer.setEmail("[email protected]");
		customer.setLastName("刘东平");
		entityManager.persist(customer);
		//5.提交事务
		transaction.commit();
		//6.关闭entitymanager对象
		entityManager.close();
		//7.关闭entitymanagerfactory对象
		entityManagerFactory.close();
		
	}

你可能感兴趣的:(JPA,JPA入门)