• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Private Constructor, no error?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's mentioned in the JPA spec 2_0 that an entity must have a no-arg constructor and it should be public or protected. I changed the visibility to private and everything(persistence logic) works fine. I am able to save and retrieve object without any warning/error. Why so? I am using Mysql and hibernate 4.3-6.Final as JPA provider. The same goes for final class , i made my Entity final and it still works.


But if i call clear() method on em , and perform some action on entity then i get Runtime exception related with private constructor
e.g.


Can someone please explain why the code above behaves like this?
Thanks
 
Creator of Enthuware JWS+ V6
Posts: 3290
296
Android Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I changed the visibility to private and everything(persistence logic) works fine. I am able to save and retrieve object without any warning/error. Why so?


That means that your JPA implementation is not obeing the JPA specifications (by the way: hibernate 4.3-x is JPA 2.1)

I created a small example and tested it with the openjpa implementation and I got the expected error:

Exception in thread "main" <openjpa-2.3.0-r422266:1540826 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: [Error while processing persistent type nl.notes.jpa.Person, declared in \nl\notes\jpa\Person.java. Error details: Unenhanced classes must have a public or protected no-args constructor. Type nl.notes.jpa.Person's no-args constructor is private or package-visible.

 
hamada yam
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, for exam purpose should i use openJPA 2.0 as an implementation in order to get the same behavior as mentioned in JPA specification?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3290
296
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I haven't found any problem (yet) with the OpenJPA implementation.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!