Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping ResultSet to Pojo Objects

Tags:

java

oop

sql

pojo

Well that's really embarrassing I have made a standard pojo class and its dao class for data retrieval purpose. I am having a difficulty to understand a basic procedure to how to handle a customized query data to Pojo class.

let's say my User class is

public class User{

private int userId;
private String username;
private int addressId;

}

public class Address{
private int addressId;
private String zip;
}
public class UserDAO{

public void getUserDetails(){

String getSql = select u.userId, u.username, a.zipcode from user u, address a where u.addressId =     a.addressId;

 //no pojo class is now specific to the resultset returned. so we can't map result to pojo object
}

}

now how I should model this with my pojo class as if using String to manage this then concept of object oriented vanishes, also complexity would increase in the future as well. kindly guide!

Update for Further Explanation

We know that we can map same table objects with same pojo class, but when the query is customized and there is a data returned which doesn't map to any specific class then what would be the procedure? i.e. should we make another class? or should we throw that data in a String variable? kindly give some example as well.

like image 713
iCurious Avatar asked Nov 10 '22 13:11

iCurious


1 Answers

For this purpose you can use one of implementation of JPA. But as you want to do it manually I will give you small example.

UPD:

public class User {
   private int userId;
   private String username;
   private Address address; // USE POJO not ID
}

public class Address{
   private int addressId;
   private String zip;
   List<User> users;
}
    public User getUserById(Connection con, long userId) {
        PreparedStatement stmt;
        String query = "select u.user_id, u.user_name, a.id, a.zip from user u, address a where a.address_id = u.id and u.id = ?";
        User user = new User();
        Address address = new Address;
        try {
            stmt = con.prepareStatement(query);
            stmt.setLong(1, userId);
            ResultSet rs = stmt.executeQuery();
            address.setId(rs.getInt("id"));
            address.setZip(rs.getString("zip");
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("user_name"));
            user.setAddressId(rs.getInt("address_id"));
            user.setAddress(address); // look here
        } catch (SQLException e) {
            if (con != null) {
                try {
                    System.err.print("Transaction is being rolled back");
                    con.rollback();
                } catch (SQLException excep) {
                }
            }
        } finally {
            if (stmt != null) {
                stmt.close();
            }
        }
        return user;
    }

You shouldn't do new POJO for that query, you should write normal query. And remember - your object model is main, tables in DB is just a way to save data of your application.

like image 71
Divers Avatar answered Nov 15 '22 05:11

Divers