<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    大魚

    JPA 單獨使用

      

    之前一直在用EJB3,但現(xiàn)在想在J2SE里面使用JPA,因為不大喜歡Hibernate的XML文件配置。

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

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

    1.創(chuàng)建web project

    2.添加jpa capabilities,選擇toplink,把自動創(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>
    

     

    生成的幾個類如下:

    實體類:

    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;
    }
    }
    }

     

    還有一個非常有用的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進行測試

    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)建實體類
    Consumer c = new Consumer();
    c.setName("jpa01");
    c.setPassword("jianchi");
    // 開始事務 
    EntityManagerHelper.beginTransaction();
    // 3. 保存
    dao.save(c);
    // 提交事務真正保存實體到數(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());
    }
    }
    }
    

     

    測試實例如下:

    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)建實體類
    Student user = new Student();
    user.setUsername("hellojpa test");
    user.setPassword("jpa password");
    user.setAge(20);
    // 開始事務 
    EntityManagerHelper.beginTransaction();
    // 3. 保存
    dao.save(user);
    // 提交事務真正保存實體到數(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");
    // 開始事務 
    EntityManagerHelper.beginTransaction();
    // 保存(JPA會自動更新變動過的字段)
    dao.update(user);
    // 提交事務真正保存實體到數(shù)據(jù)庫
    EntityManagerHelper.commit();
    // 6. 查找所有年齡為20的用戶,從第1個開始獲取(第0個是第一條記錄)
    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. 刪除
    // 開始事務 
    EntityManagerHelper.beginTransaction();
    // 保存(JPA會自動更新變動過的字段)
    dao.delete(user);
    // 提交事務真正保存實體到數(shù)據(jù)庫
    EntityManagerHelper.commit();
    }
    }

     

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

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


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲免费观看网站| 91精品全国免费观看青青| 99久久免费精品高清特色大片| 亚洲国产一成人久久精品| 国产精品免费一区二区三区| 在线亚洲精品福利网址导航| 国产伦精品一区二区免费| 亚洲午夜国产精品无码| av永久免费网站在线观看| 永久亚洲成a人片777777| a毛片免费全部在线播放** | 国内外成人免费视频| 中文字幕在线观看亚洲日韩| 蜜臀91精品国产免费观看| 免费无码精品黄AV电影| 亚洲色欲啪啪久久WWW综合网| 色播在线永久免费视频| 特级aaaaaaaaa毛片免费视频| 久久久久亚洲精品中文字幕| 亚洲日产乱码一二三区别| 国产极品美女高潮抽搐免费网站| 免费激情网站国产高清第一页| 久久久久亚洲爆乳少妇无| 久久国产精品一区免费下载| 亚洲精品99久久久久中文字幕| 拍拍拍无挡视频免费观看1000| 亚洲成a人片在线观看中文!!!| 日本最新免费不卡二区在线| 久久精品无码专区免费| 亚洲黄网站wwwwww| 国产美女无遮挡免费视频| 91在线视频免费观看| 亚洲中文字幕在线无码一区二区| 国产免费变态视频网址网站| 美女视频黄的免费视频网页| 日本亚洲免费无线码| 亚洲中文久久精品无码| 成人无遮挡裸免费视频在线观看| 好湿好大好紧好爽免费视频| 亚洲人成网站18禁止久久影院 | 中文字幕亚洲综合久久菠萝蜜|