Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3330868
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:31:49+00:00 2026-05-17T23:31:49+00:00

I have two tables, users and images which need to be joined. there is

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:31:49+00:00Added an answer on May 17, 2026 at 11:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: Accounts , which contains the information of accounts that a
I have two tables: Personnel with ID,Name,... columns and User with PersonnelID (FK of
In this simplified version of my actual problem, I have two tables: User and
i have these two tables , s(id,firstname,lastname,mydate,mount,sh) users(uid,email,password,nameoffice) and this query $sql=SELECT s.id,s.firstname,s.lastname,s.mydate,s.mount,users.nameOffice FROM
I am confused about selecting two approaches. Scenario there are two tables Table 1
Suppose I have the following tables: User, Computer and Command. Users and computers may
I have two tables named as: table_product table_user_ownned_auction table_product specific_product_id astatus ... (primary_key,autoinc) --------------------------------------
Ok, here's an easy one: I have a table in MySQL where I keep
I have a query that returns a datetime field and several other fields along
When the customers submit the orders on the website.. staffs can view the pending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.