2016-04-29 6 views
1

Ich benutze die JPA EntityManager.getReference(Class clazz, Integer pk). Ich weiß, dass pk Wert auf eins festgelegt ist, aber in dem Objekt, das von der Methode zurückgegeben wird, wird der Wert auf Null festgelegt. Ich verstehe nicht warum.JPA getReference eine falsche ID in meinem Objekt (immer auf Null gesetzt) ​​

Hier ist mein Code:

@Component 
public class TypeDAO extends MainDAO { 

    public TypeDTO simpleProxyRetrieveByIdType(int id){ 
     TypeDTO type = simpleProxyRetrieveById(TypeDTO.class, id); 
     return type; 
    } 
} 

Meine MainDAO Klasse:

static protected <E> E simpleProxyRetrieveById(Class <E> clazz, Integer id) { 
    EntityManager em = emFactory.createEntityManager(); 
    E item = em.getReference(clazz, id); 
    em.close(); 
    return item; 
} 

Hier ist die TypeDTO Klasse:

@Entity 
@Table(name = "type_livr") 
public class TypeDTO { 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE) 
    @Column(name = "type_livraison_id") 
    private int id; 
     public int getId() {return id;} 
     public void setId(int id) {this.id = id;} 

    @Column(name = "code") 
    private int code; 
    public int getCode() {return code;} 
    public void setCode(int code) {this.code = code;} 

    @Column(name = "libelle_court") 
    private String libelleCourt; 
     public String getLibelleCourt() {return libelleCourt;} 
     public void setLibelleCourt(String libelleCourt) {this.libelleCourt = libelleCourt;} 

    @Column(name = "libelle_long") 
    private String libelleLong; 
     public String getLibelleLong() {return libelleLong;} 
     public void setLibelleLong(String libelleLong) {this.libelleLong = libelleLong;} 

    @OneToMany(mappedBy = "type") 
    private List<LivraisonDTO> typeLivr; 
     public List<LivraisonDTO> getTypeLivr() {return typeLivr;} 
     public void setTypeLivr(List<LivraisonDTO> typeLivr) {this.typeLivr = typeLivr;} 

    public TypeDTO() { 
    } 

    public TypeDTO(int id, int code, String libelleCourt, String libelleLong) { 
     this.id = id; 
     this.code = code; 
     this.libelleCourt = libelleCourt; 
     this.libelleLong = libelleLong; 
    } 
} 

Vielen Dank für jede Antwort.

Antwort

0

Es gibt kein Problem in der Tat:

id nicht wirklich reale ID initialisiert wird, bis Sie die getId() Getter genannt.

Sie sollten nur im Debugger überprüft haben!

Verwandte Themen