Doubt about object passed to a constructor in GWT

23 views
Skip to first unread message

Shi

unread,
Dec 8, 2011, 3:03:39 PM12/8/11
to Google Web Toolkit
Hello everyone!
In the database I have a table "persons" with fields: id, first_name,
last_name, cod_fs with id PK
and a table "users" (id, username, password, person_id) with id PK and
person_id FK REFERENCES persons (id) .

I translated this report in my GWT project in models "PersonTO" and
"UserTO".

The code UserTO is as follows:

package com.google.gwt.client.to;

import java.io.Serializable;

public class UserTO implements Serializable {

private int id;
private String username;
private String password;
private PersonTO personTO;

public UserTO() {
}

public UserTO(int id, String username, String password, PersonTO
person ) {
this.id = id;
this.username = username;
this.password = password;
this.personTO = person;
}

public PersonTO getPersonTO() {
return personTO;
}
public void setPerson(PersonTO person) {
this.personTO = person;
}
....
and other getter/setter methods
...


The database queries are made ​​within the class UserDAO, but I do not
know how to invoke the constructor UserTO properly:

package com.google.gwt.server.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.google.gwt.client.exceptions.DBException;
import com.google.gwt.client.to.PersonTO;
import com.google.gwt.client.to.UserTO;

public class UserDAO extends BaseDAO {

private static String findUserFromCfSQL = "SELECT
id,username,password,person_id FROM users WHERE person_id = (SELECT id
FROM persons WHERE cod_fiscale = ?)";

public UserTO getUserFromCF(String cod_fiscale) throws DBException {
Connection conn = null;
PreparedStatement prepStat = null;
ResultSet rs = null;
UserTO user = null;

try {
conn = this.getConnection();
prepStat = conn.prepareStatement(findUserFromCfSQL);
prepStat.setString(1, cod_fiscale);
rs = prepStat.executeQuery();

************** user = new UserTO(rs.getInt(1), rs.getString(2),
rs.getString(3), (PersonTO)rs.getObject(4)); ***********

return user;
} catch (SQLException ex) {
ex.printStackTrace();
throw new DBException();
} finally {
try {
prepStat.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
throw new DBException();
}
}
}
}


(PersonTO)rs.getObject(4)) Of course it is wrong, because in
ResultSet at index 4 there is an object of type Integer, but I do not
know whether it is right to call for a service that returns the object
PersonDAO to pass it through to the constructor new UserTO(...)

Someone would know this dilemma?

Jens

unread,
Dec 8, 2011, 3:43:48 PM12/8/11
to google-we...@googlegroups.com
I think in this specific case I would just join the users and persons table so that the result set contains all information to construct a user with its person. 

If you want to use a framework take a look at http://www.mybatis.org/ . You can use native sql with MyBatis and it maps resultsets automatically to objects. They also have a generator (eclipse plugin) that generates DAOs for every table.

-- J.

Shi

unread,
Dec 8, 2011, 3:50:07 PM12/8/11
to Google Web Toolkit
Maybe I thought about a possible solution:
UserPersonDAO create a new class that contains all the queries that
involve both objects and UserTO PersonTO.

What do you think?

Jens

unread,
Dec 8, 2011, 5:54:10 PM12/8/11
to google-we...@googlegroups.com
Do what feels best for you, its just personal preference I think.

I prefer to only have one DAO per domain class so if I need both, an user with person and an user without person, I would go with:

- UserDAO.findByXyz(int xyz)  which delegates to UserDAO.findByXyz(xyz, true) or UserDAO.findByXyz(xyz, false) depending on what you think should be your default case.

- UserDAO.findByXyz(int xyz, boolean withPerson) which allows you to not follow the default case if needed.


Shi

unread,
Dec 9, 2011, 6:13:53 PM12/9/11
to Google Web Toolkit
Thank you for your suggestions!!
Reply all
Reply to author
Forward
0 new messages