Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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
  • 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

JPA FIeld access query

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,

I have started preparing jpa cert and going through pro jpa 2 book.
While going through field access, it says about entity class "all filed must be declared as either protected, package or private.Public fields are disallowed"

But then I ran a sample application making the fields as public and I am able to persist the record. so confused, please can someone clarify.
 
author & internet detective
Posts: 39392
761
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjay,
Welcome to CodeRanch!

I suspect this is a case of the JPA provider not implementing that part of the specification. It's not good practice to make the fields public though, so I recommend you don't (even though your provider isn't forbidding it.) If you make it public, other code could make changes without the entity being aware.
 
Sanjay Jha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Thanks a lot for your reply, I am using hibernate, but is it possible hibernate is not implementing that portion of spec, I am not sure. please could you clarify..
and that's correct that I should not use public, I was just playing around because it was written that public is not allowed but it is still working....
 
Jeanne Boyarsky
author & internet detective
Posts: 39392
761
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sanjay Jha wrote: Thanks a lot for your reply, I am using hibernate, but is it possible hibernate is not implementing that portion of spec, I am not sure. please could you clarify..


Yes. I verified the behavior in Hibernate 3. Changing the instance variables to public still worked.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!