1

I'm using JPA/Hibernate for a few years and they are really useful. but i have some philosophical problems with the design.

the main problem is why should I make all my class properties public(create public setter/getter) just because Hibernate wants to access them even though it can access them without this permission ( through reflection) and in this design getters and setters should be so simple to reduce overhead and then practically they loose their philosophy of being which is controlling access to fields.i.e. in this design getters and setters become shared between Database Access(JPA) and System Domain which need different design criteria. (you probably don'nt need to check a value before setting it to a field when you load it from database because you've done this check before!)

can any body explain any reason for this design? I know that it's helpful in some cases but in most of them it's not.!

just one more problem that it has is that setters are almost useless when loading an object from database as object is partially loaded and there is no order for it( so it almost force us to implement setters so simple and just a copy from input to filed) !

5
  • 1
    as far as I know getters/setters/constructors used by Hibernate only can be private! Commented Nov 30, 2014 at 20:41
  • What would be the purpose of a database column that you can't read from/write to? Commented Nov 30, 2014 at 22:25
  • @JohnAment in Object Oriented Programming an object can have some internal data for itself which should managed by that object only and others should not have access to those objects! Commented Dec 1, 2014 at 1:07
  • @geert3 thank you. but you'r are wrong! Commented Dec 1, 2014 at 1:08
  • 1
    @hasan I don't think so. From the Hibernate docs: Hibernate can access public, private, and protected accessor methods, as well as public, private and protected fields directly. The choice is up to you and you can match it to fit your application design. See docs.jboss.org/hibernate/orm/3.3/reference/en/html/… Commented Dec 1, 2014 at 6:57

1 Answer 1

5

You can leave your setters private if they are only used by Hibernate. From the hibernate documentation:

Hibernate can access public, private, and protected accessor methods, as well as public, private and protected fields directly. The choice is up to you and you can match it to fit your application design.

The no-argument constructor is a requirement for all persistent classes; Hibernate has to create objects for you, using Java Reflection. The constructor can be private, however package or public visibility is required for runtime proxy generation and efficient data retrieval without bytecode instrumentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.