• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Constructor, no error?

 
hamada yam
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
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2531
112
Android Chrome Eclipse IDE
  • 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
Saloon Keeper
Pie
Posts: 2531
112
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I haven't found any problem (yet) with the OpenJPA implementation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic