title: ColumnTransformer in hibernate
body: <p>I have an Entity which I use the <code>ColumnTransformer</code> for bind and extract values:</p>
  
  <pre><code>@Entity
  class BPoint {
      @Id
      private Integer id;
  
      @ColumnTransformer(read = "astext(shape)", write = "toshape(?)")
      private Shape shape;
  
  }
  </code></pre>
  
  <p>And the dao:</p>
  
  <pre><code>class BPointDao {
      @Autowired
      private EntityManager em;
  
      @Override
      public Page&lt;BPoint&gt; findAll(Pageable pageable) {
          Query q = em.createQuery("from BPoint");
          List&lt;BPoint&gt; r = q.getResultList();
          int total = em.createQuery("select count(*)  from BPoint").getFirstResult();
          return new PageImpl(r, pageable, total);
      }
      @Override
      public Integer save(BPoint hbds) {
          em.persist(hbds);
          return hbds.getId();
      }
  }
  </code></pre>
  
  <p>It works, however once I have to do some query which need the use the sql functions, I meet some problems, take this valid native sql for example:</p>
  
  <pre><code>select * from BPoint h where inside(h.shape, 100) = 1;
  </code></pre>
  
  <p>First I tried to use the hql like this:</p>
  
  <pre><code>Query q = em.createNativeQuery("select astext(shape) from BPoint h where inside(h.shape, ?) = 1");
  </code></pre>
  
  <p>However I found that the generated sql contains things like </p>
  
  <p><code>...... where inside(astext(h.shape),100).....</code> </p>
  
  <p>It seems that the <code>ColumnTransformer read</code> value is used in the sql function <code>inside</code> which is not expected.</p>
  
  <p>So I tried to use the native sql query like this:</p>
  
  <pre><code>Query q = em.createNativeQuery("select * from BPoint h where inside(h.shape, ? = 1");
  </code></pre>
  
  <p>Not the sql can be executed, but the result cannot be mapped correctly.</p>
  
  <p>Then I have to add the <code>select fileds</code> manualy like this:</p>
  
  <pre><code>Query q = em.createNativeQuery("select id,astext(shape) from BPoint h where inside(h.shape, ? = 1");
  </code></pre>
  
  <p>But how about if my Entity have a lot of fileds say it is more than 20?  And how about if some columns name changed?</p>
  
  <p>Is there an alternative method to meet my requirement?</p>