Reputation: 7651
I am developing a standalone J2SE application (Can't add J2EE, Hibernate, Spring, etc...). I need idea on designing the code architecture.
This is my code:
class PersonBean {
String name;
int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class PersonMapper{
PersonMapper(Connection conn){
}
PersonBean load(ResultSet rs){
PersonBean entity = new PersonBean();
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
return entity;
}
PersonBean findById(int id){
String query = "SELECT * FROM Person where id = ?";
PreparedStatment stmt = conn.getPreparedStatement(query);
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next())
return load(rs);
else
return null;
}
List<PersonBean> findByName(String name) {}
}
Is there anything do I need to do to improve this design?
In object world Student object extends Person object. But in database Student and Person are two different tables, which leads to create two different classes for StudentBean and PersonBean where StudentBean will not extend PersonBean. Do I need to create business layer on top of entity Bean object layer? if so how to design?
I don't know how to start browse about this, any links also would be fine.
Upvotes: 0
Views: 2043
Reputation: 73
Either for learning or for real usage,
It is best to review the Source Generated by DAO Generators. You can learn many good things and additionally stay away from Design Failures.
Review the one here: FireStorm
Upvotes: 0
Reputation: 4054
I go with the logic said by Albert
I would recommend the same, you can program using ResultSetMetaData to get all the column/pseudo column names which will map like
EMPLOYEE_NAME to employeeName setter method setEmployeeName on the fly
you can use Java Reflection to call a setter method or you can use apache BeanUtils
Upvotes: 0
Reputation: 106
If you want to be able to have a hierarchy, you should implement a better mapper, that can do the difference between loading a Person vs a Student bean. A better way would be to implement on your own, something similar to Hibernate or Spring's JdbcTemplate ... which will take you a while.
But before that, I would suggest to try out sql2java, that will generate a similar structure as yours, for all the tables that you have, and also lets you to customize it, and if the data structure changes, it can be re-generated.
Upvotes: 1