Where to put the database access
I hav开发者_JAVA百科e an entity class which is persisted to a database via JPA and I have a Utility class which does the persisting and reading for me.
Now I'm asking myself if that is really the way to go. Wouldn't it be clearer, if the data class would have methods for reading and writing to the database?
Wouldn't it be clearer, if the data class would have methods for reading and writing to the database?
Yes. You need to look at DAO pattern
The standard practice should consist following layers:
Model - Holds your POJO , simply mapped classes
DAO - Interface driven classes which provides CRUD for these POJO's
Service - Use DAO to interact with DB and perform business stuff here
No, it is not good way. If you choose that way, you would couple your domain object logic with data access layer .. They are different things, therefore you should decouple them.
精彩评论