Map ResultSet to Pojo Objects

Well, what really confuses me is that I created a standard pojo class and its dao class for data retrieval purposes. I find it difficult to understand the basic procedure for processing custom request data to the Pojo class.

let's say my user class

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 should I model this with my pojo class, as if using String to control this, then the concept of object-oriented calling disappears, and the complexity will increase in the future. kind guide!

Update for further clarification.

, pojo, , - , ? .. ? String? .

+4
4

JPA. , .

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;
    }

POJO , . : , - .

+3

, pojo, , - , ? .. ?

JPA POJO, , .

JPA, .
, , POJO .
, POJO .

:

.

JPA- JPA- .

Manager JPA EntityManagerFactory.

TypedQuery<User> q; 
String sql = "select new com.stuff.User(
int u.userId, String u.username, String a.zipcode) 
from User u, Address a where u.addressId = a.addressId";
List<User> list = entityManager.createQuery(sql).getResultList();

for(User u : list) {
  doStuff(u);
}

, , , BLOB.
, , POJO, , . , , , .

.

+1

ORM, , Hibernate, myBatis, JPA spring -JDBC

spring -jdbc myBatis SQL, JPA Hibernate SQL.

I suggest you read something and find out which one you like before launching your own solution.

0
source

Your question:

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? 

If you have 100 kinds of SQL that return different combinations of columns, can 100 different POJOs be created? The answer is "NO, stop using POJO."

This qood library is designed to solve this problem, you can try it.

-2
source

Source: https://habr.com/ru/post/1524543/


All Articles