File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes why won't changes that aren't persisted go away? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "why won Watch "why won New topic
Author

why won't changes that aren't persisted go away?

Stephen Gaukrodger
Greenhorn

Joined: Dec 03, 2006
Posts: 6
I'm new to the JPA, and I started out following the netbeans tutorials.

I'm trying to create a stand alone application that does persistence, but I was having trouble making my changes take effect. I wrote a simple app and found that things aren't working like I expect at all. Here's a simple example adapted from the page above:


Now there are two things I could expect to happen here:
1 When I tell t2 to set its name to "other guy" it changes the object, but
doesn't persist anything. When I get a list of Things from the database,
this change should go away.
or
2 When I tell t2 to set its name to "other guy" it immediately persists the
changes, so when I get a list of Things the changes are made.

Thus I expect that either the change will be present in the database and the name "other guy" will be output at or the change won't be present in the database and "other guy" won't be printed at all.

The actual result? The name "other guy" is printed AND the database name remains null.

I don't like this. I'm an unhappy person. Clearly I don't understand what is going on. I'm not averse to reading things - I mean, I'm prepared to read a lot. But I tried the netbeans stuff and don't understand. I either missed something, or I need to try another set of tutorials/book. I'm under some time pressure, so I can't afford to spend a lot of time working through tutorials I'm not confident in. So I guess I have two questions:

What's happening above?
And at the risk of opening a can of worms, where should I look for a comprehensive book or web tutorial that will teach me how to set up stand-alone apps with the JPA?
[ March 14, 2007: Message edited by: Stephen Gaukrodger ]

On a diet of black coffee and prozac buttered toast
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

please show DBHelper class code
Stephen Gaukrodger
Greenhorn

Joined: Dec 03, 2006
Posts: 6
This is pretty much straight out of the netbeans tutorial. I changed it to a singleton class, but I don't think that should have any effect - I'm not using multi-threading in the application I'm writing, so it shouldn't be a problem, right?


[ March 14, 2007: Message edited by: Stephen Gaukrodger ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

You are not calling em.persist() to actually save to the database, you are just using merge which will take a detached object and make it persisted.

Although I would think that being persisted, it would detect that your object is in the transient state, which means new with no id, and know that it would need to do an insert. Anyway, call persist and see what happens.

Mark

p.s. you can also try out the Hibernate tutorials if you want, there is one that is JPA specific if you aren't using Hibernate but some other JPA implementation. www.jboss.org


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Stephen Gaukrodger
Greenhorn

Joined: Dec 03, 2006
Posts: 6
Thanks, that fixed it. It says to use merge in the netbeans tutorial. So I guess I don't trust them anymore.

I'll work through the hibernate stuff.

Thanks once again!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why won't changes that aren't persisted go away?
 
Similar Threads
JPA Query
Why we need to persistent unit in EJB 3.0 ?
Pass JPA entities across tiers?
Issue with JPA + MySQL
JPA pro's and Con's in Java stand alone application?