I'm using Spring Boot version 3.1.7 and I'm trying to perform an insert query into a database which has a table I introspected to create an entity class, it is made up of 3 primary keys and so a primary key class was autogenerated.
My issue is whenever I try to save the object I create (using lombok builder syntax) I'm getting this error
org.springframework.dao.InvalidDataAccessApiUsageException: Supplied id had wrong type: entity 'com.sageapservice.entity.mssql.Apibs' has id type 'class com.sageapservice.entity.mssql.ApibsPK' but supplied id was of type 'class java.lang.Integer'
(The name of the class in Apibs and the primary key class is ApibsPK)
I tried just giving the values to the functions as I normally would but I'm getting the error highlighted above. I also tried creating an object of the PK class and passing it as the ID of the entity but since the builder function will not accept fields which do not already belong to the entity class.
I was not able to even run the program.
Here is a part of my entity class
@Entity
@AllArgsConstructor
@Builder
@NoArgsConstructor
@IdClass(ApibsPK.class)
public class Apibs {
@Id
@Column(name = "CNTBTCH", nullable = false, precision = 0)
private int cntbtch;
@Id
@Column(name = "CNTITEM", nullable = false, precision = 0)
private int cntitem;
@Id
@Column(name = "CNTPAYM", nullable = false, precision = 0)
private int cntpaym;
@Basic
@Column(name = "AUDTDATE", nullable = false, precision = 0)
private int audtdate;
@Basic
@Column(name = "AUDTTIME", nullable = false, precision = 0)
private int audttime;
@Basic
@Column(name = "AUDTUSER", nullable = false, length = 8)
private String audtuser;
@Basic
@Column(name = "AUDTORG", nullable = false, length = 6)
private String audtorg;
@Basic
@Column(name = "DATEDUE", nullable = false, precision = 0)
private int datedue;
// ...
}
This in my PK class
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ApibsPK implements Serializable {
@Column(name = "CNTBTCH", nullable = false, precision = 0)
@Id
private int cntbtch;
@Column(name = "CNTITEM", nullable = false, precision = 0)
@Id
private int cntitem;
@Column(name = "CNTPAYM", nullable = false, precision = 0)
@Id
private int cntpaym;
// ...
}
You can use @EmbeddedId
for composite key as below.
Create a @Embeddable
class that contain all your primary key column.
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Embeddable
public class ApibsPK implements Serializable {
@Column(name = "CNTBTCH", nullable = false, precision = 0)
private int cntbtch;
@Column(name = "CNTITEM", nullable = false, precision = 0)
private int cntitem;
@Column(name = "CNTPAYM", nullable = false, precision = 0)
private int cntpaym;
}
Your Entity Class using above class as @EmbeddableId
@Entity
@AllArgsConstructor
@Builder
@NoArgsConstructor
public class Apibs implements Serializable {
@EmbeddedId
private ApibsPK apibsPK;
@Basic
@Column(name = "AUDTDATE", nullable = true, precision = 0)
private int audtdate;
@Basic
@Column(name = "AUDTUSER", nullable = true, length = 8)
private String audtuser;
// removing other column for brevity.
}
Create an interface as below
@Repository
public interface ApibsRepository extends JpaRepository<Apibs, ApibsPK> {
}
Set the required values using lombok builder pattern and call save() method to save the record in DB.
@Bean
CommandLineRunner commandLineRunner() {
return args -> {
apibsRepository.save(Apibs.builder()
.apibsPK(ApibsPK.builder()
.cntitem(1)
.cntbtch(2)
.cntpaym(3)
.build())
.build());
};
}