大魚

          JPA 單獨(dú)使用

            

          之前一直在用EJB3,但現(xiàn)在想在J2SE里面使用JPA,因?yàn)椴淮笙矚gHibernate的XML文件配置。

          而使用DB4O的時(shí)候,出現(xiàn)了一些莫名其妙的問題,一直沒解決,就暫時(shí)擱下了。

          使用myeclipse 6開發(fā)jpa如下:

          1.創(chuàng)建web project

          2.添加jpa capabilities,選擇toplink,把自動(dòng)創(chuàng)建數(shù)據(jù)庫的和更新persistence.xml的勾上

          3.切換成database explorer視圖,選擇表,jpa逆向工程。

          4.測試

          生成的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="JPAWebPU"
          transaction-type="RESOURCE_LOCAL">
          <provider>
          oracle.toplink.essentials.PersistenceProvider
          </provider>
          <class>com.persia.entity.Consumer</class>
          <properties>
          <property name="toplink.jdbc.driver"
          value="com.mysql.jdbc.Driver" />
          <property name="toplink.jdbc.url"
          value="jdbc:mysql://localhost:3306/ejb3" />
          <property name="toplink.jdbc.user" value="root" />
          <property name="toplink.jdbc.password" value="root" />
          <property name="toplink.ddl-generation"
          value="create-tables" />
          </properties>
          </persistence-unit>
          </persistence>
          

           

          生成的幾個(gè)類如下:

          實(shí)體類:

          package com.persia.entity;
          import javax.persistence.Column;
          import javax.persistence.Entity;
          import javax.persistence.Id;
          import javax.persistence.Table;
          /**
          * Consumer entity.
          *
          * @author MyEclipse Persistence Tools
          */
          @Entity
          @Table(name = "consumer", catalog = "ejb3", uniqueConstraints = {})
          public class Consumer implements java.io.Serializable {
          // Fields
          private Integer id;
          private String name;
          private String password;
          // Constructors
          /** default constructor */
          public Consumer() {
          }
          /** full constructor */
          public Consumer(Integer id, String name, String password) {
          this.id = id;
          this.name = name;
          this.password = password;
          }
          // Property accessors
          @Id
          @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true)
          public Integer getId() {
          return this.id;
          }
          public void setId(Integer id) {
          this.id = id;
          }
          @Column(name = "name", unique = false, nullable = false, insertable = true, updatable = true, length = 45)
          public String getName() {
          return this.name;
          }
          public void setName(String name) {
          this.name = name;
          }
          @Column(name = "password", unique = false, nullable = false, insertable = true, updatable = true, length = 45)
          public String getPassword() {
          return this.password;
          }
          public void setPassword(String password) {
          this.password = password;
          }
          }
           

          DAO接口

          package com.persia.entity;
          import java.util.List;
          /**
          * Interface for ConsumerDAO.
          *
          * @author MyEclipse Persistence Tools
          */
          public interface IConsumerDAO {
          /**
          * Perform an initial save of a previously unsaved Consumer entity. All
          * subsequent persist actions of this entity should use the #update()
          * method. This operation must be performed within the a database
          * transaction context for the entity's data to be permanently saved to the
          * persistence store, i.e., database. This method uses the
          * {@link javax.persistence.EntityManager#persist(Object) EntityManager#persist}
          * operation.
          *
          * <pre>
          * EntityManagerHelper.beginTransaction();
          * IConsumerDAO.save(entity);
          * EntityManagerHelper.commit();
          * </pre>
          *
          * @param entity
          *            Consumer entity to persist
          * @throws RuntimeException
          *             when the operation fails
          */
          public void save(Consumer entity);
          /**
          * Delete a persistent Consumer entity. This operation must be performed
          * within the a database transaction context for the entity's data to be
          * permanently deleted from the persistence store, i.e., database. This
          * method uses the
          * {@link javax.persistence.EntityManager#remove(Object) EntityManager#delete}
          * operation.
          *
          * <pre>
          * EntityManagerHelper.beginTransaction();
          * IConsumerDAO.delete(entity);
          * EntityManagerHelper.commit();
          * entity = null;
          * </pre>
          *
          * @param entity
          *            Consumer entity to delete
          * @throws RuntimeException
          *             when the operation fails
          */
          public void delete(Consumer entity);
          /**
          * Persist a previously saved Consumer entity and return it or a copy of it
          * to the sender. A copy of the Consumer entity parameter is returned when
          * the JPA persistence mechanism has not previously been tracking the
          * updated entity. This operation must be performed within the a database
          * transaction context for the entity's data to be permanently saved to the
          * persistence store, i.e., database. This method uses the
          * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
          * operation.
          *
          * <pre>
          * EntityManagerHelper.beginTransaction();
          * entity = IConsumerDAO.update(entity);
          * EntityManagerHelper.commit();
          * </pre>
          *
          * @param entity
          *            Consumer entity to update
          * @returns Consumer the persisted Consumer entity instance, may not be the
          *          same
          * @throws RuntimeException
          *             if the operation fails
          */
          public Consumer update(Consumer entity);
          public Consumer findById(Integer id);
          /**
          * Find all Consumer entities with a specific property value.
          *
          * @param propertyName
          *            the name of the Consumer property to query
          * @param value
          *            the property value to match
          * @return List<Consumer> found by query
          */
          public List<Consumer> findByProperty(String propertyName, Object value);
          public List<Consumer> findByName(Object name);
          public List<Consumer> findByPassword(Object password);
          /**
          * Find all Consumer entities.
          *
          * @return List<Consumer> all Consumer entities
          */
          public List<Consumer> findAll();
          }

           

          DAO類

          package com.persia.entity;
          import java.util.List;
          import java.util.logging.Level;
          import javax.persistence.EntityManager;
          import javax.persistence.Query;
          /**
          * A data access object (DAO) providing persistence and search support for
          * Consumer entities. Transaction control of the save(), update() and delete()
          * operations must be handled externally by senders of these methods or must be
          * manually added to each of these methods for data to be persisted to the JPA
          * datastore.
          *
          * @see com.persia.entity.Consumer
          * @author MyEclipse Persistence Tools
          */
          public class ConsumerDAO implements IConsumerDAO {
          // property constants
          public static final String NAME = "name";
          public static final String PASSWORD = "password";
          private EntityManager getEntityManager() {
          return EntityManagerHelper.getEntityManager();
          }
          /**
          * Perform an initial save of a previously unsaved Consumer entity. All
          * subsequent persist actions of this entity should use the #update()
          * method. This operation must be performed within the a database
          * transaction context for the entity's data to be permanently saved to the
          * persistence store, i.e., database. This method uses the
          * {@link javax.persistence.EntityManager#persist(Object) EntityManager#persist}
          * operation.
          *
          * <pre>
          * EntityManagerHelper.beginTransaction();
          * ConsumerDAO.save(entity);
          * EntityManagerHelper.commit();
          * </pre>
          *
          * @param entity
          *            Consumer entity to persist
          * @throws RuntimeException
          *             when the operation fails
          */
          public void save(Consumer entity) {
          EntityManagerHelper.log("saving Consumer instance", Level.INFO, null);
          try {
          getEntityManager().persist(entity);
          EntityManagerHelper.log("save successful", Level.INFO, null);
          } catch (RuntimeException re) {
          EntityManagerHelper.log("save failed", Level.SEVERE, re);
          throw re;
          }
          }
          /**
          * Delete a persistent Consumer entity. This operation must be performed
          * within the a database transaction context for the entity's data to be
          * permanently deleted from the persistence store, i.e., database. This
          * method uses the
          * {@link javax.persistence.EntityManager#remove(Object) EntityManager#delete}
          * operation.
          *
          * <pre>
          * EntityManagerHelper.beginTransaction();
          * ConsumerDAO.delete(entity);
          * EntityManagerHelper.commit();
          * entity = null;
          * </pre>
          *
          * @param entity
          *            Consumer entity to delete
          * @throws RuntimeException
          *             when the operation fails
          */
          public void delete(Consumer entity) {
          EntityManagerHelper.log("deleting Consumer instance", Level.INFO, null);
          try {
          entity = getEntityManager().getReference(Consumer.class,
          entity.getId());
          getEntityManager().remove(entity);
          EntityManagerHelper.log("delete successful", Level.INFO, null);
          } catch (RuntimeException re) {
          EntityManagerHelper.log("delete failed", Level.SEVERE, re);
          throw re;
          }
          }
          /**
          * Persist a previously saved Consumer entity and return it or a copy of it
          * to the sender. A copy of the Consumer entity parameter is returned when
          * the JPA persistence mechanism has not previously been tracking the
          * updated entity. This operation must be performed within the a database
          * transaction context for the entity's data to be permanently saved to the
          * persistence store, i.e., database. This method uses the
          * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
          * operation.
          *
          * <pre>
          * EntityManagerHelper.beginTransaction();
          * entity = ConsumerDAO.update(entity);
          * EntityManagerHelper.commit();
          * </pre>
          *
          * @param entity
          *            Consumer entity to update
          * @returns Consumer the persisted Consumer entity instance, may not be the
          *          same
          * @throws RuntimeException
          *             if the operation fails
          */
          public Consumer update(Consumer entity) {
          EntityManagerHelper.log("updating Consumer instance", Level.INFO, null);
          try {
          Consumer result = getEntityManager().merge(entity);
          EntityManagerHelper.log("update successful", Level.INFO, null);
          return result;
          } catch (RuntimeException re) {
          EntityManagerHelper.log("update failed", Level.SEVERE, re);
          throw re;
          }
          }
          public Consumer findById(Integer id) {
          EntityManagerHelper.log("finding Consumer instance with id: " + id,
          Level.INFO, null);
          try {
          Consumer instance = getEntityManager().find(Consumer.class, id);
          return instance;
          } catch (RuntimeException re) {
          EntityManagerHelper.log("find failed", Level.SEVERE, re);
          throw re;
          }
          }
          /**
          * Find all Consumer entities with a specific property value.
          *
          * @param propertyName
          *            the name of the Consumer property to query
          * @param value
          *            the property value to match
          * @return List<Consumer> found by query
          */
          @SuppressWarnings("unchecked")
          public List<Consumer> findByProperty(String propertyName, final Object value) {
          EntityManagerHelper.log("finding Consumer instance with property: "
          + propertyName + ", value: " + value, Level.INFO, null);
          try {
          final String queryString = "select model from Consumer model where model."
          + propertyName + "= :propertyValue";
          Query query = getEntityManager().createQuery(queryString);
          query.setParameter("propertyValue", value);
          return query.getResultList();
          } catch (RuntimeException re) {
          EntityManagerHelper.log("find by property name failed",
          Level.SEVERE, re);
          throw re;
          }
          }
          public List<Consumer> findByName(Object name) {
          return findByProperty(NAME, name);
          }
          public List<Consumer> findByPassword(Object password) {
          return findByProperty(PASSWORD, password);
          }
          /**
          * Find all Consumer entities.
          *
          * @return List<Consumer> all Consumer entities
          */
          @SuppressWarnings("unchecked")
          public List<Consumer> findAll() {
          EntityManagerHelper.log("finding all Consumer instances", Level.INFO,
          null);
          try {
          final String queryString = "select model from Consumer model";
          Query query = getEntityManager().createQuery(queryString);
          return query.getResultList();
          } catch (RuntimeException re) {
          EntityManagerHelper.log("find all failed", Level.SEVERE, re);
          throw re;
          }
          }
          }

           

          還有一個(gè)非常有用的helper

          package com.persia.entity;
          import java.util.logging.Level;
          import java.util.logging.Logger;
          import javax.persistence.EntityManager;
          import javax.persistence.EntityManagerFactory;
          import javax.persistence.Persistence;
          import javax.persistence.Query;
          /**
          * @author MyEclipse Persistence Tools
          */
          public class EntityManagerHelper {
          private static final EntityManagerFactory emf;
          private static final ThreadLocal<EntityManager> threadLocal;
          private static final Logger logger;
          static {
          emf = Persistence.createEntityManagerFactory("JPAWebPU");
          threadLocal = new ThreadLocal<EntityManager>();
          logger = Logger.getLogger("JPAWebPU");
          logger.setLevel(Level.ALL);
          }
          public static EntityManager getEntityManager() {
          EntityManager manager = threadLocal.get();
          if (manager == null || !manager.isOpen()) {
          manager = emf.createEntityManager();
          threadLocal.set(manager);
          }
          return manager;
          }
          public static void closeEntityManager() {
          EntityManager em = threadLocal.get();
          threadLocal.set(null);
          if (em != null) em.close();
          }
          public static void beginTransaction() {
          getEntityManager().getTransaction().begin();
          }
          public static void commit() {
          getEntityManager().getTransaction().commit();
          }
          public static void rollback() {
          getEntityManager().getTransaction().rollback();
          }
          public static Query createQuery(String query) {
          return getEntityManager().createQuery(query);
          }
          public static void log(String info, Level level, Throwable ex) {
          logger.log(level, info, ex);
          }
          }
          

           

          然后使用helper和dao進(jìn)行測試

          package com.jpa.test;
          import java.util.List;
          import com.persia.entity.Consumer;
          import com.persia.entity.ConsumerDAO;
          import com.persia.entity.EntityManagerHelper;
          public class JPATest {
          /**
          * @param args
          */
          public static void main(String[] args) {
          // 1.創(chuàng)建 DAO
          ConsumerDAO dao = new ConsumerDAO();
          // 2.創(chuàng)建實(shí)體類
          Consumer c = new Consumer();
          c.setName("jpa01");
          c.setPassword("jianchi");
          // 開始事務(wù) 
          EntityManagerHelper.beginTransaction();
          // 3. 保存
          dao.save(c);
          // 提交事務(wù)真正保存實(shí)體到數(shù)據(jù)庫
          EntityManagerHelper.commit();
          // 4. 列出數(shù)據(jù)庫中所有對象
          List<Consumer> result = dao.findAll();
          for(Consumer o : result) {
          System.out.println(o.getId());
          System.out.println(o.getName());
          }
          }
          }
          

           

          測試實(shí)例如下:

          package test;
          import java.util.List;
          import dao.*;
          public class JPATest {
          public static void main(String[] args) {
          // 1.創(chuàng)建 DAO
          StudentDAO dao = new StudentDAO();
          // 2.創(chuàng)建實(shí)體類
          Student user = new Student();
          user.setUsername("hellojpa test");
          user.setPassword("jpa password");
          user.setAge(20);
          // 開始事務(wù) 
          EntityManagerHelper.beginTransaction();
          // 3. 保存
          dao.save(user);
          // 提交事務(wù)真正保存實(shí)體到數(shù)據(jù)庫
          EntityManagerHelper.commit();
          // 4. 列出數(shù)據(jù)庫中所有對象
          List<Student> result = dao.findAll();
          for(Student o : result) {
          System.out.println(o.getId());
          System.out.println(o.getUsername());
          }
          // 5. 更改用戶名
          user.setUsername("測試JPA");
          // 開始事務(wù) 
          EntityManagerHelper.beginTransaction();
          // 保存(JPA會(huì)自動(dòng)更新變動(dòng)過的字段)
          dao.update(user);
          // 提交事務(wù)真正保存實(shí)體到數(shù)據(jù)庫
          EntityManagerHelper.commit();
          // 6. 查找所有年齡為20的用戶,從第1個(gè)開始獲?。ǖ?個(gè)是第一條記錄)
          result = dao.findByAge(20, 1);
          for(Student o : result) {
          System.out.println(o.getId());
          System.out.println(o.getUsername());
          }
          // 7. 根據(jù) ID 查找
          user = dao.findById(2);
          System.out.println(user.getUsername());
          // 8. 刪除
          // 開始事務(wù) 
          EntityManagerHelper.beginTransaction();
          // 保存(JPA會(huì)自動(dòng)更新變動(dòng)過的字段)
          dao.delete(user);
          // 提交事務(wù)真正保存實(shí)體到數(shù)據(jù)庫
          EntityManagerHelper.commit();
          }
          }

           

          若部署到tomcat,沒有什么問題,也不要額外添加什么。

          posted on 2009-03-16 00:09 大魚 閱讀(793) 評論(0)  編輯  收藏 所屬分類: JPA


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 东至县| 杂多县| 如东县| 油尖旺区| 内黄县| 大悟县| 民丰县| 保靖县| 五家渠市| 霍山县| 慈利县| 巴青县| 黄平县| 隆化县| 南康市| 芜湖县| 乐陵市| 分宜县| 五河县| 北京市| 阿克| 靖州| 临武县| 桐乡市| 新平| 小金县| 甘孜县| 五原县| 雷山县| 福安市| 呼和浩特市| 资中县| 明星| 普定县| 溧水县| 马尔康县| 南通市| 琼海市| 延边| 阿尔山市| 潮州市|