zachariahyoung
zachariahyoung

Reputation: 831

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.

    <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 
@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.

I have also tried changing the class to a @Embedable type but I get a the following error "Errors in named queries"

Below is the interface I have created using Repository interface.

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?

Upvotes: 0

Views: 5772

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83081

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. See section 2.4 of the JPA specification.

Upvotes: 2

Related Questions