我正在使用 spring,jpa,hibernate,mysql,java 8,.In Dao(这是接口(interface)并扩展了 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() 也不起作用,因为数据库上没有 col1 列)。我应该怎么办 ?
最佳答案
您有很多选择,但如果您使用的是 JPA 2.1,则可以尝试以下步骤:
1) 创建一个合适的 DTO,其中应包含将获取查询结果的属性: “SELECT mt.*,(从 anotherTable 选择 count(1))作为 MY_TABLE mt 中的 col1”
2) 在 MyTable 实体中,使用 @SqlResultSetMapping 和 @ConstructorResult 注释将 native 查询映射到 DTO(在 google 上进行简单搜索将为您提供有关其用法的大量有用信息)。(请参阅 http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html )
3) 修改 MyTableDao 以使用您在步骤 2 中创建的 native 查询。
我希望这有帮助。祝你好运:)
关于java - 如何使用jpa映射select中的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42287358/