简体   繁体   中英

Using one to many relation in AdditionalCriteria

Entities: Post, Space and Profile. In summary:

class Post { Space space; String text; }

class Space { List<Profile> members; }

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

How to set an @AdditionalCriteria on Post to only return the posts that belongs to the spaces which the current user is a member of.

What I have tried so far are below.

#1 - :currentUserProfile in space.members

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

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

Results 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 member of space.members

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

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

Results in:

Caused by: org.postgresql.util.PSQLException: Não pode inferir um tipo SQL a ser usado para uma instância de br.com.senior.social.model.profile.Profile. Use setObject() com um valor de Types explícito para especificar o tipo a ser usado.
    at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:1039)

In english: "Could not infer a SQL type to use for an instance of _. Use setObject() with a explicit value of Types to set the type to be used."

#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);

Results 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);

Results in:

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

EclipseLink emits a SQL for the table without using the descriptor (prefix).

Solved it by using a not-so-elegant solution:

First, I added a raw ID attribute to the Post-Space relation:

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

Then referenced it in the additional criteria:

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

And initialized :currentUserSpaceIds to the actual spaces that the current user is member of.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM