Hibernate中使用JPA(注解)配置对象关系映射

java中注解也是一大特点,平时进行单元测试时我们用过@Test注解进行测试

JPA就是java专门针对持久层框架进行设计的一套规范

JPA:Java Persistence API,其实它也就是一堆接口,就想JDBC一样,不同的框架只要遵循这同一套规范就可以在java环境中使用。

我们都指定在使用Hibernate的时候我们要写很多的.xml配置文件,xxx.hbm.xml对象关系映射文件,hibernate.cfg.xml核心配置文件

JPA就是利用注解代替了xxx.hbm.xml这些映射文件,hibernate.cfg.xml这个核心配置文件在JPA这套规范中也换了名字,但是做的事情是差不多的

先看看JPA是怎么使用注解代替映射文件的


@Entity
@Table(name="t_customer")
public class Customer{
				
	@Id
	@Column(name="cust_id")
	@GenericGenerator(name="sysnative",strategy="native")
	@GeneratedValue(generator="sysnative")
	private Long custId;
	@Column(name="cust_name")
	private String custName;
	@Column(name="cust_sex")
	private String custSex;
				
}



核心配置文件:

之前的Hibernate核心配置文件时hibernate.cfg.xml

<hibernate-configuration>
	<!-- 配置Hibernate核心文件 -->
	<session-factory>
		<!-- 必须的配置:数据库连接配置 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///ssh</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<!-- 数据库方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 可选配置:优化开发 -->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">none</property>
		<!-- 配置使用C3P0连接池:默认存在有一个连接池 -->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!-- 配置绑定线程 -->
		<property name="hibernate.current_session_context_class">thread</property>
		<!-- 映射文件 -->
		<mapping resource="cn/test/domain/Customer.hbm.xml" />
	</session-factory>
</hibernate-configuration>

在JPA中的核心配置文件是 persistence.xml,而且它的存放位置也不一样,它的存放位置是src/META-INF/persistence.xml,而且名字必须是persistence.xml

persistence.xml 的内容

<?xml version="1.0" encoding="UTF-8"?>
<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">
	
	<!--Name属性用于定义持久化单元的名字 (name必选,空值也合法); transaction-type 指定事务类型(可选) 取值: JTA:默认值 
		RESOURCE_LOCAL -->
	<persistence-unit name="myPersistUnit" transaction-type="RESOURCE_LOCAL">
		<!-- javax.persistence.PersistenceProvider接口的一个实现类 -->
		<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
		<!-- 显式列出实体类,在Java SE 环境中应该显式列出.
		<class>cn.itcast.domain.Customer</class>
		 -->
		<!--厂商专有属性(可选) 我们用的Hibernate,后面都是hibernate.cfg.xml中配置 -->
		<properties>
			<!-- 数据库的连接信息 -->
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/custdate" />
			<property name="hibernate.connection.username" value="root" />
			<property name="hibernate.connection.password" value="root" />
			<!-- 指定方言 -->
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<!-- 是否显示SQL语句 -->
			<property name="hibernate.show_sql" value="true" />
			<!-- 是否格式化SQL语句 -->
			<property name="hibernate.format_sql" value="true" />
			<!-- 生成DDL的策略 -->
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>
</persistence>  



在JPA中的对象操作的API也有所不同

在之前Hibernate中,使用SessionFactory来创建Session,使用Session创建事务操作数据库

在JPA中,使用的是

EntityManagerFactory emFactory = Persistence.createEntitiyManagerFactory("myPersistence");//是核心配置文件中的name值

EntityManager em = emFactory.createEntityManager();

/**
	 * 单表添加
	 */
	public void addPer(){
		EntityManager em = EntityManagerFactoryUtil.createEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		Person p = new Person();
		p.setUname("哈哈哈");
		em.persist(p);
		tx.commit();
	}


/**
	 * 单表查询
	 */
	public void getPer(){
		EntityManager em = EntityManagerFactoryUtil.createEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		Person person = em.find(Person.class, 1);
		System.out.println(person);
		tx.commit();
	}


/**
	 * 单表更新
	 */
	public void updatePer(){
		EntityManager em = EntityManagerFactoryUtil.createEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		Person person = em.find(Person.class, 1);
		person.setUname("呵呵呵");
		tx.commit();
	}

数据操作其实和HQL查询方式中大致语法差不多


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值