0

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 :

  1. Is the interface code is true? I wonder if it's possible to find by brandId, when brandId's column is in the Embeddable class.

  2. Is setter getter for brandId in Entity class is true?

Thank you!

Hemi81
  • 578
  • 2
  • 15
  • 34

1 Answers1

0

You have a compound primary key. See Compound Primary Keys.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I already try to change the code like the link you give. But, it doesn't work. Error : "No supertype found". Any other solution? – Ariandi Mario Mukti Nov 06 '15 at 03:02
  • Since that's how you're supposed to do it, you likely did something wrong. Suggest you create a new question, show that attempt, and ask for help. – Andreas Nov 06 '15 at 03:11