I;m still having a little trouble though:
My first piece of code is executed when the user logs on to the application.
User user = null;
Database db = jdo.getDatabase();
db.begin();
user = (User)db.load(User.class, new Integer(1));
db.commit();
db.close();
request.getSession().setAttribute("USER", user);
The user is stored in the session for the duration of the use of the web application. Pretty simple.
Next, the user then decides to create a new note for a particular client. Which is then added to the session.
Database db = jdo.getDatabase();
db.begin();
Client client = (Client)db.load(Client.class, new Integer(1));
db.commit();
db.close();
ClientNote note = new ClientNote();
note.setClient(client);
request.getSession().setAttribute("note", note);
The user is sent to a JSP page to enter some text for the note. the note details are read from the session and displayed to the user. I.e the clients name and so on. I've skipped the code for this as its trivial.
When the user submits the page the following code is executed.
String text = request.getParameter("text");
ClientNote note = (ClientNote)request.getSession().getAttribute("note");
User user = (User)request.getSession().getAttribute("user");
note.setText(text);
note.setUser(user);
Now I want to write this out to the database... If I try
db.update(note);
or
db.update(user);
db.update(note);
I get org.exolab.castor.jdo.DuplicateIdentityException: update object which is already in the transaction. Whatever I seem to do I keep getting this.
How do I correctly use the db.update() to store this? what am I doing wrong?
---------------------------------------------------------------
Please note the that 'ClientNote' described above extends the generic 'Note' class which has a reference to a particular User.
Thanks,
qwam
From: Bruce Snyder <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Subject: Re: [castor-dev] Using castor persistence....
Date: Wed, 18 Dec 2002 16:23:03 +0000 (GMT)
This one time, at band camp, qwam swatidi said:
qs>I'm working on a web based application, and I have a question regarding how
qs>I can use the persistence features of castor.
qs>
qs>To explain...
qs>
qs>The user logs on onto the application and their details are loaded from the
qs>database and stored in their session. (NB: The User class implements
qs>Timestampable). See code listing 1.
qs>
qs>At some point the user decides to write a note on a specific client. The
qs>note is attached to the client and also includes details about the current
qs>user that submitted the note. See code listing 2. NB: A client can have a
qs>collection of notes each which refer to the particular user that created
qs>them.
qs>
qs>My problem is that when this code is commited, a new note is created for the
qs>client and it also creates a new copy of the User. This is not the desired
qs>behaviour that I'm after. I want it to use the users information and make an
qs>association between the two. Not create another copy of it.
qs>
qs>-> What should I do to solve this elegantly?
qs>
qs>I've tried initially loading the user using Database.Exclusive. i.e.
qs>
qs>user = db.load(User.class, new Integer(1), Database.Exclusive);
qs>
qs>But that doesn't seem to work.
qs>
qs>If in the second code listing,
qs>
qs>I perform a db.update(user) on the user before retrieving the client's
qs>details it works but it doesn't seem very elegant/correct to do this.
This is how Castor JDO's long transactions
(http://www.castor.org/long-transact.html) are designed to work. The
call to db.update( user ) makes the second transaction context aware of
the User instance. You already found the solution to your problem.
Bruce
--
perl -e 'print unpack("u30","<0G)U8V4\@4VYY9&5R\"F9E<G)E=\$\!F<FEI+F-O;0\`\`");'
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev
_________________________________________________________________
MSN 8 limited-time offer: Join now and get 3 months FREE*. http://join.msn.com/?page=dept/dialup&xAPID=42&PS=47575&PI=7324&DI=7474&SU= http://www.hotmail.msn.com/cgi-bin/getmsg&HL=1216hotmailtaglines_newmsn8ishere_3mf
----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev
