• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Standalone Persistence example with bug?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

I am going through the examples of O'Reillys EJB 3.0 Book. There is an example (ex05_2) that has a J2SE-Client which make the persistence directly/self over JDBC in the hypersonic database. The client instantiates a cabin-class entity and persists it with an EntityManager like following:



When I test the code I get an error: "detached entity passed to persist". When I change the line "manager.persist(cabin_1)" to "manager.merge(cabin_1)" then the example works. The cabin_1 entity has a field marked as ID and the actual ID is definitely not yet used in the database.

Why comes this error message when I use persist?

Thank you in advance,
Mike
 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the strategy you chose to generate the primary key ?

Show us the code of your classes.
 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike Rocks,

There is no problem in the code-
it looks like that cabin_1 entity hold's your primary key.

When you persist it by using the code-
---------------------------
manager.persist(cabin_1);
--------------------------------
first time when you run, it will work. Second time it will give you error.
it is actually giving the unique constraint violation error. Please look at your server logs. But at the console, it will give the error as 'detached entity persist....'

but when you are doing the code

---------------------------
manager.merge(cabin_1);

-----------------------------

it is actually updatinng the same record. that's why it didn't gives error.



To resolve your problem--

1) run your code with different value's for cabin_1 entity- surely it will work.
[ October 15, 2008: Message edited by: Amandeep Singh ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The cabin_1 entity has a field marked as ID and the actual ID is definitely not yet used in the database.


How did you check ? With a SELECT ?
 
Mike Rocks
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

thanks for your replies. I found the error with your hints at the ID. I've made a fault AND Jboss has a curious behavier.

The ID was auto-generated from my database (MS SQL Server)

and therefore I couldn't set the ID myself. I overlooked this because I copied the entity from another example. :roll: Now that I don't set the ID the code works fine with persist().

BUT why does the Jboss server adds a new Cabin with the next new ID with merge(), when I set the ID myself to a different higher number and have auto-generated IDs? I think that there should come an error message like "cannot merge the entity, because no such ID in database"...

Sincerely
Mike
 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic