2017-02-17 11 views
0

、JPA、休止状態は、MySQL、Javaの8で選択してサブクエリをマッピングするために、(JpaRepository intefaceをされ、拡張).INダオ MyTableというクラスは、どのように私は春を使用していますJPA

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.persistence.Transient; 

@Entity 
@Table(name = "MY_TABLE") 
public class MyTable { 

    @Id 
    @Column(name = "ID") 
    private long id; 
    @Column(name = "ITEM_NAME") 
    private String itemName; 
    @Column(name = "ITEM_PRICE") 
    private int itemPrice; 
    @Transient 
    private int col1; 

    public MyTable() { 
    } 
    public MyTable(long id, String itemName, int itemPrice, int col1) { 
     super(); 
     this.id = id; 
     this.itemName = itemName; 
     this.itemPrice = itemPrice; 
     this.col1 = col1; 
    } 
    public long getId() { 
     return id; 
    } 
    public void setId(long id) { 
     this.id = id; 
    } 
    public String getItemName() { 
     return itemName; 
    } 
    public void setItemName(String itemName) { 
     this.itemName = itemName; 
    } 
    public int getItemPrice() { 
     return itemPrice; 
    } 
    public void setItemPrice(int itemPrice) { 
     this.itemPrice = itemPrice; 
    } 
    public int getCol1() { 
     return col1; 
    } 
    public void setCol1(int col1) { 
     this.col1 = col1; 
    } 


} 

MyTableDao

import java.util.List; 

import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.data.jpa.repository.Query; 

import com.kolip.devchain.model.MyTable; 

public interface MyTableDao extends JpaRepository<MyTable , Long> { 

    @Query(value = "SELECT mt.*, (select count(1) from anotherTable) as col1 from MY_TABLE mt",nativeQuery = true) 
    List<MyTable> getMyTableList(); 
} 

ポンポン

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.0.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
<!--  <dependency> --> 
<!--   <groupId>org.springframework.boot</groupId> --> 
<!--   <artifactId>spring-boot-starter-security</artifactId> --> 
<!--  </dependency> --> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 

      <dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>5.1.38</version> 
</dependency> 

<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-core</artifactId> 
    </dependency> 

    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 

     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 

     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 

     <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-entitymanager</artifactId> 

</dependency> 

     <dependency> 
    <groupId>com.github.stefanbirkner</groupId> 
    <artifactId>system-rules</artifactId> 
    <version>1.16.0</version> 
</dependency> 

     <dependency> 
      <groupId>org.scala-lang</groupId> 
      <artifactId>scala-library</artifactId> 
      <version>2.11.0</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.datatype</groupId> 
      <artifactId>jackson-datatype-hibernate5</artifactId> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

MY_TABLE

CREATE TABLE `my_table` (
    `ID` bigint(20) NOT NULL, 
    `ITEM_NAME` varchar(255) DEFAULT NULL, 
    `ITEM_PRICE` int(11) DEFAULT NULL, 
    PRIMARY KEY (`ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

私は、col1の値を取得しようとするが、私は、エンティティのcol1の属性がどうあるべきかを知ることができない私のproblem.Iを簡素化してみてください。他の値をエンティティで取得できます。

@Transientなしでcol1属性を使用すると動作しますが、col1は永続的ではありません(dbにcol1列がないため、findAll()も機能しません)。私は何をすべきか ?

+0

[Spring Data JPAの結果を非エンティティPOJOにマップする](http://stackoverflow.com/questions/29082749/spring-data-jpa-map-the-result-to-non -entity-pojo) – Naros

+0

私はこのようにしましたが、同じエンティティにネイティブクエリーをマップし、namednativequeryを使用するために、エンティティクラスを設計できるように、テーブルの値の詳細を提供する必要があります。 – Akshay

答えて

1

あなたは多くのオプションを持っていますが、JPA 2.1を使用している場合は、この次のステップを試みることができる:

1)クエリの結果を取るプロパティを含めるべきか、適切なDTOを作成します。 「MTを選択しますMyTableエンティティで、@ SqlResultSetMappingアノテーションと@ConstructorResultアノテーションを使用して、ネイティブクエリをDTOにマッピングします(googleでの簡単な検索)。その使用方法に関する多くの有用な情報を提供します)(http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html参照)

3)MyTableDaoを変更して、手順2で作成したネイティブクエリを使用します。

これが役立ちます。 Goodluck :)

+0

jparepository。しかし、クエリがエンティティクラスにあるので、これはきれいには見えません。私はすべてをDAOに格納しようとします。また、私は一時的な属性のみをマップすることができ、他はデフォルトにマップされているのだろうか? – uuur

関連する問題