This Class [Entity] does not define an IdClass

Scenario: I have ‘Report’ table & a ‘Transaction’ table. The primary key column in the ‘Transaction’ table is nothing but the foreign key reference of the primary key column in the ‘Report’ table

Table1: Report

=====

ReportId (PK)

ReportName

Table2: Transaction

=======

TransactionId (PK) (Also having foreign key (FK) reference to Report :: ReportId)

Solution:

Entity class for “Report” table

@Entity
@Table(name = "report")
public class Report implements Serializable {
      private static final long serialVersionUID = 1L;

      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name = "reportid")
      Integer reportId;

      @Column(name = "reportname")
      String reportName;
}

Entity class for “Transaction” table

@Entity
@Table(name = "transaction")
@IdClass(TransactionFKReportId.class)
public class Transaction implements Serializable {
     private static final long serialVersionUID = 1L;

     @Id
     Integer transactionId;
}

IdClass definition for the Transaction entity

public class TransactionFKReportId implements Serializable {
     private static final long serialVersionUID = 1L;

     @Id
     @JoinColumn(table = "report", name = "reportid")
     @Column(name = "transactionId")
     Integer transactionId;
}

Happy Learning 🙂

Leave a comment