2

我目前正在尝试通过调用由 Netbeans 自动生成的外观上的业务方法(实体类的会话 Bean 向导)将 @Entity 对象从独立客户端传递到服务器企业应用程序。

@Entity 类由两个字符串字段组成。但是当对象到达服务器端时,所有字段都是“null”,尽管我在调用门面的远程方法之前在客户端正确初始化了实体对象,而门面的远程方法又应该将对象存储到我的 MySQL 数据库中(在事实上它被存储了,但是有那些空引用)。没有错误,除了将 @Entity 对象从客户端传递到服务器之外,一切正常。

反之亦然。我在表中手动添加了一行,并调用了外观的远程业务方法 find(Object id),以便将 db 的条目返回给客户端。编组在这个方向上工作得很好。

基本上它由 3 个项目组成(附在底部的类):

  1. Enterprise-App/EJB:持有facade的实现和到MySQL的JDBC连接
  2. Java Class-Library:保存远程业务方法和实体bean的接口(这个由服务器端和客户端共享(引用))
  3. Java Application:构建 InitialContext 并执行 JNDI 查找以获取对外观的远程业务方法的引用

我现在面对这个问题两天了,没有人知道如何解决。互联网搜索完全失败。因此,任何想法都受到高度赞赏 - 提前非常感谢!

任何想法都受到高度赞赏。

A1) EJB/Enterprise-App:这是抽象外观类(由 Netbeans 自动生成):

public abstract class AbstractFacade<T> {
    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }
}

A2) EJB/Enterprise-App:外观实现(由 Netbeans 自动生成):

@Stateless
public class UserFacade extends AbstractFacade<User> implements UserFacadeRemote {

    @PersistenceContext(unitName = "LawSuiteEE-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }

}

B1)共享类库:这是实体类:

@Entity
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String username;
    private String password;

    public User() { }

    public User(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

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

    public Integer getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

B2) Shared Class-Library:远程门面接口:

@Remote
public interface UserFacadeRemote {
    void create(User user);
    void edit(User user);
    void remove(User user);
    User find(Object id);
}

C) 客户端 Java 应用程序:

public class LawSuiteSE {

    private static UserFacadeRemote ctrlUser;

    public LawSuiteSE() {
        try {
            Properties props = new Properties();
            props.put("org.omg.CORBA.ORBInitialHost", "192.168.1.6");
            props.put("org.omg.CORBA.ORBInitialPort", "3700");
            InitialContext ctx = new InitialContext(props);
            ctrlBenutzer = (BenutzerFacadeRemote)ctx.lookup("java:global/LawSuiteEE/LawSuiteEE-ejb/UserFacade!control.UserFacadeRemote");
            User user1 = new User();
            user1.setUsername("testusr");
            user1.setPassword("testpwd");
            ctrlUser.create(user1);
            User user2 = ctrlUser.find(1L);
            System.out.println("username: "+user2.getUsername());
            System.out.println("password: "+user2.getPassword());
        } catch (NamingException ex) {
            System.err.println(ex.getMessage());
        }
    }

    public static void main(String[] args) {
        LawSuiteSE lsse = new LawSuiteSE();
    }

}
4

1 回答 1

6

My guess is it is related to the serialVersionUID you have defined, or the CORBA serializer you are using. Not sure if you can try regular RMI. Try also serializing/deserialing the object just on the client, does it work? Also try just on the server.

My guess is the class versions in your client and server are somehow different. This is most likely related to how JPA weaves the classes on the server. Try disabling weaving in your persistence.xml, does this resolve the issue?

If it does, then try using static weaving of the jar, so both the client and the server share the same jar.

What version of Glassfish and JDK are you using? We have similar tests that call remote session beans from clients in Glassfish, and they do not have any issues.

于 2012-04-18T12:53:06.247 回答