1

I have visited too many site and also question on stackoverflow for reference but i can't found exactly answer about below.

I want to design an architecture for Generic DAO Pattern without using of hibernate.

Currently I having trouble in developing GenericSpringDAOImpl<DomainObject> java file. How to make generic getList() method(Which return DomainObject objects) without hibernate or I say using jdbctemplate of spring ?

My Code are below:

GenericDAO.java

public interface GenericDAO<DomainObject extends Serializable, KeyType extends Serializable> extends Serializable{

/**
 * Give list of all entities of underlying Domain Object. 
 * @return List of all entities.
 */
public List<DomainObject> getList();
/**
 * Returns the total number of Entities that is affected by sql query,.
 * 
 * @param query
 *            the query
 * @return Number of Entities affected By SQL Query.
 */
public int getRecordBySQLQuery(String query);

/**
 * Returns Domain object whose Key-value pair matched.
 * @param keyName Column name 
 * @param keyValue any value that is being matched under Column name.
 * @return DomainObject
 */
public DomainObject getRecordByKeyandValue(String keyName,Object keyValue);

public DomainObject getRecordByKeysAndValues(String[] keyName,Object[] keyValue);

/**
 * Returns list of Domainobjects whose Key-value pair matched.
 * @param colname Column name
 * @param keyValue List of values.
 * @return List<DomainObject> according to the Condition satisfied.
 */
public List<DomainObject> getListByKeyandValue(String colname,List<? extends Object> keyValue);

/**
 * Returns the list of Entities according to the condition applied.
 * @param condition Condition
 * @return List<DomainObject> according to condition satisfied.
 */
public List<DomainObject> getListByCondition(String condition);

/**
 * Find Object based on primary key.
 * @param id Identifier value is passed.
 * @return DomainObject i.e POJO which has the passed id value.
 */
public DomainObject getRecordByPrimaryKey(KeyType id);

/**
 * get the list of primary keys based on the condition.
 * @param condition Condition 
 * @return List<KeyType> of Primary Keys according to the Condition.
 */
public List<KeyType> getPrimaryKeyCollection(String condition);

/**
 * get the list of primary keys based on the condition.
 * @param condition Condition 
 * @return List<KeyType> of Primary Keys according to the Condition.
 */
public List<KeyType> getPrimaryKeyCollection(String condition,final Object... values);

/**
 * Insert the Domain Object that is going to persist into the Database. 
 * @param object Domain Object which is going to persist into the Database
 * @return KeyType Serializable value generated By Hibernate due to session.save().
 */
public KeyType insert(DomainObject object);

/**
 * Update the Domain Object.
 * @param object Domain Object that is being changed/updated.
 * @return 1 if Domain Object is being successfully updated
 *         0 if Exception Generation while updating the Object.
 */
public int update(DomainObject object);


/**
 * Deleting Domain Object.
 * 
 * @param object
 *            Domain Object that is going to be delete.
 * @return the int
 */
public int delete(DomainObject object);

/**
 * Delete Object whose oid(Object Identifier) matched with given Primary Key.
 * @param id Identifier value.
 */
public void deleteById(KeyType id); 


}

GenericSpringDAOImpl.java

public abstract class GenericDAOSpringImpl<DomainObject extends Serializable, KeyType extends Serializable> implements Serializable, GenericDAO<DomainObject, KeyType>{

@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;

RowMapper<DomainObject> rowMapper;

public NamedParameterJdbcTemplate getJdbcTemplate() {
    return jdbcTemplate;
}

public void setJdbcTemplate(NamedParameterJdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

public List<DomainObject> getList(){    
    /*Develop Code which return DomainObjects 
     * One way is pass query to getList() but not good approch 
     */
    return new ArrayList<DomainObject>();
 }

/*
   Other methods also required to implements.
*/
}

Here Domain Object is any object like Student,School,Teacher etc.

Community
  • 1
  • 1
Kamlesh Kanazariya
  • 1,079
  • 2
  • 11
  • 29
  • What is getList() supposed to return? You'll have to give it some kind of parameters to fetch by, such as creating methods like `getDomainObjectBySomeValue(int someValue)` etc. – Kayaman Jan 07 '15 at 09:28
  • @Kayaman `getList()` method return all the objects of `DomainObject` Suppose i want to fetch all students then `DomainObject` as Student and `getList()` method returns all students. – Kamlesh Kanazariya Jan 07 '15 at 09:35
  • If it returns all the objects, then you execute the SQL query and return the objects. Why would you think you need to pass the query as a parameter? – Kayaman Jan 07 '15 at 09:37
  • 1
    @Kayaman `List getList(String query){ List domainObjects = jdbcTemplate.query(query,rowMapper); return domainObjects; }` this is one way which i can implement getList() method but i want other way – Kamlesh Kanazariya Jan 07 '15 at 09:42
  • For returning ALL the objects, you'll make a method like `List getList() { return jdbcTemplate.query("SELECT * FROM DOMAIN_OBJ", rowMapper); }`. But like I said, you'll probably want to limit what you're fetching, so you'll need to make methods like `getDomainsBySize(int size)` or `getDomainsByLength(int length)`. – Kayaman Jan 07 '15 at 09:46
  • if any one has a reason for doing a -1 than please let me know don't just down vote this question before understanding it properly – Kamlesh Kanazariya Jan 07 '15 at 09:47
  • @Kayaman Here Domain Object is generic type it can be any Student or School and i don't want to pass query inside getList() method . In Spring+Hibernate example we have not pass query and automatically get list from table. – Kamlesh Kanazariya Jan 07 '15 at 09:56
  • Forget my earlier comments, I wasn't following. The main advantage with Hibernate is, that it knows the name of the table where the entity is. If your `DomainObject` classes are always in a table named `domainobject`, you could use that for the query (through `getClass().getName()`). You could use reflection to work with the rowmapper. However, since all this is already done by Hibernate, why wouldn't you just use it? – Kayaman Jan 07 '15 at 09:59
  • I don't want to use reflection or Hibernate due to performance issue – Kamlesh Kanazariya Jan 07 '15 at 10:05
  • Then make a superclass (or rather an interface) for all domain objects that tells the name of the table and an abstract method that will return a row mapper, so all domain objects need to extend it. – Kayaman Jan 07 '15 at 10:06

1 Answers1

1

Are you looking for implementations provided by Spring Data JPA ? http://projects.spring.io/spring-data-jpa/

Nishant Prabhu
  • 85
  • 1
  • 2
  • 11