Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

java - How should I work with multiply(large numbers) entities storred in multiply db tables using jdbc? Mapping entities

My 1st entity from db user_db and table user_table

public class User {
    
    private int userId;
    private String surname;
    private String name;
    
    //getters setters

    public User(ResultSet resultSet){}

    public User(ResultSet resultSet) throws SQLException {
setUserId(resultSet.getInt("userId"));
        setSurname(resultSet.getString("surname"));
        setName(resultSet.getString("name"));
    }
}

Its Repository

public class UserRepository {
    
    @Autowired
    private DataBaseUtil dataBaseUtil;

    public User findUser (int userId) throws SQLException {
        String query = "SELECT "+
                                "surname, name, userId" +
        
                        "FROM "+
                                "user_db:user_table, "+
                        "WHERE "+
                                "userId = '" + userId + "'";

            User currentUser = null;
            try {
                ResultSet resultSet = dataBaseUtil.getResultSet(query);
                while (resultSet.next()) {
                    currentUser = new User(resultSet);
                }
            }
            finally {
                dataBaseUtil.closeConnection();
            }
        
        return currentUser;
    }
    
}

My database util

@Service
public class DataBaseUtil {
    
    @Autowired
    private DataSource dataSource;
    
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
        
    public DataBaseUtil() {}
    
    public DataBaseUtil getResultSet (String query) throws SQLException {
        this.connection = dataSource.getConnection();
        this.statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        this.resultSet = statement.executeQuery(query); 
        return this;
    }
    
    public void closeConnection() throws SQLException {     
        try (Connection connection = this.getConnection();
             Statement statement = this.getStatement();
             ResultSet resultSet = this.getResultSet()) {                       
        }
    }
    
    public void update(String updateQuery) throws SQLException {
        try (Connection connection = dataSource.getConnection();
             Statement statement = connection.createStatement() ){
             statement.executeUpdate(updateQuery);
        }
    }
    }

Ok. Passed some time. We’ve created new table - user_detail_table

I’m creating entity

//My 2nd entity from db user_db and user_detail_table

    public class UserDetail {
    
        private int userId;
        private String address;
        private String hobby;
        
    //setters getters
        
        public UserDetail(ResultSet resultSet) throws SQLException {
            setAddress(resultSet.getString("address"));
            setHobby(resultSet.getString("hobby"));
            setUserId(resultSet.getInt("userId"));
        }
    }

what should I do if I want get data from User and UserDetail? (like it does hibernate)? What I think:

I add UserDetail field in User entity. It’s OK, I think... And here I have questions: If I change String query in UserRepository method public User findUser (int userId) to

String query = "SELECT surname, name, address, hobby    
"FROM user_db:user_table AS a,  user_db:user_detail_table AS b"+
    "WHERE a.userId = b.userId AND a.userId = '" + userId + "'";

looks good, but what if I need get just UserDetail data? I should create UserDetailRepository, right? If I need only User data… I create new method? Then new table will be created again and we’ll need “to connect” it again to User… How should we make entity mapping. How will llooks like repositories (if I need only User data? If I need User and UserDetails data)?

Sorry for my not clear description promblem… I hope you’ll understand what I want: to design correctly... best practices. If you have a comment for my code, I'll be glad to hear it. Thank you a lot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...