I haven't used EntityManagers directly, but your code looks fine to me,
especially if it works ;-)

You may want to wrap your flush( ) call in a try/catch block in case
something goes wrong, and roll back the transaction in the catch block.

Once you get more comfortable with things (and if you're using Hibernate),
you may want to check out some of the integration packages (Tapernate,
Honeycomb, etc) that handle transaction scoping for you. Spring might also
be a good option if you end up splitting your app up into presentation /
service / persistence layers.

On 12/6/06, Cyrille37 <[EMAIL PROTECTED]> wrote:

Hello,

I'm coming back with a beginner question ;-)

After 3 weeks of Java Web App discovering, I'm actually working with
Tapestry 4.0.2, Netbeans 5.5 which give me easy data persistence support
with JPA Toplink essential, and Tomcat5.5 and Mysql5.

So I've finaly got something working with Beanform and JPA and I come to
you to get comments and ideas and best practice.

For the persistence, the method I've found is to do the following :

* To remember with object I'm playing with :

    @Persist("client")
    public abstract int getProductId();
    public abstract void setProductId(int productId);

* To manage the Beanform's bean :

    private Product _product ;
    public Product getProduct()
    {
        if( this._product==null && this.getProductId()>0 )
        {
            this.setProduct(  this.loadProduct( this.getProductId ()) );
        }
        return this._product ;
    }

    public void setProduct( Product product )
    {
        this._product = product ;
    }

* For initializing the page at the first time :

    public void activateExternalPage(Object[] parameters, IRequestCycle
cycle)
    {
        int pid = Integer.parseInt( parameters[0].toString());
        setProductId( pid );
        this.setProduct ( this.loadProduct(pid) );
    }

* And finally to implement the Beanform 'save' listener :

    public String save()
    {
        System.out.println( "ProductEdit.save()" );
        if( this.getDelegate().getHasErrors() )
            return null;
        EntityManager em = getDataManagerFactory().GetEntityManager();
        /*** I find that method a beat strange but it works ! ***/
        em.getTransaction ().begin();
        em.flush();
        em.getTransaction().commit();

        return "Home";
    }

Thanks to have read my poetry ;-)
Cyrille.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to