我寫了@AroundInvoke
方法調用方法Stateless
Restful API,調用方法包含EntityManager
用於創建命名查詢行裏面的表,然後使用Entity Bean
類, 返回它的問題是:我的代碼工作正常,當我第一次從Netbeans
開始Glassfish
,但編輯項目內的任何東西時,和Netbeans
自動部署項目到Glassfish
服務器,當我嘗試訪問已調用方法的Stateless Restfult API Again
時,那麼我的代碼再次無法正常工作,它會拋出此異常:持久性實體bean類重新部署後調用方法內部錯誤:beans.Users不能轉換爲beans.Users
beans.Users不能轉換到beans.Users
,直到我重新啓動Glassfish
服務器再次它不工作了。
我調用類:
public class AuthInterceptor{
@AroundInvoke
private Object initialize(InvocationContext ctx) throws Exception{
EntityManager EM = Persistence.createEntityManagerFactory("APIPU").createEntityManager();
Query query = EM.createNamedQuery("Users.fetchbyToken");
query.setParameter("token", token);
Users User = (Users) query.getSingleResult();
EM.refresh(User);
EM.detach(User);
// ..... continue other codes
}
}
我Statless寧靜的API:
@Interceptors(AuthInterceptor.class)
@Stateless
@Path("User")
public class UserAPI{
@GET
@Produces("text/plain")
public String test(){
return "Hi..";
}
}
我Users.class實體bean示例:
@Entity
@Table(name = "USERS", schema = "APP")
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.fetchbyToken", query = "SELECT u FROM Users u WHERE u.token = :token")
})
public class Users implements Serializable {
// ... continue code..
}
任何解釋嗎?