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 4111394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:03:38+00:00 2026-05-20T22:03:38+00:00

I have the following classes mapped as one to many: Reader and book when

  • 0

I have the following classes mapped as one to many: Reader and book when one reader can hold more then one book:

Book:

@Entity
@Table(name = "book")
public class Book implements Serializable{

    private static final long serialVersionUID = 1L;

    private Long id;
    private String author;
    private String title;

    public Book(){}
    public Book(String author,String title){
        this.author = author;
        this.title = title;
    }

    @Id
    @GeneratedValue
    @Column(name = "BOOK_ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "author")
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

    @Column(name="title")
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    //equals and hashcode ommited.
}

Reader

@Entity
@Table(name = "reader")
public class Reader implements Serializable{

    private static final long serialVersionUID = 1L;

    private Long id;
    private String firstName;
    private String lastName;
    Set<Book> set = new HashSet<Book>();

    @Transient
    public void loanBook(Book book){
        set.add(book);
    }

    @Id
    @GeneratedValue
    @Column(name = "READER_ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name="firstName")
    public String getFirstName() {
        return firstName;
    }


    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    @Column(name="lastName")
    public String getLastName() {
        return lastName;
    }


    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @OneToMany(cascade=CascadeType.ALL,fetch = FetchType.LAZY)
     @JoinColumn(name="READER_ID")
    public Set<Book> getSet() {
        return set;
    }


    public void setSet(Set<Book> set) {
        this.set = set;
    }

    public Reader(){}

    public Reader(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

Now I would like to fetch reader class using some book id for example:

Reader reader = service.getReaderbyBook(Long.valueOf(10));

My function looks like:

    @Override
    public Reader getReaderbyBook(Long id) {
        Session session = null;
        Reader reader = null;
         session =sessionFactory.openSession();
           org.hibernate.Transaction tr = session.getTransaction();
           tr.begin();
           String hqlSelect = "Select .... where book.id:=id";
           Query query = session.createQuery(hqlSelect);
           query.setLong("id", id);
           reader =  (Reader) query.uniqueResult();
           tr.commit();
           session.flush();
              session.close();
              return reader;
    }

}

How my hql select should look like if I just want to fetch a single reader associated with some book?

  • 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-20T22:03:39+00:00Added an answer on May 20, 2026 at 10:03 pm

    from Reader r join r.set book where book.id = :id .

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

Sidebar

Related Questions

I have the following mapped classes: <class name=Company table=Company> <id name=ID column=CompanyID> <generator class=native/>
I have the following 3 classes Book Product SpecialOptions There are many Books, and
I have the following 3 rails classes, which are all stored in one table,
I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30,
I have two C# classes that have many of the same properties (by name
I have the following classes: class Person { public string Name { get; set;
I have following classes. class A { public: void fun(); } class B: public
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following classes: public class Person { public String FirstName { set;
What is the best practice to implement the following: we have a classes Order

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.