首页 > 学院 > 开发设计 > 正文

hibernate对JPA_Annotation的支持实例讲解

2019-11-08 03:14:02
字体:
来源:转载
供稿:网友

        JPA全称java Persistence API.JPA通过JDK 5.0注解或xml描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.JPA是一种规范,而hibernate是JPA的实现,除了hibernate还有EclipseLink也是JPA的实现.JPA 是 JCP 组织发布的 Java EE 标准之一,因此任何声称符合 JPA 标准的框架都遵循同样的架构,提供相同的访问API,这保证了基于JPA开发的企业应用能够经过少量的修改就能够在不同的JPA框架下运行。

第一个hibernate(hibernate annotation)JPA项目

1.建立java项目

2.创建User Library,加入依赖包* HIBERNATE_HOME/lib/*.jar* HIBERNATE_HOME/hibernate3.jar* 加入数据库驱动(MySQL驱动)3.加入hibernate annotation支持包 * hibernate-annotations.jar* ejb3-persistence.jar* hibernate-commons-annotations.jar前三步骤参考http://blog.csdn.net/chenxiaochan/article/details/51417499,这篇博客中详细讲解了如何建立java项目,创建user library,以及加入包.4.提供hibernate.cfg.xml文件,完成基本的配置

<!DOCTYPE hibernate-configuration PUBLIC	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>	<session-factory>		<PRoperty name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_jpa_first</property>		<property name="hibernate.connection.username">root</property>		<property name="hibernate.connection.passWord">bjpowernode</property>		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>		<property name="hibernate.show_sql">true</property>		<!-- 		<property name="hibernate.format_sql">true</property>		 -->			</session-factory></hibernate-configuration>5、建立实体类User.java,采用注解完成映射

在这里需要说明,在使用@Entity注解的时候,引用javax.persistence.Entity,这个是jpa的jar包.@Id使用的注解是主键的注解.

package com.bjpowernode.hibernate;import java.util.Date;import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class User {	private String id;		private String name;		private String password;		private Date createTime;		private Date expireTime;	@Id	public String getId() {		return id;	}	public void setId(String id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getPassword() {		return password;	}	public void setPassword(String password) {		this.password = password;	}	public Date getCreateTime() {		return createTime;	}	public void setCreateTime(Date createTime) {		this.createTime = createTime;	}	public Date getExpireTime() {		return expireTime;	}	public void setExpireTime(Date expireTime) {		this.expireTime = expireTime;	}}7.在hibernate.cfg.xml文件中添加user的映射.

<mapping class="com.bjpowernode.hibernate.User"/>8.编写工具类ExoprtDB.java,注解生成ddl,必须采用AnnotationConfiguration类

package com.bjpowernode.hibernate;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;/** * * @author Administrator * */public class ExportDB {	public static void main(String[] args) {				Configuration cfg = new AnnotationConfiguration().configure();				SchemaExport export = new SchemaExport(cfg);		export.create(true, true);	}}9.建立客户端类Client,添加用户数据到mysql
package com.bjpowernode.hibernate;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;public class Client {	public static void main(String[] args) {				//读取hibernate.cfg.xml文件		Configuration cfg = new AnnotationConfiguration().configure();				//建立SessionFactory		SessionFactory factory = cfg.buildSessionFactory();				//取得session		Session session = null;		try {			session = factory.openSession();			//开启事务			session.beginTransaction();			User user = new User();			user.setId("0001");			user.setName("张三");			user.setPassword("123");			user.setCreateTime(new Date());			user.setExpireTime(new Date());						//保存User对象			session.save(user);						//提交事务			session.getTransaction().commit();		}catch(Exception e) {			e.printStackTrace();			//回滚事务			session.getTransaction().rollback();		}finally {			if (session != null) {				if (session.isOpen()) {					//关闭session					session.close();				}			}		}	}}JPA不仅拥有ORM的特点,还有自己独有的特点:

1 .标准化2 .对容器级特性的支持3 .简单易用,集成方便4 .可媲美JDBC的查询能力  JPA的查询语言是面向对象而非面向数据库的,它以面向对象的自然语法构造查询语句,可以看成是hibernate HQL的等价物。JPA定义了独特的JPQL(Java Persistence Query Language),JPQL是EJB QL的一种扩展,它是针对实体的一种查询语言,操作对象是实体,而不是关系数据库的表,而且能够支持批量更新和修改、JOIN、GROUP BY、HAVING 等通常只有 SQL 才能够提供的高级查询特性,甚至还能够支持子查询。5. 支持面向对象的高级特性


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表