I want to make a table, let's say table's name is Car. It will has 3 column, brandId, typeId and sizeId. I want all of the columns to be primary key. typeId and sizeId are column from different table. I already try to make code, but if I use annotation @Id for each column, error will appear "No supertype found".
The code is below.
@Entity
@Table(name = "CAR")
public class Car implements Serializable {
private static final long serialVersionUID = -1576946068763487642L;
@Id
@Column(name = "BRAND_ID", nullable = false, length = 20)
private String brandId;
@Id
@ManyToOne
@JoinColumn(name = "TYPE_ID", nullable = false)
private TypeId typeId;
@Id
@ManyToOne
@JoinColumn(name = "SIZE_ID", nullable = false)
private SizeId sizeId;
public String getBrandId() {
return brandId;
}
public void setBrandId(String brandId) {
this.brandId= brandId;
}
public TypeId getTypeId() {
return typeId;
}
public void setTypeId (TypeId typeId) {
this.typeId= typeId;
}
public SizeId getSizeId() {
return sizeId;
}
public void setSizeId (SizeId sizeId) {
this.sizeId= sizeId;
}
}
So, I'm googling and find out that I can use @EmbeddedId to create composite primary key. The code for entity is like this :
@Entity
@Table(name = "CAR")
public class Car implements Serializable {
private static final long serialVersionUID = -1576946068763487642L;
@EmbeddedId
private CarPk carPk;
public CarPk getCarPk () {
return carPk ;
}
public void setCarPk (CarPk carPk ) {
this.carPk = carPk ;
}
}
And Embeddable class :
@Embeddable
public class CarPk implements Serializable {
private static final long serialVersionUID = -83738833L;
@Column(name = "BRAND_ID", nullable = false, length = 20)
private String brandId;
@ManyToOne
@JoinColumn(name = "TYPE_ID", nullable = false)
private TypeId typeId;
@ManyToOne
@JoinColumn(name = "SIZE_ID", nullable = false)
private SizeId sizeId;
public String getBrandId() {
return brandId;
}
public void setBrandId(String brandId) {
this.brandId= brandId;
}
public TypeId getTypeId() {
return typeId;
}
public void setTypeId (TypeId typeId) {
this.typeId= typeId;
}
public SizeId getSizeId() {
return sizeId;
}
public void setSizeId (SizeId sizeId) {
this.sizeId= sizeId;
}
}
And then, I want to make interface that will find column, based on brandId.
the interface code :
public interface CardDao extends PagingAndSortingRepository<Car, Long>, QueryDslPredicateExecutor<Car> {
public Car findByBrandId(String brandId);
}
Unfortunately, the code is error:
"Unable to resolve attribute [brandId] against path".
So I create setter getter for brandId in Entity class :``
@Entity
@Table(name = "CAR")
public class Car implements Serializable {
private static final long serialVersionUID = -1576946068763487642L;
@EmbeddedId
private CarPk carPk;
public CarPk getCarPk () {
return carPk ;
}
public void setCarPk (CarPk carPk ) {
this.carPk = carPk ;
}
public brandId getBrandId (){
return carPk.getBrandId();
}
public void setBrandId (String brandId){
this.carPk.setBrandId(brandId);
}
}
Error is not appear, but data of the table's car also doesn't appear too.
My question are :
Is the interface code is true? I wonder if it's possible to find by
brandId, when brandId's column is in theEmbeddableclass.Is setter getter for
brandIdin Entity class is true?
Thank you!
You have a compound primary key. See Compound Primary Keys.