JavaEE5学习笔记08-JPA与EJB集成总结(2)

下面来看针对此实体操作的sessionBean代码。

接口

package jpa.eao;

  

@Remote

public interface IPersonEAO {

 

    public void save(Person entity);

    public void delete(Person entity);

    public Person update(Person entity);

    public Person findById(Integer id);

    public List<Person> findByProperty(String propertyName, Object value);

    public List<Person> findByName(Object name);

    public List<Person> findAll();

}

实现类

 

@Stateless

@TransactionManagement(TransactionManagementType.CONTAINER)

@TransactionAttribute(TransactionAttributeType.REQUIRED)

public class PersonEAO implements IPersonEAO {

    // property constants

    public static final String NAME = "name";

   

    /**

     * 容器资源注入的实体操作管理器

     */

    @PersistenceContext(unitName = "JavaEE4JbossPU")

    private EntityManager entityManager;

   

    /**

     * 增加记录

     */

    public void save(Person entity) {

       try {

           entityManager.persist(entity);

       } catch (RuntimeException re) {

           throw re;

       }

    }

   

    /**

     * 删除记录

     */

    public void delete(Person entity) {

       try {

           entity = entityManager.getReference(Person.class, entity.getId());

           entityManager.remove(entity);

       } catch (RuntimeException re) {

           throw re;

       }

    }

   

    /**

     * 更新记录

     */

    public Person update(Person entity) {

       try {

           Person result = entityManager.merge(entity);

           return result;

       } catch (RuntimeException re) {

           throw re;

       }

    }

   

    /**

     * 查询单独实体记录

     */

    public Person findById(Integer id) {

       try {

           Person instance = entityManager.find(Person.class, id);

           return instance;

       } catch (RuntimeException re) {

           throw re;

       }

    }

   

    /**

     * 按属性查找实体集合

     */

    @SuppressWarnings("unchecked")

    public List<Person> findByProperty(String propertyName, final Object value) {

       try {

           final String queryString = "select model from Person model where model."

                  + propertyName + "= :propertyValue";

           Query query = entityManager.createQuery(queryString);

           query.setParameter("propertyValue", value);

           return query.getResultList();

       } catch (RuntimeException re) {

           throw re;

       }

    }

   

    /**

     * name查找实体集合

     */

    public List<Person> findByName(Object name) {

       return findByProperty(NAME, name);

    }

   

    /**

     * 查询所有的实体记录

     */

    @SuppressWarnings("unchecked")

    public List<Person> findAll() {

       try {

           final String queryString = "select model from Person model";

           Query query = entityManager.createQuery(queryString);

           return query.getResultList();

       } catch (RuntimeException re) {

           throw re;

       }

    }

}

这个就是一个一般的会话Bean,里面是容器注入了实体管理器EntityManager,通过实体管理器对实体进行增删改查操作。

你可能感兴趣的:(bean,jpa,ejb)