aspose file tools*
The moose likes Struts and the fly likes [Torque] save() works, but where is the primary key? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "[Torque] save() works, but where is the primary key?" Watch "[Torque] save() works, but where is the primary key?" New topic
Author

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

Shin Hashitani
Ranch Hand

Joined: Dec 04, 2001
Posts: 54
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 ]

/*<br /> * Quants Inc.<br /> * Engineer<br /> *<br /> * Shin Hashitani<br /> * hashitani@quants.co.jp<br /> * <a href="http://www.quants.co.jp" target="_blank" rel="nofollow">www.quants.co.jp</a><br />*/
Tanel Alumae
Greenhorn

Joined: Aug 26, 2002
Posts: 14
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.


RefactorIT - Java Refactoring Tool<br /><a href="http://www.refactorit.com" target="_blank" rel="nofollow">www.refactorit.com</a>
Shin Hashitani
Ranch Hand

Joined: Dec 04, 2001
Posts: 54
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Torque] save() works, but where is the primary key?