How to create relationships between objects in Spring JDBC?

I want to implement a relationship from JPA to Spring JDBC. For example, suppose I have Account and Ads objects. The relationship between account and advertising is @OneToMany according to JPA.

Account Class:

public class Account {

private Long id;
private String username;
private Set<Advert> adverts = new HashSet<Advert>();

// getters + setters
}

Ad class

public class Advert {

private Long id;
private String text;
private Account account;

// getters + setters
}

AccountMapper:

public class AccountMapper implements RowMapper<Account> {

public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
    Account account = new Account();
    account.setId(rs.getLong("id"));
    account.setUsername(rs.getString("username"));
    return account;
}
}

Now I am trying to create a Mapper class for a class. How can I map an account variable from an advertising class to a string? Many thanks

+4
source share
3 answers

Well, if you do not use ORM ... you do not have an object relationship mapping! After that, ORMs were created for this reason :-)

, ORM . JDBC JPA - . , , . :

  • , ORM (: , ACL spring).

, JPA , DAO. JDBC.

, DAO JDBC, .

: ORM , JDBC SQL-.

+4

Hibernate, , Hibernate tutorial , .

JDBC, :

  • , .

  • AccountMapper:

    public class AdvertMapper implements RowMapper<Advert> {
    
        public Advert mapRow(ResultSet rs, int rowNum) throws SQLException {
            Advert advert = new Advert();
            advert.setId(rs.getLong("advert_id"));
            advert.setText(rs.getString("advert_text"));
            return advert;      
        }
    }
    
    public class AccountMapper implements RowMapper<Account> {
    
        private final AdvertMapper advertMapper;
    
        public AccountMapper(AdvertMapper advertMapper) {
            this.advertMapper = advertMapper;
        }
    
        public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
            Account account = new Account();
            account.setId(rs.getLong("account_id"));
            account.setUsername(rs.getString("account_username"));
    
            Advert advert = this.advertMapper.mapRow(rs, rowNum);
            advert.setAccount(account);
            account.getAdverts().add(advert);
    
            return account;
        }
    }
    

AccountMapper AdvertMapper .

Hibernate, .

+5

- , : " , - , RowMapper, ResultSet , .

http://simpleflatmapper.org, ResultSet POJO . , , , , .

Checkout http://simpleflatmapper.org/0104-getting-started-springjdbc.html as well as https://arnaudroger.imtqy.com/blog/2017/02/27/jooq-one-to-many.html

you will need to get a ResutlSetExtractor - it is thread safe, so only one instance is needed -,

    private final ResultSetExtractor<List<Account>> mapper = 
        JdbcTemplateMapperFactory
            .newInstance()
            .addKeys("id") // assuming the account id will be on that column 
            .newResultSetExtractor(Account.class);

    // in the method
    String query = 
        "SELECT ac.id as id, ac.username, ad.id as adverts_id, ad.text as adverts_text"
        + "FROM account ac LEFT OUTER JOIN advert ad ON ad.account_id = ac.id order by id " 
        // the order by id is important here as it uses the break on id on the root object 
        // to detect new root object creation

    List<Account> results = template.query(query, mapper);

so that you should get a list of accounts in the list of filled ads. but the ad does not have an account.

0
source

All Articles