Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes Unable to persist Entity having @OneToMany mapping on a reference in JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Unable to persist Entity having @OneToMany mapping on a reference in JPA" Watch "Unable to persist Entity having @OneToMany mapping on a reference in JPA" New topic
Author

Unable to persist Entity having @OneToMany mapping on a reference in JPA

R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 372
    
    1

Hello,

I have two entities, ComponentMetaData and ComponentType.

ComponentMetaData.class


ComponentType.class


Now when I am trying to persist the ComponentMetaData Object. It is throwing Transaction RollBack Exception.
Because it also tries to run an insert query in ComponentType table (Which of course it will not be able to do) .. As it will generate duplicate primary key entry...

This is how I am persisting...


I have attached my table structure...

Can anyone see, where I am doing anything wrong...
I doubt if I have done the Foreign Key Mapping in the component_meta_data class correct or not..



[Thumbnail for component_meta_data.png]


[user and component_type table.png]

James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
What is "transaction"? Are you using JTA or JPA transactions?

My guess is you did not find the ComponentType through the same persistence context (EntityManager or transaction).

Try moving the transaction.begin() to the start of the method, and ensure you are using the same EntityManager to find the ComponentType .


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 372
    
    1

James Sutherland wrote:What is "transaction"? Are you using JTA or JPA transactions?

My guess is you did not find the ComponentType through the same persistence context (EntityManager or transaction).

Try moving the transaction.begin() to the start of the method, and ensure you are using the same EntityManager to find the ComponentType .


Sorry, I forgot to post init() method.. This method inititializes EntityManager, EntityManagerFactory and EntityTransaction (JPA Transaction).

And anyways, the problem is now resolved.. Thanks for your reply..

Actually the problem was of MySQL Engine. (It was MyISAM and it does not support Foreign Key.. Need to enable InnoDB).
Due to this I was able to insert a value for foreign key which was not in primary key column of ComponentType..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to persist Entity having @OneToMany mapping on a reference in JPA