How can I override the bean method of an EJB 3 session with a common argument - if at all?

Suppose you have the following EJB 3 interfaces / classes:

public interface Repository<E> { public void delete(E entity); } public abstract class AbstractRepository<E> implements Repository<E> { public void delete(E entity){ //... } } public interface FooRepository<Foo> { //other methods } @Local(FooRepository.class) @Stateless public class FooRepositoryImpl extends AbstractRepository<Foo> implements FooRepository { @Override public void delete(Foo entity){ //do something before deleting the entity super.delete(entity); } //other methods } 

And then another bean that accesses the FooRepository bean:

 //... @EJB private FooRepository fooRepository; public void someMethod(Foo foo) { fooRepository.delete(foo); } //... 

However, the overriding method is never executed when the FooRepository bean removal method is FooRepository . Instead, only the implementation of the delete method, which is defined in AbstractRepository , is performed.

What am I doing wrong or is it just a limitation of Java / EJB 3 that generics and inheritance don't work well together?

+4
source share
2 answers

I tried this with pojo and it seems to work. I had to change the code a bit. I think your interfaces were a bit off, but I'm not sure.

I assumed that "Foo" was a specific type, but if not, I can do some more tests for you.

I just wrote a basic method to verify this. Hope this helps!

 public static void main(String[] args){ FooRepository fooRepository = new FooRepositoryImpl(); fooRepository.delete(new Foo("Bar")); } public class Foo { private String value; public Foo(String inValue){ super(); value = inValue; } public String toString(){ return value; } } public interface Repository<E> { public void delete(E entity); } public interface FooRepository extends Repository<Foo> { //other methods } public class AbstractRespository<E> implements Repository<E> { public void delete(E entity){ System.out.println("Delete-" + entity.toString()); } } public class FooRepositoryImpl extends AbstractRespository<Foo> implements FooRepository { @Override public void delete(Foo entity){ //do something before deleting the entity System.out.println("something before"); super.delete(entity); } } 
+2
source

Can you write unit test for your FooRepository class just using it as a POJO. If this works as expected, I am not familiar with any of the reasons why it will work differently inside the container.

I suspect something else is going on, and it will probably be easier to debug if you test it like a POJO.

+1
source

Source: https://habr.com/ru/post/1276359/


All Articles