EJB 3 + JPA2 = 具有空字段的反序列化对象
我在一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题可能来自这个问题 http://java.net/jira/browse/GLASSFISH-16164
我在 Glassfish 3.1.1 中遇到了同样的问题,其中有单独的
EAR 和 WAR
。persistence.xml 中的这个属性解决了我的问题:
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: