-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: VO from Client t o Server
PostPosted: Tue Oct 31, 2006 2:53 pm 
Regular
Regular

Joined: Thu Feb 24, 2005 2:34 pm
Posts: 80
Hi i have this scenario, i'm using Flex 2 with hibernate i have simple form that user compile a generic Product.
Then i my DaoXXX receive this Product Object, when receive this object i made some control see my code
Code:
             public void Product(ProductVO vo)
      dao.getSession().getTransaction().begin();
      List result= dao.findByCod(vo.getCod());
                ProductVO vo=(ProductVO)result.get(0);
               ......
      dao.getSession().getTransaction().commit();

but i have some problems
1) i can receive a ProductVO without primary key but an object with this "cod" is already exits in my database! Then i wish to update the object that i have loaded with object that arrive from client. (Attention cod is not pk)
Can you give me some suggestion
Devis


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 10:05 am 
Newbie

Joined: Mon Sep 18, 2006 6:34 am
Posts: 18
It's staright forward to save a VO, I'm doing it using Flex 2.

Instead of finding a product and attempting to resave it, just do the following:

public void saveProduct(ProductVO productVO) {
dao.getSession().getTransaction().begin();
dao.getSession().saveOrUpdate(productVO);
dao.getSession().getTransaction().commit();
}

Use the finder to get the initial product information for the client only. You can use dirty flags too in a BaseVO to ensure that you're not calling the saveProduct unnessecarily.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 2:04 pm 
Regular
Regular

Joined: Thu Feb 24, 2005 2:34 pm
Posts: 80
Thanks for your help.
But if i'm using saveOrUpdate Hibernate , hibernate check my VO by primary key. In my case i can have some vo identical but with PK null, then in this case Hibernate with SaveOrUpdate will inser into a new record because pk is null.
Ok i hope to write correctly:
First Case OK
if into a Flex i change an existis BaseVO with one PK for me it's correctly that hibernate update

Second Case OK
if into a Flex i change an existis BaseVO without PK=NULL for me it's correctly that hibernate insert

HERE I HAVE A PROBLEM
But if my user create a new BaseVO with an email that already exists?i can use SaveOrUpdate i think.

About dirty flags, i don't know have you some link about.... this.
Thanks again for your help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 4:51 am 
Newbie

Joined: Mon Sep 18, 2006 6:34 am
Posts: 18
We're using seperate insert method and an update methods, i.e.:

public final BasePk save(final BaseVO vo) {
try {
final Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(vo);

// return the VO pk object
tx.commit();
HibernateUtil.closeSession();
return vo.createPk();
} catch (final Exception ex) {
System.out.println(ex);
throw new DAOException(ex.getMessage());
}
}

and

public final void update(final BasePk pk, final BaseVO vo) {
BaseVO baseVO = findByPrimaryKey(pk);
save(vo);
}

This should get over the null problem as you are doing the find in the update before save is called.

VO dirty flags are used to indicate if a VO property has been modified, take a look at:

http://authors.phptr.com/corej2eepatter ... ap8_2.html

It might come in handy for you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 5:36 am 
Regular
Regular

Joined: Thu Feb 24, 2005 2:34 pm
Posts: 80
thanks for your help but i'm a bit confuse with your BasePK. you are using a composteID?


public final BasePk save(final BaseVO vo) {
try {
final Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(vo);

// return the VO pk object
tx.commit();
HibernateUtil.closeSession();
return vo.createPk(); Here return your pk ?
} catch (final Exception ex) {
System.out.println(ex);
throw new DAOException(ex.getMessage());
}
}

and


Then you externalize your your primary key?

public final void update(final BasePk pk, final BaseVO vo) {
BaseVO baseVO = findByPrimaryKey(pk);
save(vo);
} [/code]


Thanks for the link about diry flags, good point to understand.
Devis


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 5:41 am 
Newbie

Joined: Mon Sep 18, 2006 6:34 am
Posts: 18
No, we're using BasePk as a convenience class for other VOs and really isn't necessary if you have only a few VOs. You can have the return type as anything you want.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 5:47 am 
Regular
Regular

Joined: Thu Feb 24, 2005 2:34 pm
Posts: 80
ok your help it's great for me...
thank you very much


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 6:12 am 
Regular
Regular

Joined: Thu Feb 24, 2005 2:34 pm
Posts: 80
Sorry, but you save vo (that it arrived from flex),
but when you call save(vo)....????
I have made a little test, but in my code hibernate always insert...
public final void update(final BasePk pk, final BaseVO vo) {
BaseVO baseVO = findByPrimaryKey(pk);
save(vo);
}
here i don't understand very well..
You load an object into baseVO and then call save with your object from flex?
Thanks devis


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 6:16 am 
Newbie

Joined: Mon Sep 18, 2006 6:34 am
Posts: 18
Yeah, it's kind of funny how it works like that. We're doing a find on the old VO (referenced by Pk in update() method) and saving the new VO (passed from update() to save() method). Looks strange but it works for us.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 6:30 am 
Regular
Regular

Joined: Thu Feb 24, 2005 2:34 pm
Posts: 80
sorry don't kill me but always insert i have no idea...
Thanks devsi

public static void main(String[] args) {
/*Oggetto che arriva da flex*/
//Long pk = new Long(125);
Tuser vo = new Tuser();
//vo.setPkid(pk);
vo.setCod(new Long(10000));
vo.setDescr("Devis Benin");
Test t = new Test();
t.update(vo.getCod(),vo);
}



public final Tuser save(final Tuser vo) {
try {
TuserDAO dao = new TuserDAO();
final Session session = dao.getSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(vo);

// return the VO pk object
tx.commit();
dao.getSession().close();
return vo;
} catch (final Exception ex) {
System.out.println(ex);
}
return null;
}

public final void update(final Long cod, final Tuser vo) {
TuserDAO dao = new TuserDAO();
Tuser baseVO = (Tuser)dao.findByCod(cod).get(0);
save(vo);
}


}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.