writing repository class to fetch data from 3 tables that are linked

224 Views Asked by At

I have 3 tables namely

Port_Register

Mobile_Number(Primary Key) | Gaining_Code(Foreign Key to Telecom_Codes) | Losing_Codes

Telecom_Codes

Telecom_Code(Primary Key) | Contact_Provider_Id(Foreign_Key to Contact_Provider)

Contact_Provider

Contact_Provider_Id | Contact_Name

Gaining_Code and Losing_Code are actually Telecom_Code

for the following scenario I have written entity classes which look like following

@Entity
@Table(name="Port_Register")
public class Port_Register {
    @Id
    @Column(name="MOBILE_NUMBER")
    private String mobile_Number;

    @ManyToOne
    @JoinColumn(name="GAINING_CODE",referencedColumnName="TELECOM_CODE")
    private TelecomCodes gainingCode;

    @ManyToOne
    @JoinColumn(name="LOSING_CODE",referencedColumnName="TELECOM_CODE")
    private TelecomCodes losingCSPCode;
}


    @Entity
    @Table(name="Telecom_Codes")
    public class TelecomCodes {

        @Id
        @Column(name="TELECOM_CODE")
        private String telecomCode;

        @OneToOne
        @Column(name = "CONTACT__Provider_Id")
        private ContactProvider contactProviderID;
    }
    @Entity
    @Table(name="ContactProvider")
    public class ContactProvider {
        @Column(name="CONTACT__Provider_Id")
        private String contactProviderId;

        @Column(name="CONTACT__Provider_NAME")
        private String ContactProviderName;
    }

I have to write a function in repository class to fetch data in the format

MobileNumber|Gaining_code|LosingCode| Gaining_provider_Name| Losing_provider_Name

All I know is for crud there is a predefined repository interface whose function gets to know abt implementation during run time.

so i created a repository that looks like

public interface PortOutRegisterRepository  extends CrudRepository< Ported_Register,String> {

//  public void store(T t);

//  public T retrieve(int id);

    //  public T delete(int id);
}

I thought I will call

PortOutRegisterRepository customRep;
List<Ported_Register> m=new ArrayList<Ported_Register>();
m=customRep.findall();

and then I can use this to get individual info

 for(Ported_Register p:m)
    {
    p.getGainingCode().getContactProviderID().getContactName();
    }

But this is completely wrong.

Please help me on how i should write repository for this.

Any help is appreciated

1

There are 1 best solutions below

2
On

CrudRepository allows you to find an item by passing in it's Id. findById

You can then get the individual info that you want from the object.

If you want to find all the items in the table, you could also use the findAll() method findAll

edit: You have to make sure that there is to be referenced in the other tables.