FrontPage »
Forums »
DaoGen Forums » Examples of DaoGen customizations
Examples of DaoGen customizations
Posted by aradius 11:43:45 AM
Hello,
I used DaoGen to generate some source code, and I customized generated sources in order to make it works with a little project.
I don't have bugs to report, but I just wanted to send you my comments about my customization, if it can be useful to improve the DaoGen or add some stuff to generated code.
Sources I include here are generated from a previous version of DaoGen (before 2.0.0).
Thanks for this generator.
######################################
Here is example I used :
table : article
columns : id int, label varchar, ....
######################################
I modified source code like this :
#####################
# in Article.java : #
#####################
- implementing java.io.Serializable for bean object :
public class Article implements Serializable
- add equals(Object valueObject) method :
public boolean equals(Object valueObject) {
if ((valueObject == null) || (!( valueObject instanceof Article))){
return false;
}
else if (this == valueObject) {
return true;
}
else {
return equals((Article)valueObject);
}
}
Without equals(Object valueObject) method, I have errors when I try to compare objects in a Collection ; Collection.contains(Object) always return false.
This was before renaming of method equals() to hasEqualMapping().
- do :
private Integer id;
instead of
private int id;
because in ArticleDAO.searchMatching method, there is the test :
if (valueObject.getIdentifiant() != 0) {
if (first) {
first = false;
}
sql.append("AND identifiant = ").append(
valueObject.getIdentifiant()).append(
" ");
}
ie. if field equals 0 then its value is not added to SQL request.
But sometimes, it could be useful to test if an int column has 0 value in SQL request.
with my Article bean having only object properties, this is :
if (valueObject.getIdentifiant() != null) {
//...
}
######################
# in ArticleDao.java #
######################
- in methods load, loadAll, create, save, delete, deleteAll, countAll, searchMatching, I remove Connection parameter, and make them use a getConnection method, because in my project, these methods are called from an business object, and this business object of my project don't import java.sql packages and can't create a new Connection:
public List loadAll() throws SQLException {
//...
Connection conn = getConnection();
searchResults = listQuery(conn, conn.prepareStatement(sql));
return searchResults;
}
- Add a loadAll(int offset, int limit) method to get a sublist for pagination. (Uses same code as loadAll but concatenate request String with instruction "LIMIT "+offset+" "+limit+" ")
- Add a countMatching(Article valueObject) method to return the number of all rows from table that matches valueObject
- Convert :
public void load(...)
to :
public Article load(...)
like loadAll method, this method return an object.
- Same thing for singleQuery method.
############
# Others : #
############
- Add a utility class to get SQL connection
- Create a DAOException thrown by all DAO method instead of SQLException : because business objects of my project don't import any java.sql.* class.
Re: Examples of DaoGen customizations
Posted by anonymous 20.05.2004 19:45
I'd like to see:
A class that is used to create connection and DAO objects assume a connection can be obtained from this class. Then we can customize this new class like implementing a pool, loading initial db configuration and etc.
Re: Re: Examples of DaoGen customizations
Posted by anonymous 20.05.2004 19:48
It's a way to isolate business logics and physical DB operations.
Re: Re: Re: Examples of DaoGen customizations
Posted by Luigi 22.05.2004 10:15
> anonymous 20.05.2004 19:48
> It's a way to isolate business logics and physical DB operations.
Thanks for your feedback!
I understand the need for this type of class. I will seriously consider it. The biggest problem with that is, that it would break the current API, which assumes the Connection parameter for each method. (There exists quite a lot code that depends on the DaoGen API.)
// Luigi.