`
rsljdkt
  • 浏览: 452276 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

JPA Hibernate作为其实现

阅读更多

1.背景:

 JPA本身是一套接口和标准,我们可以使用hibernate的EntityManager模块来作为JPA的引擎

 

2.JPA使用的环境:

* 1. Java EE:可以在Java EE环境中配置JPA引擎

* 2. Java SE:必须在本地设置引擎:

在classpath根部的META-INF目录下persistence.xml中配置JPA

3.实现步骤:

1)在classpath根部META-INF目录下创建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="course">
      <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.url" value="jdbc:mysql://localhost:3306/db_jpa"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="root"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
      </properties>
   </persistence-unit>
</persistence>

  2)创建实体类,并加上JPA注解,代码如下:

package com.local.domain;

import java.util.Date;

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

@Entity
@Table(name="t_course")
public class Course {
	@Id
	@GeneratedValue
	private Long id;
	
	private String title;
	
	private Date beginDate;
	
	private Date endDate;
	
	private int fee;

	public long getId() {
		return id;
	}

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

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public int getFee() {
		return fee;
	}

	public void setFee(int fee) {
		this.fee = fee;
	}
	
	
}
 

3)编写Dao接口,代码如下:

package com.local.dao;

import com.local.domain.Course;

public interface CourseDao {
	public void save(Course course);
	public void delete(Long id);
	public Course get(Long id);
}

  4)编写Dao接口的JPA实现类,代码如下:

package com.local.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import com.local.dao.CourseDao;
import com.local.domain.Course;

public class JpaCourseDao implements CourseDao {

	private EntityManagerFactory entityManagerFactory;

	public JpaCourseDao() {
		entityManagerFactory = Persistence.createEntityManagerFactory("course");
	}

	@Override
	public void delete(Long id) {
		EntityManager manager = entityManagerFactory.createEntityManager();
		EntityTransaction tx = manager.getTransaction();
		try {
			tx.begin();
			Course course = manager.find(Course.class, id);
			manager.remove(course);
			tx.commit();
		} catch (RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			manager.close();
		}
	}

	@Override
	public Course get(Long id) {
		EntityManager manager = entityManagerFactory.createEntityManager();
		try {
			return manager.find(Course.class, id);
		} finally {
			manager.close();
		}
	}

	@Override
	public void save(Course course) {
		EntityManager manager = entityManagerFactory.createEntityManager();
		EntityTransaction tx = manager.getTransaction();
		try {
			tx.begin();
			manager.merge(course);
			tx.commit();
		} catch (RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			manager.close();
		}
	}
}

  5)进行Junit测试,代码如下:

package com.local.dao.impl;

import java.util.Date;
import org.junit.BeforeClass;
import org.junit.Test;
import com.local.dao.CourseDao;
import com.local.domain.Course;

public class CourseDaoImplTest {
	private static CourseDao courseDao;
	@BeforeClass
	public static void init(){
		courseDao=new JpaCourseDao();
	}
	@Test
	public void testSave(){
		Course course=new Course();
		course.setTitle("course2");
		course.setBeginDate(new Date());
		course.setEndDate(new Date());
		course.setFee(100);
		courseDao.save(course);
	}
	@Test
	public void testGet(){
		Course course=courseDao.get(new Long(4));
		System.out.println("title: "+course.getTitle());
		System.out.println("beginDate: "+course.getBeginDate());
		System.out.println("endDate: "+course.getEndDate());
		System.out.println("fee: "+course.getFee());
	}
}
 

4.关于persistence.xml配置文件的说明:

* 方式一:通过引用hibernate.cfg.xml

【注意此时由于Hibernate EntityManager会自动侦测XML映射文件和JPA注解,将他们作为映射元数据,因此不必再配置文件中显式指明。否则会出现org.hibernate.DuplicateMappingException异常】

<persistence>

<persistence-unit name="course">

<properties>

<!--引用classpath根部的Hibernate配置文件 -->

<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>

</properties>

</persistence-unit>

</persistence>

* 方式二:使用 JNDI数据源,形如:<jta-data-source>java:/DefaultDS</jta-data-source>

其他的hibernate属性在本配置文件中配置

* 方式三:数据库的连接属性和其他属性均通过hibernate的形式在persistence.xml中配置

【本例使用了此方式】

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics