user393964
user393964

Reputation:

Join query with JPA?

I'm having a hard time with this join query in my application. I have two classes, Encounter and CheckedEncounter. CheckedEncounter is a temporary model that I'm using to manage the Encounters that have already been processed. It looks like this:

@Entity
public class CheckedEncounters extends GenericModel {

    @Id
    @GeneratedValue
    @Column(name="ce_id")
    public int id;

    @Column(name="ce_encounter")
    @OneToOne
    public Encounter encounter;

    @Column(name="ce_synced")
    public boolean synchronised;

}

What I want to do is select every Encounter, that was created for a specific user and which has no CheckedEncounters record yet (so only the ones that can't be found inside CheckedEncounters tabel)

With this line I'm selecting all my encounters for my specific user:

List<Encounter> encounterRequess = Encounter.find("byTargetUser", myuser).fetch();

Edit:

public static List<Encounter> getNewEncountersByTargetUser(User targetUser){
    return Encounter.find("from Encounter as enc " +
            "where not exists ( " +
            "from CheckedEncounter as cenc " +
            "where cenc.encounter = enc)" +
            "and enc.targetUser = ?", targetUser).fetch();
}

I tried it with the query marc suggested but It's giving me an error:

Execution exception
IllegalArgumentException occured : org.hibernate.hql.ast.QuerySyntaxException: CheckedEncounter is not mapped [from models.Encounter as enc where not exists ( from CheckedEncounter as cenc where cenc.encounter = enc)and enc.targetUser = ?]

Upvotes: 2

Views: 863

Answers (1)

marc
marc

Reputation: 6223

Try

from Encounter as enc
where not exists (
    from CheckedEncounter as cenc 
    where cenc.encounter = enc
)

See here.

Upvotes: 2

Related Questions