• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA FIeld access query  RSS feed

 
Sanjay Jha
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36656
475
Eclipse IDE Java VI Editor
  • 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
Marshal
Posts: 36656
475
Eclipse IDE Java VI Editor
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!