我目前正試圖通過調用一個門面,這是自動生成的Netbeans的(會話Bean的實體類嚮導)的業務方法來傳遞從獨立客戶端向服務器企業應用程序的@Entity對象。Java&EJB3:如何正確地將@Entity對象從客戶端傳遞到服務器?
的@Entity類包含兩個字符串字段。但是,當對象到達服務器端時,所有字段都是「空」,儘管我在調用遠程方法的正面之前正確地在客戶端正確初始化了實體對象,然後應該將該對象存儲到我的MySQL數據庫中事實上它存儲,但與那些空引用)。沒有錯誤,一切正常,除了從客戶端到服務器傳遞@Entity對象。
相反的方式工作。我在表中手動添加了一行,然後調用Facade的遠程業務方法find(Object id),以便將db的輸入返回給客戶端。編組在這個方向上工作得很好。
基本上它由3個項目(附着在底部的類)的:
- 企業應用內/ EJB:保持門面和JDBC連接到MySQL
- 的Java類庫的實現:保持遠程業務方法和實體bean的接口(這一個是共享的(引用)由服務器端和客戶端)
- Java應用程序:構建的InitialContext並執行JNDI查找來獲得遠程業務方法的引用的外立面
我現在面對這個問題了兩天,沒有人有任何線索如何解決。互聯網搜索完全失敗。因此,任何想法高度讚賞 - 非常感謝提前!
任何想法高度讚賞。
A1)EJB /企業級應用:這裏是抽象的外觀類(由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 /企業級應用程序:門面執行(由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)共享CLA SS-圖書館:遠程門面接口:
@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();
}
}
你可以發佈實體 – barsju 2012-04-17 12:34:33
嗨barsju - 我發佈的實體代碼的初始消息作爲編輯...我真的很沮喪。我花了整整一週的時間來正確設置GlassFish服務器,以便與ACC進行遠程通信,現在又出現了這個問題2天:-(我無法在Internet上找到任何信息.. – salocinx 2012-04-17 14:08:02
很奇怪,你能序列化一個對象從服務器到客戶端?如果刪除serialVersionUID?如何刪除serialVersionUID? – James 2012-04-17 15:19:20