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!