/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

内部异常:org.postgresql.util.PSQLException:错误:关系“ProductType_Productitem”不存在

内部异常:org.postgresql.util.PSQLException:错误:关系“ProductType_Productitem”不存在(内部异常:org.postgresql.util.PSQLException:Error:Relation关系“ProductType_Productitem”不存在)

 1032  IT屋

在对JAX-RS和JPA进行自我教育的过程中.在Glassfish服务器3.1.2上运行项目时,我偶然发现了以下异常.但是,JPA实施可以作为独立应用程序正常运行.

内部异常:org.postgresql.util.PSQLException:错误:关系“ProductType_Productitem”不存在位置:65错误代码:0调用:选择T1,“ProductCode”,t1.COST,t1.ITEMCODE,t1.ITEMNAME(来自ProductType_ProductItem 0),“ProductItem”T1在哪里((t0.ProductType_ProductCode=?)和(T1.“ProductCode”=t0.Item_ProductCode)BIND=>[1个参数绑定]


我正在使用EclipseKeplerIDE、GlassFishServer3.1.2、泽西(用于JAX-RS)、Eclipselink 2.3(用于JPA实现)和PostgresSQL(用于数据库))。


CREATE TABLE "ProductType"
(
  "productCode" integer NOT NULL,
  "productName" character(50) NOT NULL,
  "productType" character(50),
  rate numeric(18,6),
  count integer,
  CONSTRAINT "PrimaryKey" PRIMARY KEY ("productCode")
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "ProductType" OWNER TO postgres;
GRANT ALL ON TABLE "ProductType" TO postgres;
GRANT ALL ON TABLE "ProductType" TO public;




 CREATE TABLE "ProductItem"
(
  "productCode" integer NOT NULL,
  "itemCode" character(20),
  "itemName" character(20),
  "cost" numeric(10,6)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "ProductItem" OWNER TO postgres;
GRANT ALL ON TABLE "ProductItem" TO postgres;
GRANT ALL ON TABLE "ProductItem" TO public;

实体

package com.jaxrs.crud;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlType;

@Entity
@Table(name="\"ProductType\"")
@XmlType(name = "product")  
public class ProductType implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Column(name="\"productType\"")
    private String productType;

    @Column(name="\"productName\"")
    private String productName;
    @Id
    @Column(name="\"productCode\"")
    private String productCode;

    private Float rate;
    private int count;
    @OneToMany(cascade=CascadeType.ALL)
    private List<ProductItem> item;

    private ProductType(String productID, String productType,
            String productName,float rate) {
        this.productCode = productID;
        this.productType = productType;
        this.productName = productName;
        this.rate = rate;

        ProductItem item1 = new ProductItem();
        item1.setCost(new Random().nextFloat());
        item1.setItemCode("ITC"+new Random().nextInt());
        item1.setItemName("ITN"+new Random().nextInt());
        if(item == null )
        {
            item = new ArrayList<ProductItem>();
        }
        item.add(item1);

    }
    public ProductType()
    {

    }

    public ProductType(int count) {

        this("PID"+new Random().nextInt(),"PTE"+new Random().nextInt(),"PNE"+new Random().nextInt(),new Random().nextFloat());
        this.count = count;
    }

    /**
     * @return the productType
     */
    @XmlElement(name = "productCode")
    public String getProductType() {
        return productType;
    }

    /**
     * @param productType
     *            the productType to set
     */
    public void setProductType(String productType) {
        this.productType = productType;
    }

    /**
     * @return the productName
     */
    @XmlElement(name = "productName")
    public String getProductName() {
        return productName;
    }

    /**
     * @param productName
     *            the productName to set
     */
    public void setProductName(String productName) {
        this.productName = productName;
    }

    /**
     * @return the productID
     */
    @XmlElement(name = "id")
    public String getProductID() {
        return productCode;
    }

    /**
     * @param productID
     *            the productID to set
     */
    public void setProductID(String productID) {
        this.productCode = productID;
    }

    /**
     * @return the rate
     */
    @XmlElement(name = "rate")
    public Float getRate() {
        return rate;
    }

    /**
     * @param rate
     *            the rate to set
     */
    public void setRate(Float rate) {
        this.rate = rate;
    }

    /**
     * @return the count
     */
    @XmlElement(name = "cnt")
    public int getCount() {
        return count;
    }

    /**
     * @param count
     *            the count to set
     */
    public void setCount(int count) {
        this.count = count;
    }

    /**
     * @return the itemList
     */
    @XmlElement(name = "item")
    @XmlElementWrapper(name = "items") 
    public List<ProductItem> getItemList() {
        return item;
    }

    /**
     * @param itemList
     *            the itemList to set
     */
    public void setItemList(List<ProductItem> itemList) {
        this.item = itemList;
    }

      @Override
        public String toString() {
            return new StringBuilder().append(this.productCode).append("  ").append(this.productName).append("  ").append(this.productType).append("  ").append(this.rate).append("  ").append(this.count).toString();

    }
}

ProductItem实体

package com.jaxrs.crud;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

@Entity
@Table(name="\"ProductItem\"")
@XmlType(propOrder = { "productCode","itemCode", "itemName", "cost" } )  
@XmlAccessorType(XmlAccessType.FIELD)  
public class ProductItem {
    @Id
    @Column(name="\"productCode\"")
    private String productCode;
    private String itemCode;
    private String itemName;
    private Float cost;

    /**
     * @return the itemCode
     */
    public String getItemCode() {
        return itemCode;
    }

    /**
     * @param itemCode
     *            the itemCode to set
     */
    public void setItemCode(String itemCode) {
        this.itemCode = itemCode;
    }

    /**
     * @return the itemName
     */
    public String getItemName() {
        return itemName;
    }

    /**
     * @param itemName
     *            the itemName to set
     */
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    /**
     * @return the cost
     */
    public Float getCost() {
        return cost;
    }

    /**
     * @param cost
     *            the cost to set
     */
    public void setCost(Float cost) {
        this.cost = cost;
    }

    /**
     * @return the productCode
     */
    public String getProductCode() {
        return productCode;
    }

    /**
     * @param productCode the productCode to set
     */
    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }



}

JPA调用

EntityManagerFactory entityManagerFactory = Persistence
            .createEntityManagerFactory("jaxrs");
    EntityManager em = entityManagerFactory
            .createEntityManager();

    List<ProductType> results = null;
    try {
        Query query = em.createQuery("SELECT p FROM ProductType p",
                ProductType.class);
        results = query.getResultList();
        for (ProductType pt : results)
            System.out.println(pt);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        em.close();
        entityManagerFactory.close();
    }

    return results;
解决方案

我通过更改ProductType类来解决此问题

@OneToMany(cascade=CascadeType.ALL)
    private List<ProductItem> item;

@OneToMany(orphanRemoval = true)
    @JoinColumn(name = "\"productCode\"")
    private List<ProductItem> item;

通过这种方式,我理解应该以单向关系使用JoinColumn来指定持久性提供程序的ForeignKey(此处ProductCode在ProductItem中为FK)

但是当我单独运行它时,前者仍然可以工作

EntityManagerFactory entityManagerFactory = Persistence
            .createEntityManagerFactory("jaxrs");
    EntityManager em = entityManagerFactory
            .createEntityManager();

    List<ProductType> results = null;
    try {
        Query query = em.createQuery("SELECT p FROM ProductType p",
                ProductType.class);
        results = query.getResultList();
        for (ProductType pt : results)
            System.out.println(pt);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        em.close();
        entityManagerFactory.close();
    }

    return results;

尝试对此进行分析.如果我找到任何东西,将发布.

谢谢.

在对自己进行JAX-RS和JPA教育的过程中。在Glassfish服务器3.1.2上运行该项目时,我偶然发现了以下异常。但是,JPA实现作为一个独立的应用程序运行得很好。

内部异常:org.postgresql.util.PSQLException:错误:关系“ProductType_Productitem”不存在位置:65错误代码:0调用:选择T1。“产品代码”,t1.COST,t1.ITEMCODE,t1.ITEMNAME从ProductType_ProductItem 0,“ProductItem”T1((t0.ProductType_ProductCode=?)和(T1.“ProductCode”=t0.Item_ProductCode)BIND=>[1参数绑定]


我使用EclipseKeplerIDE、GlassFish Server 3.1.2、泽西用于JAX-RS、Eclipselink 2.3用于JPA实现,PostgresSQL用于数据库。


CREATE TABLE "ProductType"
(
  "productCode" integer NOT NULL,
  "productName" character(50) NOT NULL,
  "productType" character(50),
  rate numeric(18,6),
  count integer,
  CONSTRAINT "PrimaryKey" PRIMARY KEY ("productCode")
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "ProductType" OWNER TO postgres;
GRANT ALL ON TABLE "ProductType" TO postgres;
GRANT ALL ON TABLE "ProductType" TO public;




 CREATE TABLE "ProductItem"
(
  "productCode" integer NOT NULL,
  "itemCode" character(20),
  "itemName" character(20),
  "cost" numeric(10,6)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "ProductItem" OWNER TO postgres;
GRANT ALL ON TABLE "ProductItem" TO postgres;
GRANT ALL ON TABLE "ProductItem" TO public;

实体

package com.jaxrs.crud;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlType;

@Entity
@Table(name="\"ProductType\"")
@XmlType(name = "product")  
public class ProductType implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Column(name="\"productType\"")
    private String productType;

    @Column(name="\"productName\"")
    private String productName;
    @Id
    @Column(name="\"productCode\"")
    private String productCode;

    private Float rate;
    private int count;
    @OneToMany(cascade=CascadeType.ALL)
    private List<ProductItem> item;

    private ProductType(String productID, String productType,
            String productName,float rate) {
        this.productCode = productID;
        this.productType = productType;
        this.productName = productName;
        this.rate = rate;

        ProductItem item1 = new ProductItem();
        item1.setCost(new Random().nextFloat());
        item1.setItemCode("ITC"+new Random().nextInt());
        item1.setItemName("ITN"+new Random().nextInt());
        if(item == null )
        {
            item = new ArrayList<ProductItem>();
        }
        item.add(item1);

    }
    public ProductType()
    {

    }

    public ProductType(int count) {

        this("PID"+new Random().nextInt(),"PTE"+new Random().nextInt(),"PNE"+new Random().nextInt(),new Random().nextFloat());
        this.count = count;
    }

    /**
     * @return the productType
     */
    @XmlElement(name = "productCode")
    public String getProductType() {
        return productType;
    }

    /**
     * @param productType
     *            the productType to set
     */
    public void setProductType(String productType) {
        this.productType = productType;
    }

    /**
     * @return the productName
     */
    @XmlElement(name = "productName")
    public String getProductName() {
        return productName;
    }

    /**
     * @param productName
     *            the productName to set
     */
    public void setProductName(String productName) {
        this.productName = productName;
    }

    /**
     * @return the productID
     */
    @XmlElement(name = "id")
    public String getProductID() {
        return productCode;
    }

    /**
     * @param productID
     *            the productID to set
     */
    public void setProductID(String productID) {
        this.productCode = productID;
    }

    /**
     * @return the rate
     */
    @XmlElement(name = "rate")
    public Float getRate() {
        return rate;
    }

    /**
     * @param rate
     *            the rate to set
     */
    public void setRate(Float rate) {
        this.rate = rate;
    }

    /**
     * @return the count
     */
    @XmlElement(name = "cnt")
    public int getCount() {
        return count;
    }

    /**
     * @param count
     *            the count to set
     */
    public void setCount(int count) {
        this.count = count;
    }

    /**
     * @return the itemList
     */
    @XmlElement(name = "item")
    @XmlElementWrapper(name = "items") 
    public List<ProductItem> getItemList() {
        return item;
    }

    /**
     * @param itemList
     *            the itemList to set
     */
    public void setItemList(List<ProductItem> itemList) {
        this.item = itemList;
    }

      @Override
        public String toString() {
            return new StringBuilder().append(this.productCode).append("  ").append(this.productName).append("  ").append(this.productType).append("  ").append(this.rate).append("  ").append(this.count).toString();

    }
}

产品实体

package com.jaxrs.crud;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

@Entity
@Table(name="\"ProductItem\"")
@XmlType(propOrder = { "productCode","itemCode", "itemName", "cost" } )  
@XmlAccessorType(XmlAccessType.FIELD)  
public class ProductItem {
    @Id
    @Column(name="\"productCode\"")
    private String productCode;
    private String itemCode;
    private String itemName;
    private Float cost;

    /**
     * @return the itemCode
     */
    public String getItemCode() {
        return itemCode;
    }

    /**
     * @param itemCode
     *            the itemCode to set
     */
    public void setItemCode(String itemCode) {
        this.itemCode = itemCode;
    }

    /**
     * @return the itemName
     */
    public String getItemName() {
        return itemName;
    }

    /**
     * @param itemName
     *            the itemName to set
     */
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    /**
     * @return the cost
     */
    public Float getCost() {
        return cost;
    }

    /**
     * @param cost
     *            the cost to set
     */
    public void setCost(Float cost) {
        this.cost = cost;
    }

    /**
     * @return the productCode
     */
    public String getProductCode() {
        return productCode;
    }

    /**
     * @param productCode the productCode to set
     */
    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }



}

JPA调用

EntityManagerFactory entityManagerFactory = Persistence
            .createEntityManagerFactory("jaxrs");
    EntityManager em = entityManagerFactory
            .createEntityManager();

    List<ProductType> results = null;
    try {
        Query query = em.createQuery("SELECT p FROM ProductType p",
                ProductType.class);
        results = query.getResultList();
        for (ProductType pt : results)
            System.out.println(pt);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        em.close();
        entityManagerFactory.close();
    }

    return results;
解决方案

我通过更改ProductType类来修正这个问题

@OneToMany(cascade=CascadeType.ALL)
    private List<ProductItem> item;

@OneToMany(orphanRemoval = true)
    @JoinColumn(name = "\"productCode\"")
    private List<ProductItem> item;

通过这个,我了解到应该在单向关系中使用JoinColumn来指定持久化提供者ForeignKey(在这里,ProductCode是ProductItem中的FK)

但是,当我把它作为独立的运行时,前者仍然有效。

EntityManagerFactory entityManagerFactory = Persistence
            .createEntityManagerFactory("jaxrs");
    EntityManager em = entityManagerFactory
            .createEntityManager();

    List<ProductType> results = null;
    try {
        Query query = em.createQuery("SELECT p FROM ProductType p",
                ProductType.class);
        results = query.getResultList();
        for (ProductType pt : results)
            System.out.println(pt);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        em.close();
        entityManagerFactory.close();
    }

    return results;

试着分析这个。如果我发现什么。

posted @ 2021-11-02 10:15  一品堂.技术学习笔记  阅读(426)  评论(0编辑  收藏  举报