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

  • SEARCH
  • Home
  • 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 8907505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:56:00+00:00 2026-06-15T02:56:00+00:00

I have a User class that has a @OneToMany List<Address> property. Hibernate has created

  • 0

I have a User class that has a @OneToMany List<Address> property. Hibernate has created a join table between user and address. When a user logs in, I look for the user with a custom SQL query;

select * from user where username = ? and password = ?

This obviously returns all the other fields of user but not the List<Address>. Is there a way in Hibernate to also return the relational data without using custom queries?

Could I load the user through the custom query above, pull its ID, and then reload the user so that Hibernate loads all the fields? Are there any better ways?

@Entity
public class User {

    @Id
    @GeneratedValue
    @GenericGenerator(name = "incremental", strategy = "increment")
    private Long userID;

    @Column(nullable = false)
    private String username;

    @Column(nullable = false)
    private String email;

    @Column(nullable = false)
    private String password;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_address", 
        joinColumns = @JoinColumn(name = "userID"), 
        inverseJoinColumns = @JoinColumn(name = "addressID"))
    private List<Address> addresses;
}

@Entity
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NonVisual
    public Long addressID;
}
  • 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-06-15T02:56:01+00:00Added an answer on June 15, 2026 at 2:56 am

    Try using HQL as below:

    String queryString = "select u from User u left join u.addresses "+
                         "where u.username = :userName and u.password = :password";
    

    EDIT: Sample code to execute HQL

    String queryString = "select u from User u left join u.addresses "+
                         "where u.username = :userName and u.password = :password";
    Query query = session.createQuery(queryString );
    query.setParameter("userName", userNameValue);
    query.setParameter("password", passwordValue);
    List<User> users = query.list();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Bar that has a user-defined list of config keys and
I have a User class that has a property called Creator which is of
I have a Ticket class that contains a List of User objects: class Ticket
I have two models: User MentoringRelationship MentoringRelationship is a join model that has a
I have class User that contains protected constructor and class Account that has access
Here is my current situation: I have a user class that has an attribute
I'm implementing a Model View Control program. I have a class User that has
I have a user model that has many carts class User < ActiveRecord::Base has_many
I have a user defined class that I want to create a public List
I have a User class that has many posts and the post class has

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.