Problems when remote calling SFSB wrapped EntityHome
showmanlkz.showmanlkz.gmail.com Sep 19, 2009 9:28 AMHi, I have few problems when using client to access the EntityHome that is wrapped within a SFSB. Here is the code
@Remote public interface UserManager { public void setId(Object Id); public Object getId(); public void setInstance(User instance); public User getInstance(); public boolean isManaged(); }
@Name("userHome") @Stateful public class UserHome extends EntityHome<User> implements UserManager { private static final long serialVersionUID = 415824630373139993L; @RequestParameter Long userId; @Override public Object getId() { if (userId == null) { return super.getId(); } else { return userId; } } @Override @Begin public void create() { super.create(); } /* This solves my 1st problem */ @Override public User getInstance() { return super.getInstance(); } @Remove @Destroy public void destory() {} }
@Entity @Table(name="[User]") public class User implements Serializable { private static final long serialVersionUID = 5412666781502570678L; @Id @GeneratedValue @Column(name="Id") private String id; @Column(name="BirthDate") private Date birthDate; @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="patient") private List<MeasurementData> measurementData = new ArrayList<MeasurementData>(); ........... }
My first problem is, after my client obtains the EntityHome by using JNDI look up, and try to get a user by using getInstance(), it throws ClassCastException java.lang.String. I think User's ID is where causes the exception. I somehow fix this by adding an Override getInstance() method in EntityHome. Now it works, but it will be nice if someone can provide an explanation. :)
My second problem is LazyInitializationException when try to fetching a list of measurement data that belongs to a user. I have read it in other posts and kind of know the cause, but not sure the correct way to fix it. Any help would be appreciated.
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.unsw.bsl.dss.user.User.measurementData, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:119) at org.hibernate.collection.PersistentBag.size(PersistentBag.java:248) at DomainTest.testReadUser(DomainTest.java:77) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:592) at junit.framework.TestCase.runTest(TestCase.java:164) at junit.framework.TestCase.runBare(TestCase.java:130) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)