java - Using one to many relation in AdditionalCriteria -


entities: post, space , profile. in summary:

class post { space space; string text; }  class space { list<profile> members; }  class profile { string username; list<space> spaces; } 

how set @additionalcriteria on post return posts belongs spaces current user member of.

what have tried far 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 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 ser usado.     @ org.postgresql.jdbc.pgpreparedstatement.setobject(pgpreparedstatement.java:1039) 

in english: "could not infer sql type use instance of _. use setobject() explicit value of types set type used."

#3 - this.space in profile.spaces

@additionalcriteria("this.space in (select p.spaces profile p 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 profile p p.username = :currentusername)].  [131, 139] state field path 'p.spaces' cannot resolved collection type. 

#4 - :currentusername = (subselect)

@additionalcriteria(":currentusername = (select p.username profile p p.username = :currentusername , this.space in (p.spaces))")  string currentusername = "foo"; em.setproperty("currentusername", currentusername); 

results in:

org.postgresql.util.psqlexception: error: relation "post" not exist   posição: 423 

eclipselink emits sql table without using descriptor (prefix).

solved using not-so-elegant solution:

first, added raw id attribute post-space relation:

class post {     @column(name = "space_id", updatable = false, insertable = false)     private string spaceid; } 

then referenced in additional criteria:

@additionalcriteria("this.spaceid in :currentuserspaceids") 

and initialized :currentuserspaceids actual spaces current user member of.

entitymanager em = ...; profile profile = ...; // current user profile list<space> spaces = profile.getspaces(); list<string> spaceids = new arraylist<>(); (space space : spaces) { spaceids.add(space.getid()); } em.setproperty("currentuserspaceids", spaceids); 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo