1

JAX-RS と JPA について自分自身を教育する過程で。Glassfish サーバー 3.1.2 でプロジェクトを実行したときに、次の例外に出くわしました。ただし、JPA 実装はスタンドアロン アプリケーションとして正常に動作しています。

内部例外: org.postgresql.util.PSQLException: エラー: リレーション "producttype_productitem" が存在しません 位置: 65 エラー コード: 0 呼び出し: SELECT t1."productCode", t1.COST, t1.ITEMCODE, t1.ITEMNAME FROM ProductType_ProductItem t0 , "ProductItem" t1 WHERE ((t0.ProductType_productCode = ?) AND (t1."productCode" = t0.item_productCode)) bind => [1 パラメータ バインド]


私はEclipse Kepler IDE、Glassfish Server 3.1.2、JAX-RS用のJersey、JPA実装用のEclipselink 2.3、およびデータベース用の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;
4

2 に答える 2

1

ProductType クラスを変更してこれを修正しました

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

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

これにより、永続化プロバイダーであるforeignKeyを指定するには、JoinColumnを一方向の関係で使用する必要があることがわかりました(ここでは、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;

これを分析しようとしています。何か見つけたら投稿します。

ありがとう。

于 2013-11-12T11:33:58.917 に答える