Unable to find column reference in the @MapsId mapping: item_brandId

Viewed 1113

I am trying to use multiple mapping and composite primary key. When I run my code, the code will generate exception. Here is my code. brand, item and bestseller table

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
}

The code will generate following exception.How can I change my code to get it to work?

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

I would recommend you use real embeddable ids as id classes have some rough edges. Use something like this:

@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;

}
Related