tags:

views:

32

answers:

1

I have two tables, users and images which need to be joined. there is exactly one user row in the users table to many images in the images table.

In my users bean I have a private Set variable with a @OneToMany relationship it looks like this

//Users.java
@Entity
@Table(name = "users")
@NamedQueries ({
@NamedQuery(name = "Users.getUserImage",
query("from Users as users INNER JOIN fetch users.images as image WHERE users.userId image.userId AND users.userId =: passedId")
})
public class Users
   private Set<UserImages> images;

   @OneToMany(mappedBy = "userId", fetch = FetchType.LAZY, cascade=CascadeType.ALL)
   public Set<UserImages> getImages() {
        return images;
   }

   public void setImages(Set<UserImages> images) {
        this.images = images;
  }
}

Then I have a UserImages bean which stores a bunch of data but has the fk userId which looks like so.

//UserImages.java

private Integer userId;

@Column(name = "user_id", updatable=true, nullable=false)
public Integer getUserId() {
    return userId;
}

public setUserId(Integer userId) {
    this.userId = userId;
}

I am calling the getUserImage namedQuery from within my DAO to get the resultSet.

So this works well except for when a user has NO images in the UserImages table. (User has not uploaded any images yet). I have set up a test to test everything and if a user has an image it works great I can call the getImages() method on a User and it will return a set and I can iterate through that. But if the User does not have any images it gives me a null pointer exception right away.

I have tried to set the Set to null in the setUserImages() method if variable this.images = null but that does not seem to work. Any help would be great. Thanks!

+1  A: 

That's not the way you do things in Hibernate. The whole point of using an ORM is that you don't have to deal with foreign keys, but with object references:

@Entity
public class UserImage{

    @ManyToOne
    private User user;

    public User getUser(){return user;}
    public setUser(User user){this.user = user;}

}

@Entity
public class User{

    @OneToMany(mappedBy="user")
    private Set<UserImage> images;

    public void setImages(Set<UserImage> images){this.images=images;}
    public Set<UserImage> getImages(){return this.images;}

}

About queries: don't use join. Use something like this (I only use JPA, so I'm not sure about HQL):

Select i from UserImage i where user = :user and filename like :pattern

Pass the user object and the pattern as parameter and let hibernate do the join internally. There's no use in using an ORM, if you're going to do the leg work yourself.

seanizer
Thank you for your reply, but this confuses me I am not dealing with a foreign key, my Users class contains a private variable Set<UserImage> which store the set of UserImage objects that are associated with that user. I am new to this but from what you wrote it looks like you want me to store a userid inside of the UserImages class? I assume private User user whould be private User userId?
HiberNATHAN
No, you are not dealing with a foreign key. Your database is dealing with a foreign key and so is hibernate. You are dealing with an object reference that is automatically mapped to a foreign key.
seanizer
okay, so I guess what consfuses me is that private User user, do you really mean private UserImages userId; the field that I use the mappedBy notation for?
HiberNATHAN
see my update for the way I'd model it
seanizer
Thank you very much
HiberNATHAN
I have updated my post because I think its a different situation because I am trying to call a NamedQuery in my DAO to get a specific resultSet not everything. I am not sure though I am new to Hibernate and this is starting to really confuse me.
HiberNATHAN
you don't need a named query for that, that's what hibernate does automatically if you use my code. Just try it. HQL / JPQL Queries and Named queries are for situations that go beyond what the ORM can do automatically.
seanizer
I guess but I don't understand when I am in my DAO I need to call a specific JOIN to get specific results, this just maps the two tables. If I call the getImages() method like so User.getImages(); I will get all the images back. I want to be able to do stuff like get all Images that are .png or special cases wouldnt I need a named Query for that?
HiberNATHAN
yes, for that you will need a query, though not necessarily a named one. But you don't need a Join. see my updated answer for an example query
seanizer
okay thank you for taking the time, I am very new to this. Thanks again!
HiberNATHAN
no prob. that's what this site is for
seanizer