I am building a SpringBoot API and have entity CarEntity with a composite key
@Entity
@Table(name = "CAR")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class CarEntity implements Serializable {
private static final long serialVersionUID = ...;
@EmbeddedId
private CarEntityId id; // COMPOSITE PK
@Column(name = "SELL_ID")
private String sellId;
@Formula("amount / 100")
@Column(name = "AMOUNT")
private Double amount;
@Column(name = "DESCRIPTION")
private String description;
...
}
And here is the composite key embeddable is CarEntityId defined as:
@Embeddable
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class CarEntityId implements Serializable {
private static final long serialVersionUID = -...
@Column(name = "PRODUCTION_DATE", length = 8, nullable = false)
@Convert(converter = CarProductionDateConverter.class)
private LocalDate productionDate;
@Column(name = "TYPE_CODE", length = 1, nullable = false)
private String typeCode;
@Column(name = "SEQ_NUM", length = 4, nullable = false)
private int seqNum;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "ASSEMBLY_ID", nullable = false)
private AssemblyEntity assemblyEntity;
@Override
public boolean equals(Object o) {
...
}
@Override
public int hashCode() {
return ...
}
}
When I issue GET request to get some data, I get InvalidDataAccessResourceUsageException:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement
its cause field is a JdbcSQLSyntaxErrorException:
rg.h2.jdbc.JdbcSQLSyntaxErrorException: Column "CARENTITY0_.AMOUNT" not found
WHAT I FOUND SO FAR:
I looked in H2 console CAR table and I found that indeed, the AMOUNT field is missing. The only difference for AMOUNT field definition in the CarEntity (it is nullable) is that it is annotated with @Format annotation.
I am trying to understand why I am getting this error but I am not going anywhere with it. Any help would be greatly appreciated.
In the
@Formulaannotation you should use raw SQL. In your case:instead of
And remove the
@Columnannotation. The field should be calculated and not mapped from the database column.If you want to have a column created by JPA then you should separate both fields. Like amount and amountInCents.