1

我有三个相互连接的类。订单、订单详情和产品。当我在我的 JPA 项目中执行以下操作时:

 @Override
    public Order getOrderById(String orderID) {
        Order order = (Order)

          em.createQuery("select A from Order A where A.orderId = ?1")
            .setParameter(1, orderID)
            .getSingleResult();
        return order;    
    }

检索所有信息。但是,当我将其移至 ebj 项目时。我只得到订单,仅此而已。然而,所有类都包含在 persistence.xml 文件(JPA 和 ejb3)中。为什么会这样,我应该如何解决?这三个类显示在下面。我正在使用 Oracle Weblogic 10.3.3。我尝试重新启动并清除服务器,但没有奏效。

*package eshop;

import java.io.Serializable;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;*


/**
 * The persistent class for the orders database table.
 * 
 */
@Entity
@Table(name="orders")
public class Order implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="order_id")
    private String orderId;

    @Column(name="cc_expiry")
    private String ccExpiry;

    @Column(name="cc_name")
    private String ccName;

    @Column(name="cc_number")
    private String ccNumber;

    @Column(name="delivery_address")
    private String deliveryAddress;

    @Column(name="delivery_name")
    private String deliveryName;

    @Column(name="delivery_surname")
    private String deliverySurname;

    private String status;

    //bi-directional many-to-one association to OrderDetail
    @OneToMany(mappedBy="order", cascade=CascadeType.PERSIST)
    private List<OrderDetail> orderDetails = new ArrayList<OrderDetail>();

    public void addOrUpdateOrderDetail(Product product) {
        this.orderDetails.add(new OrderDetail(product));


    }







    public Order() {
    }

    public String getOrderId() {
        return this.orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getCcExpiry() {
        return this.ccExpiry;
    }

    public void setCcExpiry(String ccExpiry) {
        this.ccExpiry = ccExpiry;
    }

    public String getCcName() {
        return this.ccName;
    }

    public void setCcName(String ccName) {
        this.ccName = ccName;
    }

    public String getCcNumber() {
        return this.ccNumber;
    }

    public void setCcNumber(String ccNumber) {
        this.ccNumber = ccNumber;
    }

    public String getDeliveryAddress() {
        return this.deliveryAddress;
    }

    public void setDeliveryAddress(String deliveryAddress) {
        this.deliveryAddress = deliveryAddress;
    }

    public String getDeliveryName() {
        return this.deliveryName;
    }

    public void setDeliveryName(String deliveryName) {
        this.deliveryName = deliveryName;
    }

    public String getDeliverySurname() {
        return this.deliverySurname;
    }

    public void setDeliverySurname(String deliverySurname) {
        this.deliverySurname = deliverySurname;
    }

    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<OrderDetail> getOrderDetails() {
        return this.orderDetails;
    }

    public void setOrderDetails(List<OrderDetail> orderDetails) {
        this.orderDetails = orderDetails;
    }

}


package eshop;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;


/**
 * The persistent class for the order_details database table.
 * 
 */
@Entity
@Table(name="order_details")
public class OrderDetail implements Serializable {
    private static final long serialVersionUID = 1L;

@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

private BigDecimal price;

private int quantity;

//bi-directional many-to-one association to Order
@ManyToOne
@JoinColumn(name="order_id")
private Order order;

//bi-directional many-to-one association to Product
@ManyToOne
@JoinColumn(name="product_id")
private Product product;

@Id
private int product_id;

public OrderDetail() {
}

public OrderDetail (Integer ProductId,Product product,Integer productQuantity,BigDecimal price, Order order) {
        this.price= price;
        this.product_id = ProductId;
        this.product = product;
        this.quantity = productQuantity;
        this.order = order;
        }

    public OrderDetail(Product product1) {
    product_id = product1.getCategoryId();
    price = product1.getPrice();
    quantity  = 1;
    product = product1;

    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public BigDecimal getPrice() {
        return this.price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public int getQuantity() {
        return this.quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Order getOrder() {
        return this.order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Product getProduct() {
        return this.product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public int getProduct_id() {
        return product_id;
    }

    public void setProduct_id(int product_id) {
        this.product_id = product_id;
    }

}

package eshop;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;


/**
 * The persistent class for the products database table.
 * 
 */
@Entity
@Table(name="products")
public class Product implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="product_id")
    private int productId;

    @Column(name="category_id")
    private int categoryId;

    @Lob
    private String descr;

    private BigDecimal price;

    @Column(name="product_name")
    private String productName;

    private int quantity;

    public Product() {
    }

    public int getProductId() {
        return this.productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public int getCategoryId() {
        return this.categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

    public String getDescr() {
        return this.descr;
    }

    public void setDescr(String descr) {
        this.descr = descr;
    }

    public BigDecimal getPrice() {
        return this.price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getProductName() {
        return this.productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getQuantity() {
        return this.quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

}
4

2 回答 2

0

看起来我必须将事务类型 JTA 添加到 ejb 的 web.xml 文件中。

于 2012-11-09T06:59:33.653 回答
0

I don't understand what you mean by "moving the code from the JPA project to the EJB project). Also, can you be a bit more specific about your problem? What makes you say that the objects are not loaded? Where is the code that invokes getOrderById()?

I suspect that it is in the web tier. In that case, I suspect that you may have an issue with the fetch type. By default, it is defined as LAZY (vs EAGER).

This means that JPA will initially load only the root object. It is when you try to access the linked objects that JPA will issue other requests. That will be transparent to the developer, with an important requirement: it will work only when you are in EJB tier. If you have returned the root object to the web tier, then it is "detached" and is not controlled by JPA any more. This means that if the dependent objects have not been loaded, then you will get null values when accessing them (follow-up requests will not be sent to the DB, because JPA is out of the loop).

于 2012-11-11T14:54:19.387 回答