1

I am using these technologies to solve this task : Java, Spring, Hibernate For instance if I have 3 Entities: User, Company, Address

as usually I should create a DaoImpl class and Dao interface:

interface Dao{
void saveUser(User user);
void saveCompany(Company company);
void saveAddress(Address address);
}

@Repository
public class DaoImpl extends HibernateDaoSupport implements Dao{
void SaveUser(User user){
getHibernateTemplate().save(user);
}
void SaveCompany(Company company){
getHibernateTemplate().save(company);
}
.
.
.

}

and here we go for every save() it should have its own method SaveCompany(), SaveUser(), SaveAddress()....

but what if we have hundreds of them is it convenient to be writing a method for everyone of them ?

can we just write such method instead ?

void save(Object obj){
getHibernateTemplate().save(obj);
}
2
  • I would create a generic save method for your entities that behind the scenes calls getHibernateTemplate().save(obj) and custom save methods in service layer only if there should be a business rule to accomplish before or after saving the entity. Commented Aug 6, 2014 at 20:42
  • Have you solved this issue? Commented Aug 14, 2014 at 11:06

1 Answer 1

1

You could use the pattern described here Generic Dao Pattern.

The foundation of using a Generic DAO is the CRUD operations that you can perform on each entity.

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

2 Comments

Yes Thanks, and sorry for taking time before confirming your answer
@user3135757 Don't worry it was a pleasure to help you :)

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.