java - Why does my EJB interface need to extend serializable? -
i'm trying figure out going on , can't reason through why.
i've got extremely simple ejb 3 implementation i've been playing (deployed standalone openejb 3.1.2 container).
if setup interface/implementation looking this...
userservice (interface)
public interface userservice { public string dostuff(string myparam); }
userservicebean (implementation)
@stateless @remote({userservice.class}) public class userservicebean implements userservice { public string dostuff(string myparam) { return "did stuff!"; } }
...this works great (i.e. can deploy openejb standalone container , run quick test involving jndi lookup , service call returns expected "did stuff!" value).
however, once introduce pojo created method signature, this:
userservice (interface)
public interface userservice { public user lookupuser(string myparam); }
userservicebean (implementation)
@stateless @remote({userservice.class}) public class userservicebean implements userservice { public user lookupuser(string username) { return new user(); } }
...where pojo looks like:
public class user implements serializable { private string username; private string firstname; private string lastname; private string employeeclasscode; private date termdate; // getters & setters follow }
i fatal reported openejb container:
2014-06-26 12:41:32,182 - fatal - couldn't write ejbresponse output stream java.io.notserializableexception: userservicebean
just error logging lead believe, fix make userservice interface extend serializable - once this, well...but struggling understand why ejb bean needs serializable after i've added pojo method signature. can explain?
by way, question clear, i'm 100% fine answer general question "why things need serialized when working ejbs?" specific point of confusion why bean itself needs serializable because serializable object being used in method signature.
edit: original answer kind of lazy; adding more clarification.
wow, beyond bizarre - wasn't entirely complete in user.java listing. doing was:
return new user() { { setfirstname("fernando"); setlastname("alonso"); } };
you wouldn't think problem, when changed more orthodox:
user user = new user(); user.setfirstname("fernando"); user.setlastname("alonso"); return user;
it works charm! issue that, original code, creating anonymous subclass of user pojo. jvm, not same thing instance of user class @ all, rather anonymous class lives within userservicebean itself. illustrate:
user anonymoususer = new user() { { setfirstname("fernando"); setlastname("alonso"); } };
inspecting anonymoususer
reference variable reveals pointing @ instance of userservicebean$1
(with "$1" part being java's way of identifying anonymous inner class)
doing more traditional:
user user = new user(); user.setfirstname("fernando"); user.setlastname("alonso");
and inspecting user
reference variable reveals to instance of user
class (as expect).
so, information in hand, original error indicating userservicebean
needed serializable makes perfect sense!
Comments
Post a Comment