2013-02-03
1.第一个JPA:JPA的配置文件的配置
a.新建web项目:JPATest
b.在src目录下新建META-INF文件夹
c.在META-INF文件夹下:新建

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"> 

 <persistence-unit name="credream" transaction-type="RESOURCE_LOCAL"> 

 <!--name可以自定义写,<persistence-unit :持久化单元:一堆实体bean(的集合)单元的 集




合:transaction-type="JTA":
以前学习的都是本地事物类型;全局事物:在一些应用场合只可以用全局事物
使用全局事物的例子:比如mysql和oracle数据库之间转账;
mysql:update mysql set amount=amount-xx where id=xx(mysql)
oracle:update mysql set amount=amount+xx where id=bb(oracle)
要确保两个动作在同一个事物中执行;
1.采用jdbc:

connection=mysql 

 connection.setAutoCommit(false); 

 mysql:update mysql set amount=amount-xx where id=xx(mysql) 

 oracle:update mysql set amount=amount+xx where id=bb(oracle) 

 connection.commit();


这个事物必须在一个connection中打开,才行,但是这里用了两中数据库:所以用一个


connection实现不了
这时候要用全局事物;本地事物无法满足要求;
2.这时候可以用JTA解决

JTA.getUserTrantion().begin(); 

 connection->mysql 

 connection2->oracle 

 connection->update mysql set amount=amount-xx where id=xx(mysql) 

 connection2-> oracle:update mysql set amount=amount+xx where id=bb(oracle) 

 JTA.getUserTrantion().commit()


如果任何一个出现问题就会回滚事物;
两次提交协议;预编译
1.执行第一条sql,结果为成功的话返回true,否则是false,放到list中,这时候并不是真正


的执行commit,只是模拟下
2.执行第二天sql,同样结果放到list中,如果两条语句,在list中的结果都为true的话,就执


行commit()提交
3.两次提交是指,在执行每一条sql的时候会提交,然后最后有一个总的提


交.JTA.getUserTrantion().commit()
一般在weblojc应用服务器中的时候,采用的全局事物
90%的时候,我们是使用的本地事物;
-->
<!--<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2dd1.auto" value="update"/>
//create-drop在应用启动的时候建立表,在应用关闭的 时候把表删除掉;
update指的是当数据库中没有这个表的时候会创建,有的时候如果有字段更新的话,会自动的


更新

<property name="hibernate.connection.driver_class" 



 value="com.mysql.jdbc.Driver"/> 

 <property name="hibernate.connection.username" value="root"/> 

 <property name="hibernate.connection.password" value="1234"/> 

 <property name="hibernate.connection.url" 



 value="jdbc:mysql://localhost:3306/testdb? 



 userUnicode=true&characterEncoding=utf8"/> 

 </properties> 

 --> 

 <properties> 

 <property name="hibernate.dialect" 



 value="org.hibernate.dialect.MySQL5Dialect"/> 

 <property name="hibernate.connection.driver_class" 



 value="org.gjt.mm.mysql.Driver"/> 

 <property name="hibernate.connection.username" value="root"/> 

 <property name="hibernate.connection.password" value="1234"/> 

 <property name="hibernate.connection.url" 



 value="jdbc:mysql://localhost:3306/testdb? 



 useUnicode=true&characterEncoding=UTF-8"/> 

 <property name="hibernate.hbm2ddl.auto" value="update"/> 

 </properties> 

 </persistence-unit> 

 </persistence>


--------------------------------------------------------------------------
2.JPA的以第一实体bean:
在com.credream.bean包下建立:

Person.java 

 package com.credream.bean; 



 import javax.persistence.Entity; 

 import javax.persistence.GeneratedValue; 

 import javax.persistence.GenerationType; 

 import javax.persistence.Id; 



 @Entity 

 public class Person { 

 
private Integer id; 

 
private String name; 

 
//默认构造函数 

 
public Person() { 

 
// TODO Auto-generated constructor stub 

 
} 

 
//构造方法:用来创建该对象时候给那么赋值 

 
public Person(String name) { 

 
this.name=name; 


 
} 

 
@Id @GeneratedValue(strategy=GenerationType.AUTO)//auto是设置的默认值,可




以不设置:
//@GeneratedValue(strategy=GenerationType.AUTO),5.auto:根据方言智能判断;
//@Id @GeneratedValue
/*采用数据库的id自增长的时候用的这个注解;提供了多种策略:
1.GenerationType.IDENTITY使用数据库id自增长方式,生成主键
2.TABLE:一张表:一个字段:附一个值,另一个记录id,每次增加都自增一下;
3.SEQUENCE:序列的方式
4.table效率不高,mysql,不可以用序列自增长,oracle不可以用自增长
5.auto:根据方言智能判断;
6.auto是设置的默认值,可以不设置:@GeneratedValue(strategy=GenerationType.AUTO)

*/ 

 
public Integer getId() { 

 
return id; 

 
} 

 
public void setId(Integer id) { 

 
this.id = id; 

 
} 

 
public String getName() { 

 
return name; 

 
} 

 
public void setName(String name) { 

 
this.name = name; 

 
} 


 }


-------------------------------------------------------------------------------
2.用junit来测试JPA:
在junit.test包下:

PersonTest.java 

 package junit.test; 



 import javax.persistence.EntityManager; 

 import javax.persistence.EntityManagerFactory; 

 import javax.persistence.Persistence; 



 import org.junit.BeforeClass; 

 import org.junit.Test; 



 import com.credream.bean.Person; 

 public class PersonTest { 

 @BeforeClass 

 public static void setUpBeforeClass()throws Exception{ 

 
} 

 @Test 

 public void save(){ 

 
EntityManagerFactory factory=Persistence.createEntityManagerFactory 



 ("credream");//这个字符串对应的是配置文件中的:<persistence-unit name="credream" 

 
//执行上面这条语句的时候可以反向生成表 

 
//--->sessionFactory-->session--->begin事物 

 
EntityManager em=factory.createEntityManager(); 

 
em.getTransaction().begin();//开始事物 

 //Session.save()-->Persist(); 

 
em.persist(new Person("创梦网络")); //持久化到数据库.persist:持久化 

 
em.getTransaction().commit(); 

 
em.close(); 

 
factory.close(); 


 } 

 }


-----------------------------------------------
a.
log4j:WARN No appenders could be found for logger


(org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.//这时候出现了如下警告
b.查看数据库:
已经生成了person表,并且:在表中插入了创梦网络的数据