Hemang Suchde
Hemang Suchde

Reputation: 13

Hibernate @where Annotation: usage for attribute present on the inverse side of @OnetoOne association. (mappedBy)

Here are my classes Alpha and Beta. Alpha has a one to one relationship with Beta

I want to fetch only those Alpha records which have Beta.

I am unable to create a where clause annotation for Alpha which will help me achieve this.

Is there a better way to arrange the annotations, since i want to avoid any additional queries in the code while fetching alpha objects.

/** CLass Alpha*/
@Entity
@Where(clause x =0  and y =0  and  ????????)
class Alpha {    

    @Column(name ="X")
    int x;

    @Column(name ="Y")
    int Y;

    @OneToOne(mappedBy = "alpha")
    Beta beta;
}

/** Class Beta*/
@Entity
class Beta {    
    @OneToOne
    @JoinColumn(name = "ALPHA_REF")
    Alpha alpha;    
}

Any pointers would be very much appreciated

Upvotes: 1

Views: 744

Answers (1)

rahul pasricha
rahul pasricha

Reputation: 931

you can use named queries

@NamedQueries({
@NamedQuery(name = "<query-name>", query = "SELECT a FROM Alpha a WHERE a.beta is not null :})

Upvotes: 1

Related Questions