论坛首页 Java企业应用论坛

用EJB3.0,不用再Spring+Hibernate

浏览 7307 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2007-09-12  
新闻系统?用啥ejb和hibernate,既然文字都有一M,还不如就用FreeMarker生成静态文本呢。

新闻系统又不是啥事务敏感系统,用得着EJB或者Spring AOP么?


真是瞎胡闹。
0 请登录后投票
   发表时间:2007-09-12  
如果EJB3能成大气,能发展足够的用户量,Glassfish+Netbeans+EJB3这样的傻瓜套餐的开发速度的确比Spring+Hibernate快一些,说起来也规范些。就怕它跑到半路跪倒了,还有其他一些唯Spring是举的OSS对EJB3的支持怎样。
0 请登录后投票
   发表时间:2007-09-12  
I am a big fan of EJB3. But the advantages of EJB3 is not like you said.

Data cache is not supported by EJB3 container,but by its Entity Manager implementation,such as topLink or Hibernate,etc. It has nothing to do with EJB3 container. And for handling big data, your application should adopt some strategy for this issue, not container solve this problem for you. Moreover,transaction management is also supported by Spring+hibernate.

After one year practice,the main advantages of EJB3 I think are as following:

1) The business logic is totally seperated from presentation tier, and can be shared remotely by other sub-system or be wrappered as Web service for outside invocation in future SOA approach.

This is vital for complex enterprise applications.But for small,isolated,dedicated system,it's not a big deal.

2) EJB3 persistence(Entity bean part) is exactly the same with Hibernate3. All the knowledge and experience you got from Hibernate,such as HQL,entity manipulation, will still useful and valuable.


3) The ejb3 entity bean is pojos,which you can use them to communicate with presentaion layer, and DTO(Data Transfer Object) is no longer needed. The Struts or JSF cocomponent can directly refer to this entity bean(anyway,they are detached). After submitting,these detached entity beans can be sent to bussiness logic layer(session bean),without any conversion using DTO or whatever.

    <div class="dark col output-field">
         <h:outputText value="#{productBean.event.product.billingDescription}"/>
    </div>               
    <div class="dark col input-field">
         <h:selectOneMenu id="service" value="#{productBean.event.product.service.id}" validator="#{productBean.validateSelection}" required="true">
             <f:selectItems value="#{productBean.serviceList}"/>
          </h:selectOneMenu>
    </div>

4) In comparison with EJB2,EJB3 is really easy to write,POJOs+annotation.

@Entity
@Table(name = "CMM_PRODUCT_PRO")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "BUNDLE", discriminatorType = DiscriminatorType.STRING)

public class Product extends BasicBaseBean
{  
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ")
    @SequenceGenerator(name="SEQ", sequenceName="product_id", allocationSize=1)
    @Column(name = "ID")
    protected long id;
   
    @Column(name = "SORT_TITLE", nullable = false)
    private String sortTitle;
   
    @Column(name = "BILLING_DESCRIPTION", nullable = false)
    private String billingDescription;

   
    @JoinColumn(name = "SERVICE", referencedColumnName = "ID")
    @ManyToOne
    private Service service=new Service();

      
    @JoinTable(name = "CMM_PRODUCT_GENRE_PRG", joinColumns = {@JoinColumn(name = "PRO_ID", referencedColumnName = "ID")}, inverseJoinColumns =  {@JoinColumn(name = "GENRE", referencedColumnName = "ID")})
    @ManyToMany(fetch=FetchType.LAZY)
    private Set<Genre> genres;
   
    @OneToMany(mappedBy = "product", fetch = FetchType.LAZY)
    private Collection<ImageAsset> imageAssets;

    ...
    public Product() {
    }

    public long getId() {
        return this.id;
    }
   
    public void setId(long id)
    {
        this.id=id;
    }
    ...
}

...

@Stateless
@TransactionManagement(value = TransactionManagementType.CONTAINER)
public class ProductSession implements ProductSessionLocal,ProductSessionRemote

{
    static final Logger log = Logger.getLogger("ProductSession");

    @PersistenceContext(unitName = "oracle")
    private EntityManager em;


    @EJB
    private AdminSessionLocal adminSession;


    /**
     * Find a product entity by its ID
     */
    public Product findProductById(long id) throws AppException, Exception
    {
Product product=null;

try {
    product=(Product)em.find(Product.class,id);
    initProduct(product);
}
         catch(Exception e) {
    throw e;
}

return product;
    }


    /**
     * Create a product
     */
    public ProductResponse createProduct(ProductEvent pe) throws AppException, Exception
    {
Product product=pe.getProduct();

try
        {
    // set product data
    product.setType(CmmejbKeys.PRODUCT);
    product.setPriority((long) 0);
    product.setAdultContent(pe.getIsAdultContent());

    if (pe.product.getChannelBrand().getId()==0)
product.setChannelBrand(null);

    // save genres and moods into ProductGenre
    saveGenreAndMood(product, pe.getGenres(), pe.getMoods());

    em.persist(product);
    em.flush(); // save into database

    em.refresh(product); // refresh from database

    initProduct(product); // initialize product
}
         catch (Exception e) {
    throw e;
}

return new ProductResponse(product);
    }
...
}


5) Thre are more ...

0 请登录后投票
   发表时间:2007-09-12  
I agree with what you said!it seems i used to have a wrong thinking of ejb3.0's advantages.Because I leant about the lifecycle of ejb2.0,in my memory,ejb2.0's cached is managed by the ejb container,so it is in
the ejb3.0!now I should correct my understanding about ejb3.0...thank you for remind me of my wrong thinking and giving me so
valuable advice! by the way,could i make a friends with you? add my qq:441802232,my email:
oak_beijing2008@126.com
0 请登录后投票
   发表时间:2007-09-13  
用ejb3.0即使web层和service层在同一机器,调用的时候也是要通过socket通信的,并发量大的时候,效率非常低,不知道这个问题你考虑过没有?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics