-->
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: Querying DAOs with Set<Something>
PostPosted: Sat Oct 14, 2006 8:39 am 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
I have a class called User and with a criteria search I make some User queries....

All users from the query where unique...nobody appeared several times.

Then I added the method

public Set<Picture> getPictures();


and all the previous queries now returned some sort of joined resultset where usernames appeared several times......

So I added:

criteria.setFetchMode("pictures", FetchMode.SELECT);

to every query and it is okay again......

BUT: I think I must be using some "wrong" Annotations...or why would Hibernate join the Picture table in a way usernames appear several times?
That's my User class:

Code:
@ManyToOne
@JoinColumn(name="uid")

   public User getUser() {
      return user;
   }



        @OneToMany (fetch=FetchType.EAGER) 

// if I dont use Eager I get lazy exceptions, so I have to use it!


   @JoinColumn(name="uid")
   
   public Set<Picture> getPictures(){
      return _pictures;
   }
   

That's my Picture class:

Code:
public class Picture implements java.io.Serializable {

   // Fields   

   private int picid;

   private User user;

        @ManyToOne
   @JoinColumn(name="uid")

   public User getUser() {
      return user;
   }



...





Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 14, 2006 2:55 pm 
Newbie

Joined: Sat Oct 07, 2006 2:48 pm
Posts: 17
if you has one-to-many relationship, you should to do something like this:

Code:
@Entity
public class User implements java.io.Serializable
{
    int id;
    Set<Picture> pictures;

    @OneToMany(mappedBy="user") //the name od the property
    public Set<Picture> getPictures()
    {
        return pictures;
    }
}


@Entity
public class Picture implements java.io.Serializable
{
    int id;
    User user;

    @ManyToOne(fetch=FetchType.LAZY)
    public User getUser()
    {
         return user;
    }
}


What is the criteria you used?

Whit this example, when you get a persisted User, you can do getPictures, and hibernate will find all pictures where property user equals to the entity are using.


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.