NULL identifier generated using @GeneratedValue on a composite key using @EmbeddedId

Trying to save a row in a table with a complex key (Long and Date) via Spring Data JPA. The length of the composite key part is @GeneratedValue. But when making a basic save () call, I get the following error:

org.hibernate.id.IdentifierGenerationException: null id generated for:class com.bts.billing.domain.CashBatchPaymentHistoryDto

The date is set manually before saving (), and I checked if the sequencer exists in the database and is available.

Entity

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

    @EmbeddedId
    private CashBatchPaymentHistoryPK pk;

    private Date        cashBatchProcDt;
    @Column(name = "C_CBP_STS", nullable = false)
    private String      cashBatchStatus;
    @Column(name = "C_CBP_TYP", nullable = false)
    private String      cashBatchType;

    //Removed Getters & Setters To Save Space
}

@Embeddable Class

@Embeddable    
@SequenceGenerator(name = "CBPMT", sequenceName = "CBPMT", allocationSize = 1)
public class CashBatchPaymentHistoryPK implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "N_CBP_OID", nullable=false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CBPMT")
    private Long cashBatchID;
    @Column(name = "D_CBP_PROC", nullable=false)
    private Date cashBatchProcDt;

    public CashBatchPaymentHistoryPK() {}

    public CashBatchPaymentHistoryPK(long cashBatchID, Date cashBatchProcDt) {
        this.cashBatchID = cashBatchID;
        this.cashBatchProcDt = cashBatchProcDt;
    }

    //Removed Getters & Setters For Space

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (cashBatchID ^ (cashBatchID >>> 32));
        result = prime * result + (int) (cashBatchProcDt.hashCode() ^ (cashBatchProcDt.hashCode() >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        CashBatchPaymentHistoryPK other = (CashBatchPaymentHistoryPK) obj;
        if (cashBatchID != other.cashBatchID) {
            return false;
        }
        if (cashBatchProcDt != other.cashBatchProcDt) {
            return false;
        }
        return true;
    }
}

Storage class

public interface CashBatchPaymentHistoryRepository extends CrudRepository<CashBatchPaymentHistoryDto, CashBatchPaymentHistoryPK> {
}

Any thoughts on this? Thank!

+4
source share
1 answer

, (Date, long, , ? , pk null, CashBatchPaymentHistoryDto repository.save(…). , EntityManager.merge(…), , , .

, id ( ), ( , persist(…) merge(…) JPA). , id null , null .

:

  • @Version save(…).
  • Persistable isNew(), , , ( , , (pk.cashBatchID null).
+4

Source: https://habr.com/ru/post/1543611/


All Articles