• 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

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

 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
component_meta_data.png
[Thumbnail for component_meta_data.png]
component_meta_data table..
user-and-component_type-table.png
[Thumbnail for user-and-component_type-table.png]
User & ComponentType table
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 think I'll just lie down here for a second. And ponder 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