I have the following problem. Maybe the design isn’t that good, but I couldn’t figure out a better one.
I have an entity class Device.java
@Entity (name = "device")
public class Device {
public enum DeviceType {
STROM,
LICHT,
TEMPERATUR,
LUFTFEUCHTIGKEIT,
BEWEGUNG,
DIMMEN
};
public static enum HomematicAttribute {
STATE,
TEMPERATURE,
HUMIDITY,
LOWBATTERY,
MOVEMENT,
LEVEL
}
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Integer id;
// Some other fields and getter and setter
and I have this enumeration inside that class / entity
public static enum HomematicDeviceName {
LICHT_FRONT("HM_DEVICE_LICHT_FRONT", DeviceType.DIMMEN, HomematicAttribute.LEVEL),
LICHT_GANG("HM_DEVICE_LICHT_GANG", DeviceType.DIMMEN, HomematicAttribute.LEVEL),
...
// constructor and getters
@PersistenceContext
EntityManager em;
@EJB
private DatabaseServiceLocal databaseService;
public Device getDevice() {
if(em == null) System.out.println("Entitymanager ist null innerhalb der Enumeration ...");
if(databaseService == null) System.out.println("databaseService ist null innerhalb der Enumeration ...");
System.out.println("----");
return (Device) em.createNamedQuery(Device.findByIdentifier).setParameter("identifier", databaseKey).getSingleResult();
}
}
Both em and databaseService are null. The goal is to get the entity from the enumeration and the other way. I don’t want to work with the entity. Instead I want to work with the enumeration, but sometimes I need the entity. So I thought I could do something like this in order to get the device entity from my enumeration.
Does anyone has an idea?
Thanks,
Hauke
Dependency injection is only supported for objects created by the container such as EJBs. That’s the usual way how dependency frameworks work (including Spring and Google Guice).
You could pass the service (not used currently?) or the EntityManager as parameter though.