我正在使用spring,jpa,hibernate,mysql,java 8 ,.在Dao中(这是接口并扩展了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();
}


绒球

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

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

最佳答案

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

1)创建一个合适的DTO,其中应包含将采用查询结果的属性:
“ SELECT mt。*,(从anotherTable中选择count(1)作为MY_TABLE mt的col1”)

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

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

我希望这有帮助。祝好运 :)

10-07 19:15
查看更多