1.配置文件:persistence.xml

 <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="TestJPA" transaction-type="RESOURCE_LOCAL"> <!-- JAP实现的提供者
1.如果只有一个提供者,可以不写 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.hanqi.dao.JPANews</class> <properties> <!-- 数据库连接 -->
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="javax.persistence.jdbc.user" value="test"/>
<property name="javax.persistence.jdbc.password" value="test"/>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/> <!-- JPA提供者的配置 -->
<!-- 数据库方言 -->
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<!-- sql语句/调试 -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<!-- 自动建表(正向工程)方式 -->
<property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit>
</persistence>

2.编写实体类:JPANews.java

 package com.hanqi.dao;

 import java.util.Date;

 import javax.persistence.*;
@NamedQuery(name="cx",query="from JPANews n where n.id = ?")
@Table(name="JPA_News")//类与表之间的映射关系
@Entity //实体类
public class JPANews { private Integer id;
private String title;
private String contant;
private Date createdate;
private String author; @GeneratedValue(strategy=GenerationType.AUTO)//主键生成策略
@Id//指定主键
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
} @Basic
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContant() {
return contant;
}
public void setContant(String contant) {
this.contant = contant;
} /**
* @return the createdate
*/
@Column(name="CREATEDATE")
@Basic
public Date getCreatedate() {
return createdate;
}
/**
* @param createdate the createdate to set
*/
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
this.author = author;
} @Override
@Transient
public String toString() {
return "News [id=" + id + ", title=" + title + ", contant=" + contant + ", createdate=" + createdate + "]";
} }

3.测试用例TestJPA.java

 package com.hanqi.dao;

 import static org.junit.Assert.*;

 import java.util.Date;
import java.util.List; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query; import org.junit.*;
import org.junit.Test; public class TestJPA {
EntityManagerFactory entityManagerFactory = null;
EntityManager entityManager = null;
EntityTransaction transaction = null; @Before
public void init()
{
//1. 创建 EntitymanagerFactory
String persistenceUnitName = "TestJPA"; entityManagerFactory =
Persistence.createEntityManagerFactory(persistenceUnitName); //2. 创建 EntityManager. 类似于 Hibernate 的 SessionFactory
entityManager = entityManagerFactory.createEntityManager(); //3. 开启事务
transaction = entityManager.getTransaction();
transaction.begin();
} @After
public void destory()
{
//5. 提交事务
transaction.commit(); //6. 关闭 EntityManager
entityManager.close(); //7. 关闭 EntityManagerFactory
entityManagerFactory.close();
} @Test
public void testjpa()
{ //数据操作
JPANews jn = new JPANews(); jn.setTitle("标题");
jn.setAuthor("作者");
jn.setContant("内容");
jn.setCreatedate(new Date());
//
// entityManager.persist(jn);//保存
JPANews jn2 = entityManager.merge(jn);//saveOrUpdate System.out.println(jn);
System.out.println(jn2);//从瞬时状态转变为持久化状态jpa是创建一个新对象将旧对象信息拷贝到新对象中 // JPANews jpan = entityManager.find(JPANews.class, 41);//立即加载
//
// jpan.setTitle("新的标题123");
//
// entityManager.flush();//提交语句
// System.out.println(jpan);
// //延迟加载
// JPANews jpan2 = entityManager.getReference(JPANews.class, 42);
//
// System.out.println("id="+jpan2.getId());
// System.out.println("标题="+jpan2.getTitle());
//
// entityManager.remove(jpan2);//删除 } @Test
public void testjpql()
{
//JPQL语句
String jpql = "from JPANews n where n.id = ?"; Query q = entityManager.createQuery(jpql); // Query q = entityManager.createNamedQuery("cx");//执行实体类注解中的jpql语句 q.setParameter(1, 41);//添加jpql语句参数,占位符编号从1开始 List<JPANews> l = q.getResultList(); System.out.println("长度=" + l.size()); } }

JPA Hibernate应用实例的更多相关文章

  1. Spring + SpringMVC + Druid + JPA(Hibernate impl) 给你一个稳妥的后端解决方案

    最近手头的工作不太繁重,自己试着倒腾了一套用开源框架组建的 JavaWeb 后端解决方案. 感觉还不错的样子,但实践和项目实战还是有很大的落差,这里只做抛砖引玉之用. 项目 git 地址:https: ...

  2. Spring Boot + Jpa(Hibernate) 架构基本配置

    本文转载自:https://blog.csdn.net/javahighness/article/details/53055149 1.基于springboot-1.4.0.RELEASE版本测试 2 ...

  3. SpringBoot + Jpa(Hibernate) 架构基本配置

    1.基于springboot-1.4.0.RELEASE版本测试 2.springBoot + Hibernate + Druid + Mysql + servlet(jsp) 一.maven的pom ...

  4. Spring Boot 2.x 之 Spring Data JPA, Hibernate 5

    1. Spring Boot常用配置项 基于Spring Boot 2.0.6.RELEASE 1.1 配置属性类 spring.jpa前缀的相关配置项定义在JpaProperties类中, 1.2 ...

  5. 持久化框架Hibernate 开发实例(一)

    1 Hibernate简介 Hibernate框架是一个非常流行的持久化框架,其中在web开发中占据了非常重要的地位, Hibernate作为Web应用的底层,实现了对数据库操作的封装.HIberna ...

  6. Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题

      (转载)Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题   这几天在用spring boot开发项目, 在开发的过程中遇到一个问题hibernate在执 ...

  7. Spring4.x Jpa + hibernate的配置(废弃JpaTemplate)

    近年来 ORM(Object-Relational Mapping,对象关系映射,即实体对象和数据库表的映射)技术市场热闹非凡,各种各样的持久化框架应运而生,其中影响最大的是 Hibernate 和 ...

  8. springboot 集成 jpa/hibernate

    pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  9. 五、spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate

    1.pom添加依赖 <!-- spring data jpa,会注入tomcat jdbc pool/hibernate等 --> <dependency> <group ...

随机推荐

  1. Golang tips

    1.go test 测试单个函数 go test -v -test.run Test* 2.

  2. Codeforces 461B. Appleman and Tree[树形DP 方案数]

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. ATM模拟程序

    一个很简单的ATM模拟程序 #include <stdio.h> void chaxun(int a3){ int b; b=a3; printf("您的余额为:%d\n&quo ...

  4. Stunnel服务端

    Stunnel on Debian GNU/Linux 6 (squeeze) 传统的POP3, SMTP, Samba等服务,都是不加密的协议(即在网络上明文传输数据),通过stunnel,可以将访 ...

  5. net对XML增删改查

    Pass:看公司代码,配置下拉框的功能,和下拉框的数字转文字.配置xml里面有下拉的value,name,这样界面直接显示数字,然后转译成中文 1.xml文件格式 <?xml version=& ...

  6. 配置Supervisor开机启动

    配置Supervisor开机启动: 新建一个"supervisord.service"文件 # dservice for systemd (CentOS 7.0+) # by ET ...

  7. android edittext 去边框 去下划线

    EditText的background属性设置为@null就搞定了:android:background="@null"style属性倒是可加可不加 附原文:@SlumberMac ...

  8. shiro退出登陆清空缓存实现

    上一篇介绍了使用springmvc集成shiro登陆过程(http://www.cnblogs.com/nosqlcoco/p/5579081.html),通过FormAuthenticationFi ...

  9. Burndown chart

    S型的燃尽图 在一次milestone开发过程中,开发者会持续编辑issue列表,每个issue都有自己的生命周期.燃尽图预期这些issues会被线性的消灭掉,所以从第一天直接到最后一天画个直线表示预 ...

  10. ASP.NET的编译原理

    http://www.cnblogs.com/mdy2001212/archive/2008/01/31/1060345.html