0
我遇到以下問題。也許設計不太好,但我找不出更好的設計。在實體類中枚舉中使用Entitymanager
我有一個實體類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
,我有這個枚舉類/實體
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();
}
}
兩個EM內部和databaseService爲空。目標是從枚舉和其他方式獲取實體。我不想與實體合作。相反,我想使用枚舉,但有時我需要實體。所以我想我可以做這樣的事情,以便從我的枚舉中獲取設備實體。
有沒有人有想法?
謝謝, Hauke
依賴注入在枚舉內也不錯。但將它傳遞給方法的想法並不是那麼糟糕。不是很好,但至少應該工作。 謝謝! – Hauke 2012-04-20 10:14:17