简体   繁体   English

如何使用Spring Data JPA查询没有主键的表

[英]How to query a table without a primary key using spring data jpa

have a simple query that I need to only run a select on. 有一个简单的查询,我只需要运行选择。 Below is the example select statement. 下面是示例select语句。

    <named-native-query name="Capacity.findByCapacityCount" result-class="com.model.Capacity">
        <query>    
SELECT MAX_VAL,
      FROM PARAMETER
     WHERE  LOCATION = :facilityCode
       AND FUN_C = :functionName
       AND PAM_TYP = :dayOfWeek
        </query>
    </named-native-query>

For the query results I created a simple Entity class called Capacity. 对于查询结果,我创建了一个简单的Entity类,称为Capacity。

@Entity 
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Capacity  {        

    @Column(name="PAM_VAL_DCM") 
    private BigDecimal slotCount;

}

When my application starts up I get an error with "No identifier specified for entity". 当我的应用程序启动时,出现“未为实体指定标识符”错误。 I understated that Entity requires a Id property to be defined but in this case the table doesn't have a true primary key. 我低估了实体需要定义一个Id属性,但是在这种情况下,表没有真正的主键。

I have also tried changing the class to a @Embedable type but I get a the following error "Errors in named queries" 我也尝试过将类更改为@Embedable类型,但出现以下错误“命名查询中的错误”

Below is the interface I have created using Repository interface. 下面是我使用Repository接口创建的接口。

public interface CapacityRepository extends Repository<Capacity,String> {

    public Capacity findByCapacityCount(@Param("facilityCode")String facilityCode,
                                                                                   @Param("functionName")String functionName,
                                                                                   @Param("dayOfWeek")String dayOfWeek);
}

So using Spring Data JPA how would configure a class using JPA annotations and the Repository interface to query a table without a primary key? 因此,使用Spring Data JPA如何配置使用JPA批注和Repository接口的类以查询没有主键的表?

I don't think this is going to work but that's not due to Spring Data JPA's limitations but JPA not allowing entities without primary keys. 我认为这行不通,但这不是由于Spring Data JPA的限制,而是JPA不允许没有主键的实体。 See section 2.4 of the JPA specification. 请参阅JPA规范的2.4节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM