0

We use Cassandra (and the DataStax driver) to store our entities. As such we have a custom entity service that creates new instances of our entity classes when it retrieves data from Cassandra.

I also need to inject services into my entity classes using CDI. How do I do this? When I simply at the @Inject annotation, it never gets injected.

public class Customer{

    @Inject
    private Event<DeactivationEvent> events;

    private String uid;

    public void setUid(String uid){
        this.uid = uid;
    }

    public String getUid(){
        return this.uid;
    }

    public void deactivate(){

        events.fire( new DeactivationEvent() );

    }

}


public CassandraEntityService{

    public static Customer findCustomer(String uid){

        ...whatever lookup logic...
        Customer customer = new Customer();

        customer.setUid(..)
        customer.set...

        return customer;

    }

}

For reference, I'm using JBoss/Wildfly 8.1.

peterl
  • 1,881
  • 4
  • 21
  • 34

1 Answers1

0

The immediate problem in CassandraEntityService.findCustomer() is that the Customer instance is not a CDI bean, because findCustomer calls the constructor directly.

You might run into trouble using entities as CDI beans, but I think (a) you need a producer method for the Customer, and (b) CassandraEntityService itself needs to be another bean which @Injects the Customer, instead of calling the constructor directly.

However, a better solution for the more general problem (firing an event when the entity changes) would probably be an Entity Listener, in which case Customer probably won't have any need to be a CDI bean.

Community
  • 1
  • 1
seanf
  • 6,504
  • 3
  • 42
  • 52