This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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
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 ]
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.
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