0

내가 가진이최대 절전 모드 기준 1 : 1 하위 레코드가 null이 아닌 상위 레코드를 반환합니까?

같은 일대일 관계 나 부모를 반환 기준을 만들려는 부모

@JsonAutoDetect 
@Entity 
@Table(name = "Parent") 
public class Parent{ 

    private Integer id; 
    private Child child; 

    @Id 
    @GeneratedValue(strategy= GenerationType.AUTO) 
    @Column (name="idparent") 
    public Integer getId() { 
     return id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent") 
    @JoinColumn(name="idchild", nullable=true) 
    public Child getChild() { 
     return child; 
    } 
    public void setChild(Child child) { 
     child.setParent(this); 
     this.child = child; 
    } 
} 

와 아이

@JsonAutoDetect 
@Entity 
@Table(name="Child") 
public class Child{ 

    private Integer id; 
    private Parent parent; 

    @Id 
    @GeneratedValue(generator = "foreign") 
    @GenericGenerator(name = "foreign", strategy = "foreign", parameters = { @Parameter(name = "property", value = "parent") }) 
    @Column (name = "idchild") 
    public Integer getId() { 
     return id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 

    @OneToOne(fetch = FetchType.LAZY, optional = true) 
    @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE }) 
    @PrimaryKeyJoinColumn 
    @JsonIgnore 
    public Parent getParent() { 
     return parent; 
    } 
    public void setParent(Parent parent) { 
     this.parent= parent; 
    } 

}

그 아이가있다 아니, 아니 난 tryed 솜 생각처럼

Criteria criteria = session.createCriteria(Parent.class); 
criteria.add(Restrictions.isNotNull("child")); 

하지만 작동하지 않습니다, 제발 좀 도와 주실 수 있습니까? 감사합니다.

+0

호기심에서 벗어나 부모와 자녀를 사용합니다. 학부모가 자녀를 여러 명 가질 수 있습니까? 그렇다면 관계의 각 끝에서 OneToMany와 ManyToOne을 사용해야하며 API/jpql 조건에 "exists"가 있어야합니다. –

답변

1

우선 매핑이 잘못되었습니다. 상위 클래스에서 연관은 child.parent으로 매핑되고, id_child이라는 조인 열을 사용하여 매핑되었다고 말한 직후입니다. 당신의 마음을 확인하십시오. child.parent 속성으로 매핑되었으므로 @JoinColumn을 삭제해야합니다. 또는 JoinColumn에 의해 매핑되고 하위 항목에서 @PrimaryKeyJoinColumn을 제거하고 하위 항목에서 mappedBy="child"을 사용해야합니다. 조인은 내부 조인이기 때문에 그것은 단지 비를 가지고 부모를 선택합니다,

Criteria criteria = session.createCriteria(Parent.class); 
criteria.createAlias("child"); // inner join 

: 이제

는 쿼리 작업을하기 위해, 매핑이 무엇이든, 당신은 단순히 내부 조인해야 null 자식.

+0

안녕하세요, 응답을 위해 thznks, 부모로부터 JoinColumn을 제거했지만 작동하지 않습니다. 어떻게하는지 정확하게 표시해주세요. – user820688