EJB 3 + JPA2 = 具有空字段的反序列化对象

发布于 2024-12-08 09:42:44 字数 1562 浏览 0 评论 0原文

我在一个 jar 中有一个 JPA 带注释的对象和一个 EJB 远程接口,并且 EJB 无状态实现以及注入的实体管理器部署在一个 war 文件中。我的客户创建该对象的一个​​新实例并通过网络发送它。

然而,当它在服务器端反序列化时,该对象的每个字段都为 null,并且还包含其他字段,例如 _persistence_primaryKey。看来服务器实现认为我的反序列化对象已经被管理......但它不是......我如何通过线路传递对象以便正确反序列化?如果实体管理器被注释掉,对象反序列化就可以了。

请参阅下面的代码示例:

Chicken.java(JPA 带注释的 POJO)

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

    /**
     *serial version id 
     */
     private static final long serialVersionUID = 1L;

     @Id
     @Column(name="ID")
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;

     @Column(name="NAME")
     private String name;

     public Chicken(){
     }

     public void setName(String name){
        this.name = name;
     }

     public String getName(){
         return name;
     }
}

ChickenCreator.java(带 JPA 注入的无状态 EJB)

@Stateless
public class ChickenCreator implements ChickenCreatorRemote{

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

  public ChickenCreator(){
  }

  public createChicken(Chicken chicken){
     em.persist(chicken);
  }

} 

ChickenClient.java(带有注入的 JSF UI)远程EJB接口)

@ManagedBean
@SessionScoped
public ChickenClient(){

    @EJB private ChickenCreatorRemote ccr;

    public void makeChicken(){
       Chicken chicken = new Chicken();
       chicken.setName("Bob");
       ccr.createChicken(chicken);
    }   
}

I have a JPA Annotated Object and a EJB remote interface in one jar and the EJB Stateless implementation with the Entity Manager injected deployed in a war file. My client creates a new instance of the object and sends it across the wire.

However when it is deserialized on the server side, the object has null for every field, plus is contains other fields like _persistence_primaryKey. It seems that the server implementation thinks that my deserialized object is already managed...but its not...how do I pass the object across the wire so its deserialized properly? If the entity manager is commented out, the object deserializes just fine.

See below for code example:

Chicken.java (JPA Annotated POJO)

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

    /**
     *serial version id 
     */
     private static final long serialVersionUID = 1L;

     @Id
     @Column(name="ID")
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;

     @Column(name="NAME")
     private String name;

     public Chicken(){
     }

     public void setName(String name){
        this.name = name;
     }

     public String getName(){
         return name;
     }
}

ChickenCreator.java (Stateless EJB w/ JPA injection)

@Stateless
public class ChickenCreator implements ChickenCreatorRemote{

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

  public ChickenCreator(){
  }

  public createChicken(Chicken chicken){
     em.persist(chicken);
  }

} 

ChickenClient.java (JSF UI with injected Remote EJB Interface)

@ManagedBean
@SessionScoped
public ChickenClient(){

    @EJB private ChickenCreatorRemote ccr;

    public void makeChicken(){
       Chicken chicken = new Chicken();
       chicken.setName("Bob");
       ccr.createChicken(chicken);
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2024-12-15 09:42:44

您的问题可能来自这个问题 http://java.net/jira/browse/GLASSFISH-16164

我在 Glassfish 3.1.1 中遇到了同样的问题,其中有单独的 EAR 和 WAR
persistence.xml 中的这个属性解决了我的问题:

<property name="eclipselink.weaving" value="false"/>

Your issue may be comes from this problem http://java.net/jira/browse/GLASSFISH-16164

I had same issue in Glassfish 3.1.1 with separate EAR and WAR.
This property in persistence.xml solved my issue:

<property name="eclipselink.weaving" value="false"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文