WEB开发网
开发学院图形图像Flash 基于flex4技术从零开发flex博客系统: 7 Using JPA... 阅读

基于flex4技术从零开发flex博客系统: 7 Using JPA

 2009-05-05 12:06:09 来源:WEB开发网   
核心提示: 该类是我从Greeting.java复制的,只改动了几处地方,基于flex4技术从零开发flex博客系统: 7 Using JPA(3),相对之前定义的用于JDO的Greeting,这个实体类定义简单,仅在class上方添加了@Entity注释,及在id上添加主建标识@Id及自增策略

该类是我从Greeting.java复制的,只改动了几处地方。相对之前定义的用于JDO的Greeting,这个实体类定义简单,仅在class上方添加了@Entity注释,及在id上添加主建标识@Id及自增策略。

在sban.flexblog目录下,添加类HelloWorldUsingJPA:

package sban.flexblog;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.transaction.Transaction;

import org.datanucleus.jpa.JPAQuery;

import sban.flexblog.managers.EMFactory;

/**
 * @author sban.li
 *
 */
public class HelloWorldUsingJPA {

        private EntityManager em = EMFactory.getInstance().createEntityManager();

        public Boolean greet2(String user, String content) {
                
                Boolean result = true;
                GreetingEntity greeting = new GreetingEntity(user, content, new Date());

                try {
                        EntityTransaction tx = em.getTransaction();
                        tx.begin();
                        em.persist(greeting);
                        tx.commit();
                } catch (Exception e) {
                        result = false;
                } finally {
                        if (em.isOpen()) em.close();
                }

                return result;
        }
        
        /**
         * find entity by key
         * 
         * @param id
         * @return
         */
        public GreetingEntity getGreetingById(Long id)
        {
                return (GreetingEntity) em.find(GreetingEntity.class, id);
        }
        
        /**
         * edit entity
         * 
         * @param id
         * @param content
         * @return
         */
        public Boolean editGreeting(Long id, String content)
        {
                Boolean result = true;
                
                try {
                        EntityTransaction tx = em.getTransaction();
                        tx.begin();
                        GreetingEntity greeting = getGreetingById(id);
                        greeting.setGreetingContent(content);
                        em.flush();
                        tx.commit();
                } catch (Exception e) {
                        result = false;
                } finally {
                        if (em.isOpen()) em.close();
                }
                
                return result;
        }
        
        /**
         * remove entity
         * 
         * @param id
         * @return
         */
        public Boolean deleteById(Long id) {
                Boolean result = true;

                try {
                        EntityTransaction tx = em.getTransaction();
                        tx.begin();
                        GreetingEntity greeting = getGreetingById(id);
                        em.remove(greeting);
                        tx.commit();
                } catch (Exception e) {
                        result = false;
                } finally {
                        if (em.isOpen()) em.close();
                }

                return result;
        }
        
        /**
         * delete all
         * @return
         */
        @SuppressWarnings(value="unchecked")
        public Boolean deleteAllGreetings() {
                
                Boolean result = true;

                try {
                        
                        JPAQuery query = (JPAQuery)em.createQuery("select from " + GreetingEntity.class.getName());
                        List<GreetingEntity> greeings = query.getResultList();
                        
                        for (GreetingEntity greeting : greeings) {
                                EntityTransaction tx = em.getTransaction();
                                tx.begin();
                                em.remove(greeting);
                                tx.commit();
                        }
                        
                } catch (Exception e) {
                        result = false;
                } finally {
                        if (em.isOpen()) em.close();
                }

                return result;
        }

        /**
         * create query
         * @return
         */
        @SuppressWarnings(value="unchecked")
        public List<GreetingEntity> getAllGreetings() {
                List<GreetingEntity> result;
                
                try {
                        JPAQuery query = (JPAQuery)em.createQuery("select from " + GreetingEntity.class.getName());
                        result = query.getResultList();
                        
                        while (!result.isEmpty() && result.get(result.size() - 1) == null) {
                                result.remove(result.size() - 1);
                        }
                } finally {
                        if (em.isOpen()) em.close();
                }

                return result;
        }
}

上一页  1 2 3 4 5  下一页

Tags:基于 flex 技术

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接