netbeans5.5在这个方面做的比较好,可以在IDE里面建立连接,直接从数据库获得结构生成persistence java bean。而且也通过POJO对象生成会话 EJB Facade。

解决方案 »

  1.   

    工具生成的对于CUSTOMER表的映射!package inter;import java.io.Serializable;
    import java.math.BigDecimal;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.Table;/**
     * 实体类 Customer1
     * 
     * @author inter
     */
    @Entity
    @Table(name = "CUSTOMER")
    @NamedQueries( {
            @NamedQuery(name = "Customer1.findById", query = "SELECT c FROM Customer1 c WHERE c.id = :id"),
            @NamedQuery(name = "Customer1.findByName", query = "SELECT c FROM Customer1 c WHERE c.name = :name"),
            @NamedQuery(name = "Customer1.findByPhone", query = "SELECT c FROM Customer1 c WHERE c.phone = :phone")
        })
    public class Customer1 implements Serializable {    @Id
        @Column(name = "ID", nullable = false)
        private BigDecimal id;    @Column(name = "NAME")
        private String name;    @Column(name = "PHONE")
        private String phone;
        
        /** Creates a new instance of Customer1 */
        public Customer1() {
        }    /**
         * 使用指定的值创建 Customer1 的新实例。
         * @param id,Customer1 的 id
         */
        public Customer1(BigDecimal id) {
            this.id = id;
        }    /**
         * 获取此 Customer1 的 id。
         * @return id
         */
        public BigDecimal getId() {
            return this.id;
        }    /**
         * 将此 Customer1 的 id 设置为指定的值。
         * @param id,新建 id
         */
        public void setId(BigDecimal id) {
            this.id = id;
        }    /**
         * 获取此 Customer1 的 name。
         * @return name
         */
        public String getName() {
            return this.name;
        }    /**
         * 将此 Customer1 的 name 设置为指定的值。
         * @param name,新建 name
         */
        public void setName(String name) {
            this.name = name;
        }    /**
         * 获取此 Customer1 的 phone。
         * @return phone
         */
        public String getPhone() {
            return this.phone;
        }    /**
         * 将此 Customer1 的 phone 设置为指定的值。
         * @param phone,新建 phone
         */
        public void setPhone(String phone) {
            this.phone = phone;
        }    /**
         * 返回对象的散列代码值。该实现根据此对象
         * 中 id 字段计算散列代码值。
         * @return 此对象的散列代码值。
         */
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (this.id != null ? this.id.hashCode() : 0);
            return hash;
        }    /**
         * 确定其他对象是否等于此 Customer1。当且仅当
         * 参数不为 null 且该参数是具有与此对象相同 id 字段值的 Customer1 对象时,
         * 结果才为 <code>true</code>。
         * @param 对象,要比较的引用对象
         * 如果此对象与参数相同,则 @return <code>true</code>;
         * 否则为 <code>false</code>。
         */
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Customer1)) {
                return false;
            }
            Customer1 other = (Customer1)object;
            if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
            return true;
        }    /**
         * 返回对象的字符串表示法。该实现根据 id 字段
         * 构造此表示法。
         * @return 对象的字符串表示法。
         */
        @Override
        public String toString() {
            return "inter.Customer1[id=" + id + "]";
        }
        
    }
      

  2.   

    向导生成的控制:package inter;import java.util.List;
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;/**
     *
     * @author inter
     */
    @Stateless
    public class Customer1Facade implements Customer1FacadeRemote {    @PersistenceContext
        private EntityManager em;
        
        /** Creates a new instance of Customer1Facade */
        public Customer1Facade() {
        }    public void create(Customer1 customer1) {
            em.persist(customer1);
        }    public void edit(Customer1 customer1) {
            em.merge(customer1);
        }    public void destroy(Customer1 customer1) {
            em.merge(customer1);
            em.remove(customer1);
        }    public Customer1 find(Object pk) {
            return (Customer1) em.find(Customer1.class, pk);
        }    public List findAll() {
            return em.createQuery("select object(o) from Customer1 as o").getResultList();
        }
        
    }