Where to query another table?

I’m a beginner with Cuba/Java, bare with me.

I have an entity that pulls data from a table, the columns in this table contain IDs that reference another table to get the values.

Example:

checkID column in table B references the checkName column in table A with the same ID

This is my current entity code:

package uk.co.test.reportal.entity;
import javax.persistence.*;
import com.haulmont.cuba.core.global.*;
import java.util.Date;

import com.haulmont.cuba.core.entity.BaseIntIdentityIdEntity;

@DesignSupport("{'imported':true}")
@Table(name = "CheckResults")
@Entity(name = "reportal3$CheckResults")
public class CheckResults extends BaseIntIdentityIdEntity {
    private static final long serialVersionUID = 6993637252092407129L;

    @Column(name = "CheckID")
    protected Integer checkID;

    @Column(name = "ClientID")
    protected Integer clientID;

    @Column(name = "Result")
    protected Integer result;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "\"Timestamp\"")
    protected Date timestamp;

    public void setCheckID(Integer checkID) {
        this.checkID = checkID;
    }

    public Integer getCheckID() { return checkID;  }

    public void setClientID(Integer clientID) {
        this.clientID = clientID;
    }

    public Integer getClientID() {
        return clientID;
    }

    public void setResult(Integer result) {
        this.result = result;
    }

    public Integer getResult() {
        return result;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public Date getTimestamp() {
        return timestamp;
    }


}

This works as is because it lists the table data as expected. However I need to do a query in order to convert the ID to the name. Where would I put this?

Hi Lewis,

Did you map table A to an entity? Could you provide the DDL of the tables (primary keys and related attributes) and better explain relations between them?

Hi Konstantin,

Both tables are mapped to their own entity.

Table A has a Primary Key called ID, and the table also contains the Column CheckName.
Table B has a Column called CheckID, which references the PK of Table A.

I need to replace the CheckID with the CheckName value from table A.

I have been trying to set a relationship in the entity, but unfortunately I cannot get this to work as expected.

If you have EntityB with CheckID column which is a primary key of EntityA, then you can map the relationship as follows:

class EntityB {
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CheckID")
    private EntityA a;
    
}

Then you will be able to define views for EntityB that include a.checkName and display them in UI components. Or execute queries like select b from EntityB b where b.a.checkName = :some_name