复合键-休眠

时间:2018-11-12 11:15:53

标签: java hibernate spring-boot jpa entity

我有一个表GROCERY,其结构如下:

CREATE TABLE grocery 
( 
     gro_id           NUMBER, 
     gro_name         VARCHAR(32), 
     gro_dep_name     VARCHAR(32), 
     gro_price        NUMBER(16, 2), 
     gro_max_discount NUMBER(16, 2), 
     CONSTRAINT gro_pk PRIMARY KEY (gro_id, gro_dep_name) 
  )

我的问题是,当我尝试从表中获取数据(保存在oracle数据库中)时,出现以下错误:

  

org.hibernate.id.IdentifierGenerationException:生成的null ID   for:class com.domain.Grocery

我已经根据表格的结构生成了以下实体类:

Grocery.java

package com.domain;

import java.math.BigDecimal;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;

/**
 * Grocery generated by hbm2java
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "GROCERY", schema = "TPRDBA")
public class Grocery implements java.io.Serializable {

    @EmbeddedId
    private GroceryId id;
    private String groName;
    private BigDecimal groPrice;
    private BigDecimal groMaxDiscount;

    public Grocery() {
    }

    public Grocery(GroceryId id) {
        this.id = id;
    }

    public Grocery(GroceryId id, String groName, BigDecimal groPrice, BigDecimal groMaxDiscount) {
        this.id = id;
        this.groName = groName;
        this.groPrice = groPrice;
        this.groMaxDiscount = groMaxDiscount;
    }

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "groId", column = @Column(name = "GRO_ID", nullable = false, precision = 22, scale = 0)),
            @AttributeOverride(name = "groDepName", column = @Column(name = "GRO_DEP_NAME", nullable = false, length = 32)) })
    public GroceryId getId() {
        return this.id;
    }

    public void setId(GroceryId id) {
        this.id = id;
    }
    @Column(name = "GRO_NAME", length = 32)
    public String getGroName() {
        return this.groName;
    }
    public void setGroName(String groName) {
        this.groName = groName;
    }
    @Column(name = "GRO_PRICE", precision = 16)
    public BigDecimal getGroPrice() {
        return this.groPrice;
    }
    public void setGroPrice(BigDecimal groPrice) {
        this.groPrice = groPrice;
    }
    @Column(name = "GRO_MAX_DISCOUNT", precision = 16)
    public BigDecimal getGroMaxDiscount() {
        return this.groMaxDiscount;
    }
    public void setGroMaxDiscount(BigDecimal groMaxDiscount) {
        this.groMaxDiscount = groMaxDiscount;
    }
}

GroceryId.java

package com.domain;
// Generated Nov 12, 2018 11:42:16 AM by Hibernate Tools 4.3.1.Final

import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * GroceryId generated by hbm2java
 */
@SuppressWarnings("serial")
@Embeddable
public class GroceryId implements java.io.Serializable {


    private BigDecimal groId;
    private String groDepName;

    public GroceryId() {
    }

    public GroceryId(BigDecimal groId, String groDepName) {
        this.groId = groId;
        this.groDepName = groDepName;
    }

    @Column(name = "GRO_ID", nullable = false, precision = 22, scale = 0)
    public BigDecimal getGroId() {
        return this.groId;
    }

    public void setGroId(BigDecimal groId) {
        this.groId = groId;
    }

    @Column(name = "GRO_DEP_NAME", nullable = false, length = 32)
    public String getGroDepName() {
        return this.groDepName;
    }

    public void setGroDepName(String groDepName) {
        this.groDepName = groDepName;
    }

    public boolean equals(Object other) {
        if ((this == other))
            return true;
        if ((other == null))
            return false;
        if (!(other instanceof GroceryId))
            return false;
        GroceryId castOther = (GroceryId) other;

        return ((this.getGroId() == castOther.getGroId()) || (this.getGroId() != null && castOther.getGroId() != null
                && this.getGroId().equals(castOther.getGroId())))
                && ((this.getGroDepName() == castOther.getGroDepName())
                        || (this.getGroDepName() != null && castOther.getGroDepName() != null
                                && this.getGroDepName().equals(castOther.getGroDepName())));
    }

    public int hashCode() {
        int result = 17;

        result = 37 * result + (getGroId() == null ? 0 : this.getGroId().hashCode());
        result = 37 * result + (getGroDepName() == null ? 0 : this.getGroDepName().hashCode());
        return result;
    }

}

我已遵循此example。 请帮帮我,我无法找出问题所在。

以下是我从数据库中获取数据的服务,该数据库具有GroceryRepository扩展了CrudRepository:

@Service
public class GroceryService {

    @Autowired
    GroceryRepository groceryRepository;

    public List<Grocery> getAllGrocery()
    {
        List<Grocery> groceries = new ArrayList<>();
        groceryRepository.findAll().forEach(groceries::add);
        return groceries;
    }

    public void addGrocery(Grocery grocery)
    {
        groceryRepository.save(grocery);
    }
}

2 个答案:

答案 0 :(得分:0)

在Grocery.java中缺少@EmbeddedId注释。如下更新代码。

 @EmbeddedId
 private GroceryId id;

答案 1 :(得分:0)

只需使用@EmbeddedId。使用EmbeddedId批注时,必须只有一个EmbeddedId批注,而没有Id批注。

@EmbeddedId
 private GroceryId id;