简体   繁体   English

复合主键错误

[英]Composite Primary Key Error

I want to make a table, let's say table's name is Car. 我想做一张桌子,假设桌子的名字是Car。 It will has 3 column, brandId , typeId and sizeId . 它将具有3列brandIdtypeIdsizeId I want all of the columns to be primary key . 我希望所有列都是主键 typeId and sizeId are column from different table. typeIdsizeId是来自不同表的列。 I already try to make code, but if I use annotation @Id for each column, error will appear "No supertype found". 我已经尝试编写代码,但是如果我在每列中都使用注释@Id ,则会出现错误“找不到超类”。

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. 因此,我正在谷歌搜索,发现可以使用@EmbeddedId创建复合主键。 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 . 然后,我想基于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". “无法根据路径解析属性[brandId]”。

So I create setter getter for brandId in Entity class :`` 所以我在实体类中为brandId创建了setter getter:``

@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. 我不知道是否有可能通过找到brandId ,当brandId的列是Embeddable类。

  2. Is setter getter for brandId in Entity class is true? Entity类中brandId setter getter是否正确?

Thank you! 谢谢!

You have a compound primary key. 您有一个复合主键。 See Compound Primary Keys . 请参阅复合主键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM