Calling Remote EJB in EJB 3.1

2019-06-25 11:29发布

I need to call Remote stateless EJB from another Web application with in same glassfish( 3.1 final version) which return Entity Bean( JPA 2/Eclipselink).Iam getting ejb referance in web application through Dependancy Injection ( @EJB ) but entity becoming null.I google it and found that it is might be Serializable issue. Somewhere I found this

TopLink either modifies entity classes ("weaves" them) on load or substitutes collection access at runtime to be able to detect lazily accessed or modified relationships (there is no way to support lazy load without this or subclassing or using a proxy at runtime). Which brings us to a very important point: you shouldn't use reflection to access an entity, but only via its business methods.When a weaved entity is serialized on the server and deserialized on the client that doesn't have its corresponding entity weaved, the serialVersionUIDs won't match as the calculation of the value includes class fields and methods.

So do I need DTO convertion on my application ???

3条回答
叼着烟拽天下
2楼-- · 2019-06-25 11:48

What do you mean by "but entity becoming null"? Do you call an method on the remote SessionBean and get back null, or do you get back and Entity whose relationship is null?

If it is a null relationship, the it could be a LAZY issue, if your relationship is LAZY, and has not been fetched or accessed, then it will be null. You need to either fetch it, access it, or make it EAGER.

If you are getting back null, then something else is wrong.

查看更多
Bombasti
3楼-- · 2019-06-25 12:03

Maybe you're being affected by Glassfish bug 16164.

The suggested workaround is to add this property to persistence.xml:

<property name="eclipselink.weaving" value="false"/>

That solved the issue in my case.

查看更多
Animai°情兽
4楼-- · 2019-06-25 12:05

Remote EJB with EclipseLink and Glassfish doesn't work good when you try to return an entity.

Same for Hibernate, you need to remove all proxy before returning your response. With Hibernate, you need to flush and clear your persistence context before removing proxys. If not loaded, set null to attribute. You can make it work with an Java EE interceptor.

But, EclipseLink doesn't work like Hibernate. Even if your clear your persistence context, get / set on lazy attribute will try to fetch. Even outside transaction.

If you set property name="eclipselink.weaving" value="false", it will work cause EclipseLink will not change the bytecode of your POJO class but ManyToOne will always be fetched. So it can load the database in memory.

The only way I resolve this is using DTO or using Hibernate with interceptor.

EDIT : You can always override the entity serialization with the Externalization interface. Get the objet by the field to make sure the lazy fetch doesn't apply.

Openjpa seem to use the weaving method by default too. http://openjpa.apache.org/entity-enhancement.html

查看更多
登录 后发表回答