This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi guys! I'm really doubtful about this code here but it's the only thing that works for me as of now. I wanted this to be in a single transaction but I'm not sure how to do it.
If you look at the code, you'll notice that I made an early commit, which I didn't want to do but I had no choice because I wanted to get the generated ID to be able to produce a controlId String.
You'll also notice that I made many commit statements which makes the rollback in the try-catch block useless because changes are already reflected to the database when an exception is thrown. I really want to avoid this so I want to put them all in a single transaction and rollback everything if an exception occurs. Many thanks!
The problem of obtaining a generated id before commit can be solved by issuing a flush call, so I would normally recommend that you do that, however, looking at your code I have to say (and no offence intended) that you might really want to so some more reading on JPA before going any further. I think you would find that a small investment of a day or two would really pay dividends. Pick up any book on JPA (I can recommend one ;-) that takes your fancy and learn when and how to use which EntityManager operations, etc.