Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

[Torque] save() works, but where is the primary key?

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Environment:
Struts 1.12b
PostgreSQL 7.2.3
Torque 3.0 rc-2
JDK 1.4.0

Situation:
I am trying to use Torque for Struts application. The save() method of each torque class works fine.
In one situation, I have to create an entry first, then create multiple entires in another table with foreign keys to the primary key of the first entry. I can't find a good way to do it.
Structure:
Magazine object (Contains magazine info)
Article object (each article inside of Magazine object, and has a foreign key mapped to the primary key of the magazine. Magazine class corresponding TorqueMagazine class, and Article class has a corresponding TorqueArticle class. The magazine object is contained in ActionForm as a bean.
Hi everyone,
save() method doesn't return the primary key when inserted. I didn't know this, so now I have a problem.
I get "foreign key integraton violation" when I call save method of torqueArticle. It says that magazine table doesn't contain the primary key that is referenced by a foreign key of the torqueArticle object.
MagazineBeanHelper.save(actionForm.getMagazine());
pageKey = actionForm.getMagazine().getMagazineID();
for(int i =0; i < actionForm.getMagazine().getArticle().size(); i++) {
((ArticleBean)actionForm.getMagazine().getArticle().get(i)).setMagazineID(pageKey);
PagePartBeanHelper.save((ArticleBean)actionForm.getMagazine().getArticle().get(i));
}
Something like this.
And above code doesn't work after inserting a magazine entry. MagazineBeanHelper create torqueMagazine object, fill it with magazine object, and calls save() method . save method doesn't return the primary key, but I thought at least set the newly created primary key to the torqueMagazine object.
It didn't.
The only other way I can think of is to do "saving magazine", "getting that magazine right away", get the primary key of the magazine", "use the key to store each article objects". Or I have to use doInsert() of Peer class instead.
Then again,
how do I get the newly stored entry?? I can't get the primary key. The system is accessed concurrently by multiple users, so getting the latest torqueMagazine doesn't always work.
Or before that,
now I think that there must be a smarter way to do this!
Does anybody know??
I appreciate any suggestion or thought.
Thank you in advance.
[ December 05, 2002: Message edited by: Shin Hashitani ]
[ December 05, 2002: Message edited by: Shin Hashitani ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
Check the tutorial.
Rather toward the end of the tutorial there's a code sample that has the following comment:

This might be fixed in a later version of torque.
Check this tutorial anyway, it has a good sample how to use foreign keys.
 
Shin Hashitani
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tanel,
Thanks for the reply.
Yes, I remember this tutorial. It was a long time ago, so I didn't remember much in detail. I sure don't remember the comment you mentioned. You gave me a new starting point.
From the tutorial:
Criteria crit = new Criteria();
crit.add( AuthorPeer.LAST_NAME, "Eddings" );
Vector v = AuthorPeer.doSelect( crit );
if ( v != null && v.size() > 0 )
de = (Author) v.elementAt(0);
Well, he actually uses last name of the author to get the inserted record, so this doesn't work. This is something I tried to avoid because, well, this is weird. And in my case, using one field won't be sufficient. I mean, in order to find a primary key, if I have to use lots of fields as a criteria to get it, what is a point of having the primary key at the first place? This I have to try Peer's doInsert method.
criteria = new Criteria();
criteria.add(MagazinePeer.SITE, magazine.getSite());
...
objectKey = MagazinePeer.doInsert(criteria);
primaryKey = Integer.parseInt(objectKey.getValue().toString());
I ended up doing like this.
This is pretty close to what Peer's doInsert method does, so I feel a little dumb, but this gives me the primary key. I was actually a little nervous if I can get the primary key in int by parseInt() method, but I sure did. So, key property in ObjectKey object contains the primary key in object format.
There might be a better way to do this (I sure hope there are), but this is one way to do it. In case somebody else encounter the same problem, I posted my solution. And I hope this not-setting-primary-key-after-save-method problem will be fixed as Tanel mentioned.
[ December 08, 2002: Message edited by: Shin Hashitani ]
[ December 08, 2002: Message edited by: Shin Hashitani ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic