Spring Data JPA repository retrieves Entity with IdClass composite key as null from DB

747 views Asked by At

I think the repository can read the records within the DB as requested (can count the records that meets the where clause condition) but there is a problem mapping the retrieved records into Entity.

Here is my IdClass:

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class MetaEdgeInfoId implements Serializable {

    @Column(name = "SRC_NODE", nullable = false, length = 30)
    private Integer srcNode;
    @Column(name = "DST_NODE", nullable = false, length = 30)
    private Integer dstNode;
    @Column(name = "BR_ID", length = 30)
    private Integer brId;

    @Override
    public int hashCode() {
        return (this.srcNode.hashCode() + this.dstNode.hashCode() + this.brId.hashCode());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        MetaEdgeInfo other = (MetaEdgeInfo) obj;
        return (Objects.equals(this.srcNode, other.getSrcNode()) &&
                Objects.equals(this.dstNode, other.getDstNode()) &&
                Objects.equals(this.brId, other.getBrId()));
    }
}

I looked up the internet and found out that the IdClass must be public, there must be a NoArgsConstructor, Serializable must be implemented, and hashCode() and equals() must be implemented as well.

Here is my Entity class:

@Entity
@Getter
//@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor
@AllArgsConstructor
@Table(schema = "HYPERDATA", name = "META_EDGE_INFO")
@IdClass(MetaEdgeInfoId.class)
public class MetaEdgeInfo implements Serializable {

    @Id
    @Column(name = "SRC_NODE", nullable = false, length = 30)
    private Integer srcNode;

    @Id
    @Column(name = "DST_NODE", nullable = false, length = 30)
    private Integer dstNode;

    @Id
    @Column(name = "BR_ID", length = 30)
    private Integer brId;

    @Column(name = "EDGE_TYPE", nullable = false)
    private String edgeType;
}

Lastly, here is my repository with a method that I will show the result of:

public interface MetaEdgeInfoRepository extends JpaRepository<MetaEdgeInfo, MetaEdgeInfoId>,
        MetaEdgeInfoRepositoryCustom {

    List<MetaEdgeInfo> findByEdgeType(String edgeType);
}

Here is my controller to test the find method:

@GetMapping("/va1/test/om/writeValueAsString")
public void test_wvas() throws JsonProcessingException {
    List<MetaEdgeInfo> table = metaEdgeInfoRepository.findByEdgeType("TABLE");
    System.out.println(table);
}

Result is below: Knows how many records, but shown as null

I first tried using EmbeddedId & Embeddable, but could not figure it out and trying IdClass, still struggling. It'd be great if I can get a hint of why my set up is not working.

Best,

0

There are 0 answers