목차

JPA and Native Query (SQL)

NativeSQL 결과 암묵적 Entity 매핑

Book b = (Book) em.createNativeQuery("SELECT * FROM book b WHERE id = 1", Book.class).getSingleResult();

NativeSQL 결과 명시적 Entity 매핑 : ResultSetMapping

XML 예제

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
  xmlns="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
    http://java.sun.com/xml/ns/persistence/orm
    http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
  version="2.0">
  <sql-result-set-mapping name="nativesql_mapping_example">
    <entity-result entity-class="kr.pe.kwonnam.Book">
      <field-result name="id" column="bk.id" />
      <field-result name="title" column="bk.title" />
      <field-result name="price" column="bk.price" />
      <!-- Author writer, Long writerId 매핑이 함께 존재할경우 -->
      <field-result name="writerId" column="bk.writer_id" />
      <field-result name="writer" column="bk.writer_id" />
    </entity-result>
    <entity-result entity-class="kr.pe.kwonnam.Author">
      <field-result name="id" column="at.id" />
      <field-result name="name" column="at.name" />
    </entity-result>
  </sql-result-set-mapping>
</entity-mappings>

필드 매핑시 주의할 점

in 파라미터 매핑

참고