简体   繁体   中英

How to resolve IdClass Property not found issue when creating composite primary key using JPA

I am having issues creating composite primary key

I am getting org.hibernate.AnnotationException: Property of @IdClass not found this exception.

My serializable implementing class and model class is given below:

public class MakeCompositeKey implements Serializable {
    private String codeName;
    private int year;

    public MakeCompositeKey() {
    }

    public MakeCompositeKey(String codeName, int year) {
        this.codeName = codeName;
        this.year = year;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
}

and

@Entity

@IdClass(MakeCompositeKey.class)
    
public class Averages {

    @Id
    @Column(name = "code_name")
    private  String CodeName;
    @Id
    private  int year;


    @Column(name = "weighted_average_shs_out")
    private double Weighted_Average_Shs_Out;

    @Column(name = "weighted_average_shs_dil")
    private double Weighted_Average_Shs_Out_Dil;

    @Column(name = "average_receivables")
    private double Average_Receivables;

    @Column(name = "average_payables")
    private double Average_Payables;

    @Column(name = "average_inventory")
    private double Average_Inventory;
    
    //getters and setters 
}

I don't understand what's going wrong. Please, help me resolve this issue.

Thank you!

MakeCompositeKey should be annotated with @Embeddable

Look at this: How to map a composite key with JPA and Hibernate?

You have a typo here which probably breaks everything up.

public class Averages {
  @Id
  @Column(name = "code_name")
  private String CodeName;  // <----this should be codeName instead of CodeName
  
  //...class implementation
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM