JPA入门

来源:互联网 发布:dns怎么解析域名 编辑:程序博客网 时间:2024/05/22 13:06

 

1. JPA(Java Persistence API) Java持久化API。是sun公司在ORM领域的规范,旨在结束Hibernate,TopLink等各

    自为政的局面。使用JPA的好处是具有良好的伸缩性和可维护性。

 

2. JPA开发环境的搭建

    需要的jar包:

    Hibernate核心包(8个文件)hibernate-distribution-3.3.1.GA  
   -----------------------------------------------------------
   hibernate3.jar
   lib/bytecode/cglib/hibernate-cglib-repack-2.1_3.jar
   lib/required/*.jar

   Hibernate注解包(3个文件):hibernate-annotations-3.4.0.GA
   ---------------------------------------------------------
   hibernate-annotations.jar
   lib/ejb3-persistence.jar, hibernate-commons-annotations.jar

   Hibernate针对JPA的实现包(3个文件):hibernate-entitymanager-3.4.0.GA  
   ----------------------------------------------------------------------
   hibernate-entitymanager.jar
   lib/test/log4j.jar, slf4j-log4j12.jar

 

    配置文件:persistence.xml,该文件应该放在类路径下的META-INF目录中。

   Transaction-type="RESOURCE_LOCAL"

 

    <?xml version="1.0"?>  

  1.   
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">  
  4.   
  5. <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">  
  6.   
  7.    <properties>  
  8.     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />  
  9.     <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" />  
  10.     <property name="hibernate.connection.username" value="root" />  
  11.     <property name="hibernate.connection.password" value="123456" />  
  12.     <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8" />  
  13.     <property name="hibernate.max_fetch_depth" value="3" />  
  14.     <property name="hib..ernate.show_sql" value="true" />  
  15.     <property name="hibernate.hbm2ddl.auto" value="update"/>  
  16.    </properties>  
  17.   
  18. </persistence-unit>  
  19.   
  20. </persistence> 

    Tansaction-type="JTA"

 

   

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <persistence version="1.0"  
  4. xmlns:persistence="http://java.sun.com/xml/ns/persistence"  
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd ">  
  7.   
  8. <!--   
  9.      Name属性用于定义持久化单元的名字 (name必选,空值也合法);  
  10.      transaction-type 指定事务类型(可选)   
  11. -->  
  12. <persistence-unit name="unitName" transaction-type="JTA">  
  13.   
  14.    <!-- 描述信息.(可选) -->  
  15.    <description> </description>  
  16.   
  17.    <!-- javax.persistence.PersistenceProvider接口的一个实现类(可选) -->  
  18.    <provider>   </provider>  
  19.   
  20.    <!-- Jta-data-source和 non-jta-data-source用于分别指定持久化提供商使用的JTA和/或non-JTA数据源的全局JNDI名称(可选) -->  
  21.    <jta-data-source>java:/MySqlDS</jta-data-source>  
  22.    <non-jta-data-source> </non-jta-data-source>  
  23.   
  24.    <!-- 声明orm.xml所在位置.(可选) -->  
  25.    <mapping-file>product.xml</mapping-file>  
  26.   
  27.    <!-- 以包含persistence.xml的jar文件为基准的相对路径,添加额外的jar文件.(可选) -->  
  28.    <jar-file>../lib/model.jar</jar-file>  
  29.   
  30.    <!-- 显式列出实体类,在Java SE 环境中应该显式列出.(可选) -->  
  31.    <class>com.domain.User</class>  
  32.    <class>com.domain.Product</class>  
  33.   
  34.    <!-- 声明是否扫描jar文件中标注了@Enity类加入到上下文.若不扫描,则如下:(可选) -->  
  35.    <exclude-unlisted-classes/>  
  36.   
  37.    <!--   厂商专有属性(可选)   -->  
  38.    <properties>  
  39.     <!-- hibernate.hbm2ddl.auto= create-drop / create / update -->  
  40.     <property name="hibernate.hbm2ddl.auto" value="update" />  
  41.     <property name="hibernate.show_sql" value="true" />  
  42.    </properties>  
  43.   
  44. </persistence-unit>  
  45.   
  46. </persistence>