@Column 省略背上的鍋

今天修改實體類時項目啓動報錯:

org.hibernate.MappingException: Unable to find column with logical name: product_type_id in org.hibernate.mapping.Table(product_type_view) and its related supertables and secondary tables

-------------------------------------------------------------------------------------------------------------------------------------

添加一對多後啓動報錯,添加@Column後,正常啓動

@Getter
@Setter
@Entity
@Table(name = "product_type_view")
public class ProductTypeView {

  /** 產品類型id {@link ProductType#id} */
  @Id
  @Column(name = "product_type_id")
  private Long productTypeId;

  /** 產品類型名 */
  private String productTypeName;
  /**
   * 父級產品id {@link ProductType#parentId}<br>
   * </>(若爲頂級產品類型,則此項爲0{@link ProductType#DEFAULT_TOP_PARENT_ID})
   */
  @Column(name = "super_type_id")
  private Long superTypeId;

  /** 級聯查詢數據結構 級聯查詢成功 */
  @OneToMany(targetEntity = ProductTypeView.class, fetch = FetchType.LAZY)
  @JoinColumn(name = "super_type_id", referencedColumnName = "product_type_id")
  List<ProductTypeView> childProductTypeList = new ArrayList<>();
}

 

(此爲jpa的自關聯查詢,一對多且自身維護外鍵,不能同時添加@ManyToOne,否則報錯無法啓動項目)

 

--------------項目試用JPA自關聯查詢,此外附上--------------------------------------

自關聯sql查詢語句示例:

SELECT
--     pa.id as id,
--     pa.product_type_name AS pa_type_name,
--     pb.parent_id AS parent_id,
--     pb.id as pb_id,
--     pb.product_type_name as pb_type_name

*
FROM
    `product_type_view` AS pa
    JOIN product_type_view AS pb ON pa.product_type_id=pb.super_type_id

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章