title: Spring Boot with JDBC: Nested Object Modelling
body: <p>i have the following TableStructure in a PostgreSQL DB which is supposed to be the DB Backend for my WebApp:<br /><br />
  <b>init_db.sql</b>
  </p>
  
  <pre><code>CREATE TABLE article (
      id integer NOT NULL,
      name character varying NOT NULL,
      type_id integer NOT NULL
  );
  CREATE TABLE article_type (
      id integer NOT NULL,
      type_desc character varying NOT NULL
  );
  ALTER TABLE ONLY article
  ADD CONSTRAINT 
      article_type_id_fkey FOREIGN KEY (type_id) REFERENCES article_type(id);
  </code></pre>
  
  <p>The basic access to this works (via DataSource Object defined in application.properties and letting Spring Boot handle the rest). I'm having now difficulties in understanding how to access/model this best in Spring Boot. Currently my Model Classes look like this:<br /><br />
  <b>ArticleType.java</b>
  </p>
  
  <pre><code>public class ArticleType {
      private Integer id;
      private String name;
  
      // Getters and Setters
  }
  </code></pre>
  
  <p>and<br /><br /><b>Article.java</b>
  </p>
  
  <pre><code>public class Article {
      private Integer id;
      private String name;
      private String desc;
      private ArticleType article_type;
  
      // Getters and Setters
  }
  </code></pre>
  
  <p>Following <a href="http://xantorohara.blogspot.de/2013/11/spring-boot-jdbc-sample.html" rel="nofollow">this</a> example, i was constructing those classes:
  <br /><br /><b>ArticleTypeRepository.java</b>
  </p>
  
  <pre><code>@Repository
  public class ArticleTypeRepository {
  
      @Autowired
      protected JdbcTemplate jdbc;
  
      public ArticleType getArticleType(int id) {
          return jdbc.queryForObject("SELECT * FROM article.article_type WHERE id=?", articleTypeMapper, id);
      }
  
      private static final RowMapper&lt;ArticleType&gt; articleTypeMapper = new RowMapper&lt;ArticleType&gt;() {
          public ArticleType mapRow(ResultSet rs, int rowNum) throws SQLException {
              ArticleType articletype = new ArticleType();
              articletype.setId(rs.getInt("id"));
              articletype.setName(rs.getString("type_desc"));
              return articletype;
          }
      };
  </code></pre>
  
  <p>and for the following file my question arises:<br /><br /><b>ArticleRepository.java</b>
  </p>
  
  <pre><code>@Repository
  public class ArticleRepository {
  
      @Autowired
      protected JdbcTemplate jdbc;
  
      public Article getArticle(int id) {
          return jdbc.queryForObject("SELECT * FROM article.article WHERE id=?", articleMapper, id);
      }
  
      private static final RowMapper&lt;Article&gt; articleMapper = new RowMapper&lt;Article&gt;() {
          public Article mapRow(ResultSet rs, int rowNum) throws SQLException {           
              Article article = new Article();
              article.setId(rs.getInt("id"));
              article.setName(rs.getString("name"));
              // The following line is the one in question
              // ArticleType at = getArticleType(Integer.parseInt(rs.getString("type_id")));
              article.setArticle_type(at);
              article.setDesc(rs.getString("description"));
              return article;
          }
      };
  </code></pre>
  
  <p>What is the best practice to get the ArticleType here for the Article? Is this anyway good practice to retrieve those objects? Or should I just use a plain String object in the Article Object and query this with a view or something? I looked through the internet for "Spring Boot JDBC Nested Object Java Access Modeling" and the alike, but couldn't find any real hints or tutorials to this specific question, which makes me wonder if i'm doing something conceptually completely wrong. Any hints are appreciated (tutorials, doc's, paradigms how to do this properly, etc.)</p>