2016-06-01 7 views
0

Entitäten: Post, Space und Profile. Zusammengefasst:Verwenden einer Eins-zu-Viele-Beziehung in AdditionalCriteria

class Post { Space space; String text; } 

class Space { List<Profile> members; } 

class Profile { String username; List<Space> spaces; } 

Wie ein @AdditionalCriteria auf Post, um nur die Beiträge zurückkehren zu setzen, die zu den Räumen gehören, die die aktuellen Benutzer Mitglied ist.

Was ich bisher versucht habe, sind unten.

# 1 -: currentUserProfile in space.members

@AdditionalCriteria(":currentUserProfile in (this.space.members)") 

Profile profile = new Profile(); 
em.setProperty("currentUserProfile", profile); 

Ergebnisse in:

Exception [EclipseLink-6015] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.QueryException 
Exception Description: Invalid query key [space] in expression. 
Query: ReadObjectQuery(name="readPost" referenceClass=Post) 

# 2 -: currentUserProfile Mitglied space.members

@AdditionalCriteria(":currentUserProfile member of this.space.members") 

Profile profile = new Profile(); 
em.setProperty("currentUserProfile", profile); 

Ergebnisse in:

In Englisch: "Konnte keinen SQL-Typ für eine Instanz von _ ableiten. Verwenden Sie setObject() mit einem expliziten Wert von Typen der Typ, der verwendet werden „

# 3 - this.space in profile.spaces

@AdditionalCriteria("this.space in (select p.spaces from Profile p where p.username = :currentUsername)") 

String username = "foo"; 
em.setProperty("currentUsername", username); 

Ergebnisse in:.

Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.JPQLException 
Exception Description: Problem compiling [this.space in (select p.spaces from Profile p where p.username = :currentUsername)]. 
[131, 139] The state field path 'p.spaces' cannot be resolved to a collection type. 

# 4 -: currentUsername = (subselect)

@AdditionalCriteria(":currentUsername = (select p.username from Profile p where p.username = :currentUsername and this.space in (p.spaces))") 

String currentUsername = "foo"; 
em.setProperty("currentUsername", currentUsername); 

Ergebnisse in:

org.postgresql.util.PSQLException: ERROR: relation "post" does not exist 
    Posição: 423 

aussendet Eclipse eine SQL für die Tabelle ohne den Deskriptor (prefix).

Antwort

0

es wird gelöst durch eine nicht-so-elegante Lösung mit:

Zuerst habe ich ein rohes ID-Attribut die Beziehung Post-Raumes:

class Post { 
    @Column(name = "SPACE_ID", updatable = false, insertable = false) 
    private String spaceId; 
} 

es dann in den zusätzlichen Kriterien verwiesen:

@AdditionalCriteria("this.spaceId in :currentUserSpaceIds") 

Und initialisiert :currentUserSpaceIds auf die tatsächlichen Räume, das die aktuellen Benutzer Mitglied ist.

EntityManager em = ...; 
Profile profile = ...; // current user profile 
List<Space> spaces = profile.getSpaces(); 
List<String> spaceIds = new ArrayList<>(); 
for (Space space : spaces) { spaceIds.add(space.getId()); } 
em.setProperty("currentUserSpaceIds", spaceIds); 
Verwandte Themen