jpa入门(使用hibernate)

新建工程,加入jar包,如图:

jpa入门(使用hibernate)_第1张图片


User:

package com.jpa;

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

@Entity(name="TB_USER")
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	@SequenceGenerator(name="SEQ_USER")
	private int id;
	
	@Column(name="USER_AGE")
	private int age;
	
	@Column(name="USER_NAME")
	private String name;
	
	public int getId() {
		return id;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "User [id=" + id + ", age=" + age + ", name=" + name + "]";
	}

}

persistence.xml:

<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="orclJPA">
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
			<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
			<property name="hibernate.connection.username" value="HIBERNATE" />
			<property name="hibernate.connection.password" value="HIBERNATE" />
			<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
			<property name="hibernate.max_fetch_depth" value="3" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>
</persistence>

建表SQL:

CREATE TABLE TB_USER
(
    ID INTEGER PRIMARY KEY,
    USER_NAME VARCHAR2(20) NOT NULL,
    USER_AGE INTEGER NOT NULL
);

CREATE SEQUENCE SEQ_USER
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
CACHE 10;

CREATE OR REPLACE TRIGGER TR_USER BEFORE INSERT ON TB_USER FOR EACH ROW
BEGIN
    SELECT SEQ_USER.NEXTVAL INTO :NEW.ID FROM DUAL;
END;

测试代码:

package com.jpa;

import java.util.Random;

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

import junit.framework.TestCase;

public class TestJPA extends TestCase {
	private static final Random random = new Random();
	
	private static final String persistenceUnitName = "orclJPA";
	
	private static EntityManagerFactory emf;
	
	private static EntityManager em;

	@Override
	protected void setUp() throws Exception {
		emf = Persistence.createEntityManagerFactory(persistenceUnitName);
		emf.createEntityManager();
		em = emf.createEntityManager();
	}

	public void testSave() {
		em.getTransaction().begin();
		User user = new User();
		user.setAge(random.nextInt(100));
		user.setName("user" + random.nextInt(100));
		em.persist(user);
		em.getTransaction().commit();
	}
	
	public void testFind() {
		em.getTransaction().begin();
		User user = em.find(User.class, 1);
		System.out.println(user);
	}
	
	public void testUpdate() {
		em.getTransaction().begin();
		User user = em.find(User.class, 1);
		user.setName("user" + random.nextInt(100));
		em.getTransaction().commit();
	}
	
	public void testRemove() {
		em.getTransaction().begin();
		User user = em.find(User.class, 3);
		em.remove(user);
		em.getTransaction().commit();
	}
	
	@Override
	protected void tearDown() throws Exception {
		em.close();
		emf.close();
	}
}


你可能感兴趣的:(jpa入门(使用hibernate))