wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes JPA,/mysql/JBoss and a big head ache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "JPA,/mysql/JBoss and a big head ache " Watch "JPA,/mysql/JBoss and a big head ache " New topic
Author

JPA,/mysql/JBoss and a big head ache

Gal Rubinstein
Greenhorn

Joined: Jan 13, 2008
Posts: 13
It's simple: there is an Entity
{
@Id private Integer
private String name
}
..getters and setters.

table, persistence.xml and mysql-ds.xml file all working well:

entityManager.persist(test); adds a line to DB,
getById also works (using EJB-QL...).

the problem:
after I do persist for that bean I get null from the getID() method of the
entity.

further test:
I add a @PostPersist method that prints the id value, and guess what? it printed the Id that was given (auto increment) in the table.

so:
the insertion went fine. but why is there null when invoking getId()
after persist?

thank all in advance.
Gal

p.s.: here is the table and the bean:

[ January 13, 2008: Message edited by: Gal Rubinstein ]
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Is the "id" vs. "ID" causing your problem?

Try also @GeneratedValue w/o any parameters.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Gal Rubinstein
Greenhorn

Joined: Jan 13, 2008
Posts: 13
Tried that, doesn't work, don't really understand why the Entity doesn't have the id, it seems like ether the id gets nulled or the @PostPersist shows the entity state after the toString() is activated.. also tried with or without manager.flush() without any results.. so weird, don't have that problem with neither with Oracle or MSSql..

Hope you have any clue...

Gal
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Do you have a server and a client?

If you are in the client with a testEntity instance, then send it "across the wire" to the server, it is being copied.

The server will take the copy, create an ID for it, and begin managing the instance. The copy on the client is unchanged. getId() on it should return 0.
Gal Rubinstein
Greenhorn

Joined: Jan 13, 2008
Posts: 13
sry... that doesn't help also, the more I search around the more I see other people with that problem.


at the workplace we moved to work with MS-SQL because of this.

tried fire-bird, also problematic with JPA, specially with jboss.

if there is no solution, are there other open source RDBMS-es that I can use?

(PS, maybe it lingual problems, but I could understand if oracleXE is free or not)

thank you for the help
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
JPA can sit on top of many databases,
i'm using mysql,

but you haven't provided enough code example to confirm if the problem exists there,
Gal Rubinstein
Greenhorn

Joined: Jan 13, 2008
Posts: 13
Sorry about that, here is the rest of the code.
first the mysql-ds.xml


and here is the persistence.xml


the entity and table I have already added, here is the manager bean:


bare in mind that in the entity the toString() has been over written and there is a @PostPersist the prints out the id.

the executable is quit standard: creates a new Entity, sets the 'name' property, looks up the manager and use its create(test) method. after that prints out the entity. this is how you can see the null in the toString() in comparison to the @PostPersist.

thank you for the effort. hope this helps,

gal
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Gal,

You need to change the contract of the manager.
Even when you access it in local JVM, you are not guaranteed how the EJB will treat it. It goes through a local proxy, which treats it like a remote object. The object that gets persisted is copied.

Basically, the manager needs to return the newly persisted object. Optionally the ID can be returned - this can lead to sticky problems with embedded owned objects that get changed.

This is where I hooked into my code, and called your code:


Here's how I modified your code:






output I obtained:
Gal Rubinstein
Greenhorn

Joined: Jan 13, 2008
Posts: 13
thank you very much... it's working perfectly..

by the way, in EJB3 you don't need to do narrow() anymore, specially on jboss ejb3 container...

again, thank you, you where a good aspirin for this head ache :-)

Gal.

ps: is there anyway to announce this post as resolved, other might be running into this problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA,/mysql/JBoss and a big head ache
 
Similar Threads
"hibernate_sequence" does not exist
Persisting pojo with collection attribute using Hibernate
Cannot update object - Hibernate
Cannot add or update a child row: a foreign key constraint fails JPA/Hibernate
How to join two tables having each two fileds in common? (seems like @OneToMany with 2 joinColumns)