无法在onetomany映射JPA中映射集合

时间:2018-10-23 10:21:52

标签: java spring-boot jpa one-to-many

模型/实体1:PriceListItemTest.Java

package com.Pricing.Pricing_App.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"pricingUOMCode","lineTypeCode","primaryPricingUOMFlag","priceListId","itemId","priceListItemId"})


@Entity
@Table(name = "QP_PRICE_LIST_ITEMS")
public class PriceListItemTest implements Serializable{

//  @Id
    @Column(name = "price_list_item_id")    
    private String priceListItemId;

    @Column(name = "pricing_uom_code")  
    private String pricingUOMCode; 

    @Column(name = "line_type_code")    
    private String lineTypeCode; 

    @Column(name = "primary_pricing_uom_flag")  
    private String primaryPricingUOMFlag;

    @Id
    @Column(name = "item_id")   
    private String itemId;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "item_id", referencedColumnName = "inventory_item_id")
//    @JoinColumn(name = "inventory_item_id", referencedColumnName = "item_id")
    private List<ItemDetailTest> itemDetailsTest;

    public List<ItemDetailTest> getItemDetailsTest() {
        return itemDetailsTest;
    }

    public void setItemDetailsTest(List<ItemDetailTest> itemDetailsTest) {
        this.itemDetailsTest = itemDetailsTest;
    }

    public String getPricingUOMCode() {
        return pricingUOMCode;
    }

    public void setPricingUOMCode(String pricingUOMCode) {
        this.pricingUOMCode = pricingUOMCode;
    }

    public String getLineTypeCode() {
        return lineTypeCode;
    }

    public void setLineTypeCode(String lineTypeCode) {
        this.lineTypeCode = lineTypeCode;
    }

    public String getPrimaryPricingUOMFlag() {
        return primaryPricingUOMFlag;
    }

    public void setPrimaryPricingUOMFlag(String primaryPricingUOMFlag) {
        this.primaryPricingUOMFlag = primaryPricingUOMFlag;
    }

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public String getPriceListItemId() {
        return priceListItemId;
    }

    public void setPriceListItemId(String priceListItemId) {
        this.priceListItemId = priceListItemId;
    }   
}

模型/实体2:ItemDetailTest.java

package com.Pricing.Pricing_App.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonPropertyOrder({"itemNumber","inventoryItemId","organizationId"})


@Entity
@Table(name = "egp_system_items_b")
public class ItemDetailTest implements Serializable {
    @Id
    @Column(name = "inventory_item_id") 
    private String inventoryItemId; 

    @Column(name = "item_number")   
    private String itemNumber; 

    @Column(name = "organization_id")   
    private String organizationId;

    public String getItemNumber() {
        return itemNumber;
    }

    public void setItemNumber(String itemNumber) {
        this.itemNumber = itemNumber;
    }

    public String getInventoryItemId() {
        return inventoryItemId;
    }

    public void setInventoryItemId(String inventoryItemId) {
        this.inventoryItemId = inventoryItemId;
    }

    public String getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(String organizationId) {
        this.organizationId = organizationId;
    }
}

我无法启动本地服务器并出现以下错误:

    Exception encountered during context initialization - cancelling refresh 
    attempt: org.springframework.beans.factory.BeanCreationException: Error 
    creating bean with name 'entityManagerFactory' defined in class path 
    resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 
   : Invocation of init method failed; nested exception is 
    org.hibernate.AnnotationException: Unable to map collection 
    com.Pricing.Pricing_App.model.PriceListItemTest.itemDetailsTest

我认为映射中存在一些问题,请问有人可以帮忙吗? 如果我将关系更改为OneToOne,则工作正常。但期望是使其成为一字不漏的

2 个答案:

答案 0 :(得分:0)

一对多:

@Entity
public class PriceListItem {
    @OneToMany(mappedBy = "listItem", cascade = ALL)
    private List<ItemDetail> itemDetails;
    // getters, setters, other fields
}

多到一侧:

public class ItemDetail {
    @ManyToOne
    private PriceListItem listItem;
    // getters, setters, other fields
}

答案 1 :(得分:0)

查看您的映射。您正在尝试在元素表中映射FK。

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "item_id", referencedColumnName = "inventory_item_id")
//    @JoinColumn(name = "inventory_item_id", referencedColumnName = "item_id")
private List<ItemDetailTest> itemDetailsTest;

您错误地使用了“名称”和“ referencedColumnName”。删除当前的@JoinColumn并取消注释另一个,并删除referencedColumnName,因为所有者中只有1个PK字段,因此该规范是默认的(即,仅指定“ name” .. 。元素表中FK列的名称)。所有这些都可以在any decent JPA docs中找到。