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中配置
【本例使用了此方式】
分享到:
相关推荐
通过参考和引用传智播客的免费教程,将springmvc4.1.6与hibernate4.3.10提供的JPA实现整合,使用mysql5.6.25,在MyEclipse2014中测试通过。可以作为web开发的基础框架使用。 使用说明: 1.需要安装mysql,并创建名为...
基于springmvc+maven+hibernate做的一个简易博客系统,方便对于springmvc的掌握和入门,按照过程一个个自己实践,相信会有一个不错的入门效果(仅作为入门,掌握springmvc和hibernate(JPA)所用,比如对于JPA...
JPA 使用 Hibernate 作为实现方式JPA + Hibernate + MySQL in Spring Boot注意此项目使用spring-boot 1.4.0版本,不适用于spring-boot 2.0 版本;2.0版本的项目配置可参考:本项目使用IDEA构建,直接导入IDEA即可...
springmvc-jpa-springdata-postgres 使用Hibernate作为JPA实现和PostgreSQL数据库的Spring Data。
Hibernate 是最流行的ORM 框架之一,也是最早实现JPA 的规范框架之一。它被JBoss 收购后, 目前作为JBoss 的一个开源框架,它遵循LGPL v2.1 开源许可协议,官方主页是http://www.hiber ate.org/。
通过实现JPA 作为内存数据库 进行集成测试 和提供的API文档 提供的数据库迁移 作为容器化解决方案 使用HAL实现将库用作HATEOAS 如何启动DropwizardGuice应用程序 运行mvn clean verify以构建您的应用程序 使用java ...
springmvc-jpa-postgres 使用Hibernate作为JPA实现对Spring MVC的持久性。 仅使用postgres数据库的“ springmvc-jpa”存储库的简化版本。
支持上传图片和文件,文件作为附件可以下载,图片可直接在JSP页面中显示 支持回帖,并在主楼中显示 答辩用PPT 选做部分: 帖子搜索 用户查找 管理员对帖子加精、置顶 根据浏览量,自动在首页生成热门帖子列表...
JPA用于整合现有的ORM技术,可以简化现有Java EE和Java SE应用的对象持久化的开发工作,实现ORM的统一。JPA作为一项对象持久化的标准,不但可以获得Java EE应用服务器的支持,还可以直接在Java SE中使用。JPA必将...
在开发JPA之前,Hibernate已作为开源开发。Hibernate的出现引起了当时正努力编写SQL的Java工程师的极大关注。 最终,像Hibernate这样的ORM框架开始在开发领域得到认可,并且对它们的需求也在增加,因此JCP开始制定...
jpa整合spring的demo,使用hibernate作为jpa的实现orm框架。
JPA(Java Persistence API)作为Java EE 5.0平台标准的ORM规范,将得到所有Java EE服务器的支持。Sun这次吸取了之前EJB规范惨痛失败的经历,在充分...目前Hibernate 3.2、TopLink 10.1.3以及OpenJpa都提供了JPA的实现。
JPA由EJB 3.0软件专家组开发,作为JSR-220实现的一部分。但它不囿于EJB 3.0,你可以在Web应用、甚至桌面应用中使用。JPA的宗旨是为POJO提供持久化标准规范,由此可见,经过这几年的实践探索,能够脱离容器独立运行,...
启动项目时将其用作基础模板。 UI端的实现示例参考/。 对于相同用例的 Spring Boot 实现示例,请参阅 。 分层的概念 它是三个层次的著名模型,但将基础设施层视为横截面解释。 层 用户界面 接收用例请求 应用 用例...
关于如何使用Spring Boot 2 , JPA , Hibernate和MySQL使用JWT令牌设置OAuth2授权服务器的简单项目。 简而言之 所有和客户端都存储在数据库中。 可以具有许多与之相关联的,并且可以具有与之相关联的许多,这些最后...
我实现 blueprints-jpa-graph 的原因是我被 ObjectDB(2) 的性能 (3) 所吸引。 1:JPA: : 2:对象数据库: ://www.objectdb.com/ 3:JPA 性能基准: ://www.jpab.org/All/All/All.html 支持功能 支持所有蓝图...
RESTful API,可让您管理仓库该项目使用Java和以下工具构建: 作为服务器端框架作为构建自动化工具为ORM / JPA实施作为数据库实现作为Hibernate的顶层作为数据库迁移工具作为动态查询的替代框架应用结构模型域模型是...
* Dao层(Data Access Object数据访问对象)使用Spring Data JPA来维护数据读写操作,Spring Data JPA的底层是基于Hibernate的,其JpaRepository、PageAndSortingRepository、CrudRepository等组件能够实现快速单表...
在使用jpa时,比如我使用hibernate作为实现,默认情况下如果classpath下有bean validation实现会自动开启JSR-303验证。其通过Listener实现,即可以在如insert、update之前执行验证,如果验证失败会直接抛出验证失败...
10.2.2 在Java SE环境下使用 Hibernate JPA实现 370 10.2.3 在Java SE环境下使用 TopLink JPA实现 374 10.2.4 在Java SE环境下使用 EntityManager 377 10.2.5 使用orm.xml管理O/R映射 379 10.3 理解实体 382 10.3.1 ...