Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Private Constructor, no error?  RSS feed

 
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: 3273
296
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
Posts: 3273
296
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.
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!