-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JPA / Hibernate...probem with criteria in the relationship
PostPosted: Tue Jan 27, 2009 3:21 pm 
Newbie

Joined: Tue Jan 13, 2009 11:04 am
Posts: 13
doubts with other criteria when I query that amount
where the document type 1 and the equal value CPF200
would have to return that record but only two back
because that user has two records but when I
getDocuments user has read the two as I do to get it?


In the spring selção was on top of the document type restriction equal to 1
CPRF200 and value and has two records of getDocuments?

Somebody help me ....


abs



[code]

class user (
@ Id
private id;

@ OneToMany (mappedBy = "user", cascade = javax.persistence.CascadeType.ALL)
private List <Document> documents <Document> = new ArrayList ();

-----

public class Document (

@ Id
@ GeneratedValue
@ Column (name = "DOCUMENT_ID")
Private long id;
@ ManyToOne (fetch = FetchType.EAGER)
@ JoinColumn (name = "USER_ID")
UserImpl private user;

@ Column
private String type;

@ Column
private String value;

[/ code]

[code]
- as is in the database

select * from document

id user type value
1 11 1 CPF200
2 11 2 RG100

--- reading the result getDocuments
Criteria c = session.createCriteria (User.class, "user");
c.createAlias ( "documents", "doc");
if (ccs.getCampo (). equals ( "SFC"))
c.add (Restrictions.eq ( "doc.type", 1));
c.add (Restrictions.eq ( "doc.value", "CPF200"));

List result = c.list ();

System.out.println ( "Return list size =" + result.size ());

for (Iterator iterator = result.iterator (); iterator.hasNext ()) (
UserImpl user = (UserImpl) iterator.next ();
System.out.println ( "** Id =" + user.getId ());

For (Document doc: user.getDocuments ()) (
System.out.println ("-------------------------------------");
System.out.println ( "** Document Id =" + doc.getId ());
System.out.println ( "** Document value =" + doc.getValue ());
System.out.println ( "** Document User =" + doc.getUser (). GetId ()+"-"+ doc.getUser (). GetName ());

)
System.out.println ("-------------------------------------");
)

----

result

Hibernate: select this_.USER_ID the USER1_9_7_, this_.ADDRESS_ID the ADDRESS11_9_7_, this_.admissionDate the admissio2_9_7_, this_.birthDate the birthDate9_7_, this_.email the email9_7_, this_.GROUP_ID the GROUP12_9_7_, this_.ldapPath the ldapPath9_7_, this_.logonAttempts the logonAtt6_9_7_ , this_.name the name9_7_, this_.profession the profession9_7_, this_.ROLE_ID the ROLE13_9_7_, this_.object_state the object9_9_7_, this_.upn the upn9_7_, address3_.ADDRESS_ID the ADDRESS1_16_0_, address3_.additionalInfo the addition2_16_0_, address3_.city the city16_0_, address3_ . country16_0_ the country, address3_.mobile the mobile16_0_, address3_.neighborhood the neighbor6_16_0_, address3_.phone the phone16_0_, address3_.postalCode the postalCode16_0_, address3_.state the state16_0_, address3_.street the street16_0_, documents1_.DOCUMENT_ID the DOCUMENT1_18_1_, documents1_.document_type the document2_18_1_, documents1_.USER_ID the USER4_18_1_, documents1_.value the value18_1_, fields5_.DOCUMENT_ID the DOCUMENT5_9_, fields5_.FIELD_ID the FIELD1_9_, fields5_.FIELD_ID the FIELD1_17_2_, fields5_.description the descript2_17_2_, fields5_.DOCUMENT_ID the DOCUMENT5_17_2_, fields5_.name the name17_2_ , fields5_.value the value17_2_, userimp6_.USER_ID the USER1_9_3_, userimp6_.ADDRESS_ID the ADDRESS11_9_3_, userimp6_.admissionDate the admissio2_9_3_, userimp6_.birthDate the birthDate9_3_, userimp6_.email the email9_3_, userimp6_.GROUP_ID the GROUP12_9_3_, userimp6_.ldapPath the ldapPath9_3_, userimp6_ . logonAttempts the logonAtt6_9_3_, userimp6_.name the name9_3_, userimp6_.profession the profession9_3_, userimp6_.ROLE_ID the ROLE13_9_3_, userimp6_.object_state the object9_9_3_, userimp6_.upn the upn9_3_, groupimpl7_.GROUP_ID the GROUP1_26_4_, groupimpl7_.name the name26_4_, groupimpl7_.PARENT_GROUP_ID the PARENT3_26_4_, groupimpl8_.GROUP_ID the GROUP1_26_5_, groupimpl8_.name the name26_5_, groupimpl8_.PARENT_GROUP_ID the PARENT3_26_5_, roleimp9_.ROLE_ID the ROLE1_23_6_, roleimp9_.description the descript2_23_6_, roleimp9_.name the name23_6_ from _USER this_ left outer join _ADDRESS address3_ on this_.ADDRESS_ID = address3_.ADDRESS_ID inner join _DOCUMENT documents1_ on this_.USER_ID = documents1_.USER_ID left outer join _DOCUMENT_FIELD fields5_ on documents1_.DOCUMENT_ID = fields5_.DOCUMENT_ID left outer join _USER userimp6_ on documents1_.USER_ID = userimp6_.USER_ID left outer join _GROUP groupimpl7_ on this_. group_id = groupimpl7_.GROUP_ID left outer join _GROUP groupimpl8_ on groupimpl7_.PARENT_GROUP_ID = groupimpl8_.GROUP_ID left outer join _ROLE roleimp9_ on this_.ROLE_ID = roleimp9_.ROLE_ID where documents1_.USER_ID = this_.USER_ID and documents1_.document_type =? and documents1_.value =? order by this_.USER_ID desc
Return list size = 1
** Id = 4
Email = padrao@certisign.com.br
LdapPath = fsufhsdufhs
Name = Paulo Ribeiro
Occupation = null
Admission Date = null
Hibernate: select documents0_.USER_ID the USER4_1_, documents0_.DOCUMENT_ID the DOCUMENT1_1_, documents0_.DOCUMENT_ID the DOCUMENT1_18_0_, documents0_.document_type the document2_18_0_, documents0_.USER_ID the USER4_18_0_, documents0_.value the value18_0_ from _DOCUMENT documents0_ where documents0_.USER_ID =?
Hibernate: select fields0_.DOCUMENT_ID the DOCUMENT5_1_, fields0_.FIELD_ID the FIELD1_1_, fields0_.FIELD_ID the FIELD1_17_0_, fields0_.description the descript2_17_0_, fields0_.DOCUMENT_ID the DOCUMENT5_17_0_, fields0_.name the name17_0_, fields0_.value the value17_0_ from _DOCUMENT_FIELD fields0_ where fields0_. DOCUMENT_ID =?
-------------------------------------
** Document Id = 1
** Document CPF69 Value =
** Document Value = 4-Paul
-------------------------------------
** Document Id = 2
** Document Value RG = 001
** Document Value = 4-Paul
-------------------------------------
End testCriteriaBuscaUsuario

[/ code]


Top
 Profile  
 
 Post subject: Have you tried the other way around?
PostPosted: Tue Jan 27, 2009 6:10 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi,
Instead of Querying on the User class and restricting the document type, have you tried to query on the Document Class and restric on the Document Type?
Something like this:-
Criteria c = session.createCriteria (Document.class, "doc");
//c.createAlias ( "documents", "doc");
if (ccs.getCampo (). equals ( "SFC"))
c.add (Restrictions.eq ( "doc.type", 1));
c.add (Restrictions.eq ( "doc.value", "CPF200"));
//c.add (Restrictions.eq ( "doc.USER_ID",1);

List result = c.list ();
//iterate over the results

-Srilatha.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.