2016-09-15 4 views
0

저는 Java 세계에 발을 들여 놓기 시작했으며이를 이해하는 데 어려움을 겪고 있습니다.2 id를 기본 키로 사용하는 객체를 사용하는 EntityManager.find 동작

JPQL을 사용하여 데이터베이스와 상호 작용하는 데 JPA를 사용하고 있습니다.

@Embeddable 
public class CATPKimplements Serializable { 
    //default serial version id, required for serializable classes. 
    private static final long serialVersionUID = 1L; 
    @Column(name="ID_CAT") 
    private String idCAT; 
    @Column(name="ID_DEMANDE") 
    private String idDemande; 

    public CATPK() {} 
    public String getIdCAT() {return this.idCAT;} 
    public void setIdCAT(String idCAT) {this.idCat= idCat;} 
    public String getIdDemande() {return this.idDemande;} 
    public void setIdDemande(String idDemande) {this.idDemande = idDemande;} 
    /* ...*/ 
} 

그래서 basicly의 primay 키를 2로 구성되어 있습니다

@Entity 
@Table(name="C_A_T") 
@NamedQuery(name="CAT.findAll", query="SELECT c FROM CAT c") 
public class CAT implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @EmbeddedId 
    private CATPK id; 
    private String offer; 
    @Temporal(TemporalType.DATE) 
    @Column(name="MODIF_DATE") 
    private Date modifDate; 


    public CAT() {} 
    public CATPK getId() {return id;} 
    public void setId(CATPK id) {this.id = id;} 
    public String getOffer() {return offer;} 
    public void setOffer(String offer) {this.offer = offer;} 
    public Date getModifDate() {return modifDate;} 
    public void setModifDate(Date modifDate) {this.modifDate= modifDate;} 
    /* ... */ 
} 

클래스 CATPKCAT의 인스턴스의 기본 키를 나타냅니다

나는 이와 같은 영속 클래스가 다른 ID. 그것은 C_A_T 테이블에없는 경우

지금은 가끔, 내 데이터베이스에 CAT를 삽입하기 전에 내가 확인 : 이제

EntityManagerFactory emf = Persistence.createEntityManagerFactory("cie"); 
em = emf.createEntityManager(); 
em.getTransaction().begin(); 
// inCAT is an instance of CAT on which there is a complete primary key CATPK (made of 2 id) 
CAT CATinDB = em.find(CAT.class, inCAT.getId()); 
if (null == CATinDB) 
    em.persist(inCAT); // CAT created 
em.getTransaction().commit(); 
em.close(); 

, 문제는 때 선 em.find(CAT.class, inCAT.getId());null를 반환하지 않는다는 것입니다 할까요.

예를 들어 CATinDB에는 실제로 idCATidDemande 행이 없을 때 행을 포함 할 수 있습니다.

그러면 find은 일치하는 catPK의 ID가 하나만있는 것으로 간주합니까? 나는이 ID를 일치하는 기록이 없었 때 find가 null 반환되지 않은 이유를 정말 확실하지

if (null == CATinDB || CATinDB.getId().getIdCAT() != inCAT.getId().getIdCAT() || CATinDB.getId().getIdDemande() != inCAT.getId().getIdDemande()) 

:에

if (null == CATinDB) 

: 결국

, 나는 라인을 변경했습니다.

+0

여기에서 JPA 제공자 로그를보고 SQL 호출을 볼 수 있습니다. –

+0

어떻게하면됩니까? 나는 intelliJ를 사용하고있다. persistence.xml 파일이 있는데 거기에서 어디로 갈지 알지 못합니다. – Ellone

+0

persistence.xml은 대개 JPA 제공자를 지정합니다. 그것이 좋든 그렇지 않든, 당신은 CLASSPATH와 "emf"인스턴스의 클래스에서 사용하고있는 것을 쉽게 처리 할 수 ​​있습니다. 따라서 해당 공급자의 문서를보고 로그를 살펴보십시오. 일명 "디버깅" –

답변

0

사실, weblogic 서버를 다시 시작해야했습니다. 데이터베이스를 업데이트 할 때 데이터 소스가 동적으로 업데이트되지 않는 것 같습니다.