tin_tin
tin_tin

Reputation: 488

using spring data jpa repository if there is no primary key

Iam working on a project where i have view of a database table which is exposed by the other team and other teams are also using that view. Iam using spring data jpa in the project..

The view exposed to us does not have a primary key.

if there is no primary key how should my ViewRepository interface be declared..

I have a view which contains many values and i have created the entity as follows

package com.mf.acrs.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="mv_garage_master")
public class GarageMaster {

    @Column(name="GARAGE_CODE")
    private String garageCode;

    @Column(name="GARAGE_NAME")
    private String garageName;

    @Column(name="GARAGE_BRANCH")
    private String garageBranch; 


}

the repository interface is as follows:

package com.mf.acrs.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.mf.acrs.model.GarageMaster;

@Repository
public interface GarageMasterRepository extends JpaRepository<GarageMaster,ID>{
}

My question is what value should go in ID in the above interface..

Upvotes: 4

Views: 12188

Answers (1)

Brother
Brother

Reputation: 2220

So, Entity, JPA and Spring Data needs you to have the ID, that is how the entity is found or updated.

[UPDATED] @tintin if the garageCode is unique according to your comment, you can achieve like this:

@Entity
@Table(name="mv_garage_master")
public class GarageMaster implements Serializable {
    @Id
    @Column(name="GARAGE_CODE")
    private String garageCode;

    @Column(name="GARAGE_NAME")
    private String garageName;

    @Column(name="GARAGE_BRANCH")
    private String garageBranch; 
}

And the repository:

@Repository
public interface GarageMasterRepository extends JpaRepository<GarageMaster,String>{
}

Upvotes: 2

Related Questions