【问题标题】:Unable to find column reference in the @MapsId mapping: item_brandId无法在 @MapsId 映射中找到列引用:item_brandId
【发布时间】:2020-11-23 03:03:28
【问题描述】:

我正在尝试使用多个映射和复合主键。当我运行我的代码时,代码会产生异常。这是我的代码。 品牌、商品和畅销书表

CREATE TABLE IF NOT EXISTS `mydb`.`brand` (
  `brandId` INT NOT NULL AUTO_INCREMENT,
  `brandName` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`brandId`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mydb`.`item` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `itemId` INT NOT NULL,
  `itemDesc` VARCHAR(45) NOT NULL,
  `itemPic` BLOB NOT NULL,
  `itemBarcode` INT NOT NULL,
  `itemShortDesc` VARCHAR(45) NULL,
  `price` INT NOT NULL,
  `salesPrice1` INT NULL,
  `salesPrice2` INT NULL,
  `salesPrice3` INT NULL,
  `item_brandId` INT NOT NULL,
  `SubCategory_subCatId` INT NOT NULL,
  PRIMARY KEY (`id`, `item_brandId`, `SubCategory_subCatId`),
  CONSTRAINT `fk_Item_Brand1`
    FOREIGN KEY (`item_brandId`)
    REFERENCES `mydb`.`brand` (`brandId`),
  CONSTRAINT `fk_item_SubCategory1`
    FOREIGN KEY (`SubCategory_subCatId`)
    REFERENCES `mydb`.`subCategory` (`subCatId`)
    ON DELETE NO ACTION`
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mydb`.`bestseller` (
  `sellerId` INT NOT NULL AUTO_INCREMENT,
  `sellerName` VARCHAR(45) NULL DEFAULT NULL,
  `item_id` INT NOT NULL,
  PRIMARY KEY (`sellerId`, `item_id`),
  CONSTRAINT `fk_bestseller_item1`
    FOREIGN KEY (`item_id`)
    REFERENCES `mydb`.`item` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Brand.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="brand")
public class Brand {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="brandId")
    private int brandId;
    
    @Column(name="brandName")
    private String brandName;
    
    public Brand() {
        
    }
    
    public Brand(String brandName)
    {
        this.brandName=brandName;
    }
    //getter and setter 
}

Item.java

package com.entity;


import java.util.Arrays;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="item")
@IdClass(ItemEmbeddable.class)
public class Item {
    @Id
    @Column(name="id")
    private int id;
    
    @Column(name="itemId")
    private int itemId;
    
    @Column(name="itemDesc")
    private String itemDesc;
    
    @Column(name="itemPic")
    private byte[] itemPic;
    
    @Column(name="itemBarcode")
    private int itemBarcode;
    
    @Column(name="itemShortDesc")
    private String itemShortDesc;
    
    @Column(name="price")
    private int price;
    
    @Column(name="salesPrice1")
    private int salesPrice1;
    
    @Column(name="salesPrice2")
    private int salesPrice2;
    
    @Column(name="salesPrice3")
    private int salesPrice3;
    
    @Id
    @OneToOne
    @JoinColumn(name="item_brandId",referencedColumnName="brandId")
    private Brand brand;
    
    @Id
    @OneToOne
    @JoinColumn(name="SubCategory_subCatId",referencedColumnName="subCatId")
    private SubCategory subcategory;
    
    public Item()
    {
        
    }

    public Item(int itemId, String itemDesc, byte[] itemPic, int itemBarcode, String itemShortDesc, int price,
            int salesPrice1, int salesPrice2, int salesPrice3) {
        this.itemId = itemId;
        this.itemDesc = itemDesc;
        this.itemPic = itemPic;
        this.itemBarcode = itemBarcode;
        this.itemShortDesc = itemShortDesc;
        this.price = price;
        this.salesPrice1 = salesPrice1;
        this.salesPrice2 = salesPrice2;
        this.salesPrice3 = salesPrice3;
    }

    //getter and setter
}

ItemEmbedable.java

package com.entity;

import java.io.Serializable;

public class ItemEmbeddable implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int id;
    private int brand;
    private int subcategory;
    
    public ItemEmbeddable()
    {
        
    }
    
    public ItemEmbeddable(int id, int subCategory, int brand) {
        super();
        this.id = id;
        this.subcategory = subCategory;
        this.brand = brand;
    }
    //getter,setter, hashcode and equal
}

BestSeller.java

package com.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;


@Entity
@Table(name="bestseller")
@IdClass(BestSellerEmbeddable.class)
public class BestSeller {

    @Id
    @Column(name="sellerId")
    private int sellerId;
    
    @Column(name="sellerName")
    private String sellerName;
    
    @Id
    @OneToOne
    @JoinColumn(name="item_id",referencedColumnName="id")
    private Item item;

    public BestSeller() {
        super();
        // TODO Auto-generated constructor stub
    }
    

    public BestSeller(String sellerName) {
        super();
        this.sellerName = sellerName;
        
    }

}

BestSellerEmbeddable.java

package com.entity;

import java.io.Serializable;

public class BestSellerEmbeddable implements Serializable{

    private static final long serialVersionUID = 1L;
    private int sellerId;
    private int item;
    
    public BestSellerEmbeddable() {
        super();
        // TODO Auto-generated constructor stub
    }

    public BestSellerEmbeddable(int sellerId, int item) {
        super();
        this.sellerId = sellerId;
        this.item = item;
    }

    //getter and setter
}

代码将产生以下异常。如何更改代码以使其正常工作?

org.hibernate.AnnotationException: Unable to find column reference in the @MapsId mapping: item_brandId

【问题讨论】:

    标签: java mysql spring hibernate jpa


    【解决方案1】:

    我建议您使用真正的可嵌入 id,因为 id 类有一些粗糙的边缘。使用这样的东西:

    @Embeddable
    public class BestSellerEmbeddable implements Serializable { ... }
    
    @Entity
    @Table(name="bestseller")
    public class BestSeller {
    
        @EmbeddedId
        private BestSellerEmbeddable id;
        
        @Column(name="sellerName")
        private String sellerName;
        
        @OneToOne
        @JoinColumn(name="item_id", insertable = false, updatable = false)
        private Item item;
    
    }
    

    【讨论】:

    • 感谢您的回答@ChristianBeikov,但仍然是这个例外-> A Foreign key refering com.entity.Item from com.entity.BestSeller has the wrong number of column. should be 3
    • 不可能仅通过唯一键的一部分来引用实体。可能有多行具有相同的item_id。您可以将其映射为 @OneToMany @JoinColumn(name = "item_id", referencedColumn = "item_id") Set<Item> items; 或首先考虑为什么需要复合 ID。也许您可以简单地将 id 设为主键?
    猜你喜欢
    • 2013-01-16
    • 2015-01-26
    • 1970-01-01
    • 2018-02-27
    • 2012-07-11
    • 1970-01-01
    • 2019-03-27
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多