第一个JPA程序

JPA是最新的Java ORM规范。在EJB3中,JPA用来实现持久化,取代原来的Entity Bean。
关于实现的版本,现在主要有OpenJPA和Hibernate,个人感觉这两个实现版本没有什么太大的区别,
因为我打算在JBoss下练习EJB3,所以采用了Hibernate的实现。
目录结构如果下:
src
|
|--divinemind.......
|
|--META-INF
        |-MANIFEST.MF
        |-orm.xml
        |-persistence.xml
1.persistence.xml
a.我用的Hibernate的实现
<provider>org.hibernate.ejb.HibernatePersistence</provider>
b.用Annotation而不是xml
<class>divinemind.onlyfun.jpa.entity.User</class>
  ※如果想用xml进行映射,可以删除<class>...</class>之后,添加<mapping-file>/META-INF/orm.xml</mapping-file>
c.剩下的就是数据库的设置了
具体代码如下:

<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" 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"> <persistence-unit name="JPADemo" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>divinemind.onlyfun.jpa.entity.User</class> <!--<mapping-file>/META-INF/orm.xml</mapping-file>--> <properties> <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver" /> <property name="hibernate.connection.url" value="jdbc:derby:D:/Project/002/JavaDB/T11111T;bootPassword=aLNwHOVoVGZ9N9zOsWf3Bw==" /> <property name="hibernate.connection.username" value="T11111T" /> <property name="hibernate.connection.password" value="aLNwHOVoVGZ9N9zOsWf3Bw==" /> <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" /> <property name="hibernate.show_sql" value="false" /> <property name="minPoolSize" value="5" /> <property name="initialPoolSize" value="10" /> <property name="maxPoolSize" value="200" /> <property name="idleConnectionTestPeriod" value="120" /> <property name="acquireIncrement" value="10" /> <property name="checkoutTimeout" value="3600" /> <property name="numHelperThreads" value="4" /> <property name="maxStatements" value="400" /> <property name="maxStatementsPerConnection" value="20" /> <property name="maxIdleTime" value="180" /> <property name="acquireRetryAttempts" value="30" /> </properties> </persistence-unit> </persistence>

 

 2.orm.xml
这个很简单,没有什么好说的。

<?xml version="1.0" encoding="UTF-8"?> <entity-mappings version="1.0" xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"> <entity class="divinemind.onlyfun.jpa.entity.User"> <table name="USER_INFO"></table> <attributes> <id name="userId"> <column name="USER_ID" /> </id> <basic name="userName"> <column name="USER_NAME" /> </basic> <basic name="password"> <column name="USER_PASSWORD" /> </basic> </attributes> </entity> </entity-mappings>

3.User.java

很明显,这里的annotation和上面的orm.xml的效果是一样的。但是个人感觉如果能用annotation就用annotation,因为我感觉代码总是比xml看起来要舒服点。

package divinemind.onlyfun.jpa.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "USER_INFO") public class User { @Id @Column(name = "USER_ID") private String userId; @Column(name = "USER_NAME") private String userName; @Column(name = "USER_PASSWORD") private String password; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

 4.测试代码

package divinemind.onlyfun.jpa.dao.impl; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.Query; import divinemind.onlyfun.jpa.dao.UserDAO; import divinemind.onlyfun.jpa.entity.User; public class UserDAOImpl implements UserDAO { public List<User> getUserByJPQL(String jpql) { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPADemo"); EntityManager entityManager = factory.createEntityManager(); Query query = entityManager.createQuery(jpql); List<User> userList = query.getResultList(); return userList; } public User getUserByPrimaryKey(String userId) { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPADemo"); EntityManager entityManager = factory.createEntityManager(); return entityManager.find(User.class, userId); } public void persistUser(User user) { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPADemo"); EntityManager entityManager = factory.createEntityManager(); entityManager.getTransaction().begin(); entityManager.persist(user); entityManager.getTransaction().commit(); } public void removeUser(String userId) { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPADemo"); EntityManager entityManager = factory.createEntityManager(); entityManager.getTransaction().begin(); User userFromDB = entityManager.find(User.class, userId); if (userFromDB != null) { entityManager.remove(userFromDB); } entityManager.getTransaction().commit(); } public void updateUser(User user) { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPADemo"); EntityManager entityManager = factory.createEntityManager(); entityManager.getTransaction().begin(); entityManager.merge(user); entityManager.getTransaction().commit(); } }

上面的代码其实问题还是蛮多的,每个方法都需要自己创建EntityManager,自己控制事务,这些最好是托管给IOC容器。
后面再讨论这个问题

你可能感兴趣的:(Hibernate,String,jpa,user,Class,encoding)