Amarnath Reddy Dornala
Amarnath Reddy Dornala

Reputation: 189

Composite Primary Key usage in REST Spring Boot

I never saw this form helpful to me. I wish it will be now.

In my spring-boot, My database has two primary keys

TransId and PrsnRef

Now When I create Entity's for my database Table named PersonCore I am getting 2 classes

PersonCore (As known this has all rest of data)

/**
 * The persistent class for the PERSON_CORE database table.
 * 
 */
@Entity
@Table(name="PERSON_CORE")
@NamedQuery(name="PersonCore.findAll", query="SELECT f FROM PersonCore f")
public class PersonCore implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private PersonCorePK id;

    @Column(name="PRSN_AGE")
    private BigDecimal prsnAge;

    @Column(name="PRSN_AMER_INDIAN_ALASKA_NTV")
    private String prsnAmerIndianAlaskaNtv;

    @Column(name="PRSN_DOB")
    private String prsnDob;

PersonCorePK (This has two Id's transid and personref)

    /**
 * The primary key class for the PERSON_CORE database table.
 * 
 */
@Embeddable
public class PersonCorePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="TRANS_ID")
    private String transId;

    @Column(name="PRSN_REF")
    private String prsnRef;

    public FdshPersonCorePK() {
    }
    public String getTransId() {
        return this.transId;
    }
    public void setTransId(String transId) {
        this.transId = transId;
    }
    public String getPrsnRef() {
        return this.prsnRef;
    }
    public void setPrsnRef(String prsnRef) {
        this.prsnRef = prsnRef;
    }

I am trying to create a RESTFul service using spring-boot I created repository with @RepositoryRestResource(bla bla bla)

@RepositoryRestResource(path = "person", collectionResourceRel = "FdshPersonHDR")
public interface PersonCoreRepository extends PagingAndSortingRepository<PersonCore, PersonCorePK> {
    Collection<PersonCore > findAll();
    Collection<PersonCore > findAllByIdTransIdAndIdPrsnRef(@Param("transid")String transId, @Param("prsnRef") String prsnRef);
}

Spring boot is so awesome that it takes good care of me. Lets see ...

When I try to fetch all records from database its all fine except the final link is something like this

http://localhost:8080/person/com.----.Application.Methods.PersonCorePK@83e14116

This is driving me crazy. I know its reading what I want but not displaying me exactly what I need. I want help figuring out how I can convert that "com.----.Application.Methods.PersonCorePK@83e14116" . It should actually display transid of that record.

And please let me know if there is anyway we can add those two composite keys to that o/p format because I am getting all data I want except those two which are kind of important for me. I am not sure I can use two @Id's in single Entity. But I will check into it.

Sample Screenshot

Upvotes: 2

Views: 6983

Answers (1)

K.Nicholas
K.Nicholas

Reputation: 11561

What you have is the output of a toString() call on a PersonCorePK object. If you want to change the way this object is represented as a String, you need to override the toString() method for the PersonCorePK class. Something like this:

EDIT: New toString() method.

@Override
public String toString() {
    return "/search/findAllByIdTransIdAndIdPrsnRef?transId="+transId+"&prsnRef="+prsnRef;
}

I tried that it it worked for me. Slow down and be sure you understand is going on. Also, note that I changed (@Param("transid") to (@Param("transId") with an uppercase I.

Also, I added a constructor for the PersonCorePK:

public PersonCorePK(String transId, String prsnRef) {
    this.transId = transId;
    this.prsnRef = prsnRef;
}

Finally, when I tested the application I made sure to put a Content-Type of application/json in the POST request so that the system understands the data it is receiving.

I used Accessing JPA Data with REST as a guide.

Upvotes: 3

Related Questions