1

I am working on an existing schema where a table has a composite primary key with one value autogenerated and another Id assigned. Below is the code:

@Entity
@Table(name = "emp_details")
public class Emp implements Serializable {

    private Long id;
    private String bank_emp_id;

    @Id
    @GeneratedValue
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Id
    @Column(name = "bank_emp_id")
    public String getBank_emp_id() {
        return bank_emp_id;
    }

    public void setBank_emp_id(String bank_emp_id) {
        this.bank_emp_id = bank_emp_id;
    }

}

On trying to add a record, the insert statement is failing with error:

Field 'bank_emp_id' doesn't have a default value org.hibernate.exception.GenericJDBCException: could not insert

On further analysis, it looks like the insert statement doesn't include the column bank_emp_id.

Please suggest any changes if required.

dynamoG
  • 39
  • 1
  • 3
  • 11
  • Hope this will help you. http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate – Vikas May 11 '17 at 04:53
  • are you sure the composite key implementation here is okay ? you could see this http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#mapping-declaration-id – Shafin Mahmud May 11 '17 at 04:54
  • Hope this will help you . https://vladmihalcea.com/2016/08/01/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/ – Harshad_Kenjale May 11 '17 at 10:29

0 Answers0