Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JPA行为_Jpa_Glassfish_Eclipselink - Fatal编程技术网

JPA行为

JPA行为,jpa,glassfish,eclipselink,Jpa,Glassfish,Eclipselink,我很难理解JPA的行为。也许有人能给我一个暗示。 情况: 产品实体: @Entity public class Product implements Serializable { ... @OneToMany(mappedBy="product", fetch=FetchType.EAGER) private List<ProductResource> productResources = new ArrayList<ProductResource>(); .... pu

我很难理解JPA的行为。也许有人能给我一个暗示。 情况:

产品实体:

@Entity
public class Product implements Serializable {
...
@OneToMany(mappedBy="product", fetch=FetchType.EAGER)
private List<ProductResource> productResources = new ArrayList<ProductResource>();
....
public List<ProductResource> getProductResources() {
    return productResources;
}
public boolean equals(Object obj) {
    if (obj == this) return true;
    if (obj == null) return false;
    if (!(obj instanceof Product)) return false;
    Product p = (Product) obj;
    return p.productId == productId;
}
}
这是在glassfish上运行的会话Bean

@Stateless(mappedName="PersistenceManager")
public class PersistenceManagerBean implements PersistenceManager {

@PersistenceContext(unitName = "local_mysql")
    private EntityManager em;

public Object create(Object entity) {
    em.persist(entity);
    return entity;
}
public void delete(Object entity) {
    em.remove(em.merge(entity));
}
public Object retrieve(Class entityClass, Long id) {
    Object entity = em.find(entityClass, id);
    return entity;
}
public void update(Object entity) {
    em.merge(entity);
}
}
我从java客户端调用会话Bean:

public class Start {
public static void main(String[] args) throws NamingException {

    PersistenceManager pm = (PersistenceManager) new InitialContext().lookup("java:global/BackITServer/PersistenceManagerBean");

    ProductResource pr = new ProductResource();
    Product p = new Product();
    Resource r = new Resource();

    pr.setProduct(p);
    pr.setResource(r);

    ProductResource pr_stored = (ProductResource) pm.create(pr);

    pm.delete(pr_stored);
    Product p_ret = (Product) pm.retrieve(Product.class, pr_stored.getProduct().getProductId());
// prints out true ????????????????????????????????????
    System.out.println(p_ret.getProductResources().contains(pr_stored));
}
}
所以我的问题来了。为什么ProductResource实体仍在上面的ProductResources代码列表中。数据库中的productResource元组在删除之后消失了,我重新检索产品实体。如果我理解正确的话,客户端的每个方法调用都发生在新的持久性上下文中,但是这里我显然得到了未刷新的产品对象

谢谢你的帮助

谢谢


Marcel

当我在retrieve方法中使用refresh方法时,它可以工作

public Object retrieve(Class entityClass, Long id) {
    Object entity = em.find(entityClass, id);
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    em.refresh(entity);
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    return entity;
}
所以我假设问题在于eclipselink的缓存。如果我对ProductResource对象使用create方法,那么产品和资源对象也存储在cascade=CascadeType.Persist中。当我再次加载产品对象时,它会从缓存中出来,因此不是实际的。因此,我还应该删除产品和资源对象列表中的ProductResource对象,并进行更新。正当在这里调用refresh实际上没有意义,因为它绕过了缓存

谢谢
Marcel

能否显示在服务器端执行的sql请求?
public class Start {
public static void main(String[] args) throws NamingException {

    PersistenceManager pm = (PersistenceManager) new InitialContext().lookup("java:global/BackITServer/PersistenceManagerBean");

    ProductResource pr = new ProductResource();
    Product p = new Product();
    Resource r = new Resource();

    pr.setProduct(p);
    pr.setResource(r);

    ProductResource pr_stored = (ProductResource) pm.create(pr);

    pm.delete(pr_stored);
    Product p_ret = (Product) pm.retrieve(Product.class, pr_stored.getProduct().getProductId());
// prints out true ????????????????????????????????????
    System.out.println(p_ret.getProductResources().contains(pr_stored));
}
}
public Object retrieve(Class entityClass, Long id) {
    Object entity = em.find(entityClass, id);
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    em.refresh(entity);
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    return entity;
}