This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Stephen Gaukrodger
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please show DBHelper class code
 
Stephen Gaukrodger
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephen Gaukrodger
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic