如何使用jpa在select中映射子查询

时间:2017-02-17 00:37:59

标签: java spring hibernate jpa

我正在使用spring,jpa,hibernate,mysql,java 8,.In Dao(这是inteface并扩展JpaRepository) MyTable类

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();
}

POM

<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属性。其他值可以在实体中获得。

如果我使用没有@Transient的col1属性,它将起作用,但col1不是持久的(同时findAll()也不起作用,因为db上没有col1列)。我该怎么办 ?

1 个答案:

答案 0 :(得分:1)

您有很多选择,但如果您使用的是JPA 2.1,则可以尝试以下步骤:

1)创建一个合适的DTO,其中应包含将获取查询结果的属性: &#34; SELECT mt。*,(从anotherTable中选择count(1))作为来自MY_TABLE mt的col1&#34;

2)在MyTable实体中,使用@SqlResultSetMapping和@ConstructorResult注释将本机查询映射到DTO(谷歌上的简单搜索将为您提供有关其用法的大量有用信息)。(参见http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html

3)修改MyTableDao以使用您在步骤2中创建的本机查询。

我希望这会有所帮助。古德勒克:)