-->
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.  [ 4 posts ] 
Author Message
 Post subject: JPA / Hibernate doubts fk relationship with criteria
PostPosted: Fri Jan 23, 2009 1:09 pm 
Newbie

Joined: Tue Jan 13, 2009 11:04 am
Posts: 13
as doubts mount criteria which have the class to create User
teho and the criteria that relate to other class that has a fk

I am not able to make this relationship ...

podria someone help me ...



[code]

Criteria c = session.createCriteria (User.class);

C.add (Restrictions.eq ( "id", new Long (9)));
C.createAlias ( "Document", "doc");
c.add (Restrictions.eqProperty ( "doc.value", "0"));

List result = c.list ();
/ *
[/ code]


[code]

- classes created

class User (
id in private

)

class Document (
private int id
private String value
private int id_user
)


[/ code]

[code]
erro:
org.hibernate.QueryException: could not resolve property: ImsDocument of: br.com.certisign.ims.data.model.user.ImsUserImpl
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 8:16 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
At class level, you should not use, for example, int-fields as foreign keys. You design your object-orientated model, which hibernate maps into a relational model. So your Document-class should look like this:
Code:
class Document (
  private int id;
  private String value;
  private User user;
)

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 9:49 am 
Newbie

Joined: Tue Jan 13, 2009 11:04 am
Posts: 13
Now how do you relate the two objects:

- Because one is a list and another is an id

Someone would go for an example

Code:



class Document (
  private int id;
  private String value;
  @ManytoOne
  private User user;
)


class User
id in private
// add this list
@OnetoMany
List documents ;



Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 6:23 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I don't know, if I understand you right. With relate you mean, how to tell hibernate that Document.user and User.documents are the same association? If yes, then you could use mappedBy:
Code:
class User
id in private
// add this list
@OneToMany(mappedBy="user")
List documents ;
}


Now, if you want to add documents to a user, you call user.getDocuments().add(..) and also document.setUser(...).

Take a look into the Annotation reference guide to see more examples how to map associations.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.