Hibernate中使用JPA(註解)配置物件關係對映
阿新 • • 發佈:2019-02-16
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查詢方式中大致語法差不多