Java JPA getReference在我的对象中设置了错误的id(始终设置为零)
我使用的是JPAJava JPA getReference在我的对象中设置了错误的id(始终设置为零),java,jpa,Java,Jpa,我使用的是JPAEntityManager.getReference(Class clazz,Integer pk)。 我知道pkvalue被设置为1,但在方法返回的对象中,该值仍然被设置为零。我不明白为什么 这是我的密码: @Component public class TypeDAO extends MainDAO { public TypeDTO simpleProxyRetrieveByIdType(int id){ TypeDTO type = simpleP
EntityManager.getReference(Class clazz,Integer pk)
。
我知道pk
value被设置为1,但在方法返回的对象中,该值仍然被设置为零。我不明白为什么
这是我的密码:
@Component
public class TypeDAO extends MainDAO {
public TypeDTO simpleProxyRetrieveByIdType(int id){
TypeDTO type = simpleProxyRetrieveById(TypeDTO.class, id);
return type;
}
}
我的MainDAO
课程:
static protected <E> E simpleProxyRetrieveById(Class <E> clazz, Integer id) {
EntityManager em = emFactory.createEntityManager();
E item = em.getReference(clazz, id);
em.close();
return item;
}
@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;
}
}
谢谢您的回答。事实上没有问题: 在调用getId()getter之前,id不会真正初始化为真实id
您应该只签入调试器 事实上没有问题: 在调用getId()getter之前,id不会真正初始化为真实id 您应该只签入调试器