EJB @Local and @Stateless together
Asked Answered
M

2

8

I got my work rejected at uni because of using @Local and @Stateless on one EJB. It is a helper bean to validate/fix passed objects. I think it is completely legal to annotate my ejb's with both local and stateless. Can someone explain to me why can this be an issue?

Monopode answered 26/1, 2015 at 13:45 Comment(2)
what do you mean by "it is a helper bean to validate/fix passed objects" ?Slalom
you pass to validate method a student object and it validates that no fields are set to null, and if they are it assigns values to them via reflection. Example - name is null - it would be set to String "" .Monopode
A
9

From javax.ejb.Local javadoc:

When used on the bean class, declares the local business interface(s) for a session bean. ... Use of the Local annotation is only required when the bean class does not implement only a single interface other than any of the following: java.io.Serializable; java.io.Externalizable; any of the interfaces defined in javax.ejb.

So when you use this annotation on the bean class, you need to pass local interface' class as parameter to this annotation. If your bean exposes a no-interface view you should annotate it with @LocalBean. From javax.ejb.LocalBean javadoc:

Designates that a session bean exposes a no-interface view. This annotation is required if a session bean exposes any other client views (local, remote, no-interface, 2.x Remote Home, 2.x Local Home, Web Service) in addition to the no-interface view or its implements clause contains an interface other than java.io.Serializable; java.io.Externalizable; or any of the interfaces defined by the javax.ejb package.

So, if your bean doesn't implement any interfaces, you may just annotate it with @Stateless:

@Stateless
public class MyEJB {
    public void localMethod() {}
}
Absa answered 26/1, 2015 at 13:54 Comment(0)
S
2

If your EJB methods are made to be used by a local client (i.e. if ejb client is in same environment where ejb session bean is to be deployed. ) you have to extract an interface annotated by @Local in order to expose your business methods.

Example :

@Stateless
public class MyEJB implements MyEJBLocal{
    public void myBusinessMethod(){
        //Implementation
    }
}

@Local
public interface MyEJBLocal{
    public void myBusinessMethod();
}
Slalom answered 26/1, 2015 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.