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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:23:53+00:00 2026-05-17T02:23:53+00:00

I try to build some application using hibernate for persistence (my first one). The

  • 0

I try to build some application using hibernate for persistence (my first one). The application has genres and books. The genre class has a set of books. But every book in the set has the idGenre (the foreign key) value 0. I think the mapping is not right. Please tell me where is the mistake. Thanks.

Here is the mapping:

<hibernate-mapping package = "model">

   <class name = "User" table="virtual_bookcase.users">
        <id name = "id" column = "id" type = "long">
            <generator class = "increment"/>
        </id>
        <property name = "username" column = "username" type = "string"/>
        <property name = "password" column = "password" type = "string"/>
   </class>

   <class name = "Genre" table = "virtual_bookcase.genres">
      <id name = "id" column = "idGenres" type = "long">
        <generator class = "increment"/>
      </id>   
      <property name = "name" column = "name" type = "string"/>
      <set name = "books" table = "virtual_bookcase.books" cascade = "all-delete-orphan">
        <key column = "idGenre" not-null = "true" />
        <one-to-many class = "Book"/>
      </set>
      <many-to-one name = "user" class = "User" column = "user_id" />
   </class>

   <class name = "Book" table = "virtual_bookcase.books">
        <id name = "id" column = "idBooks" type = "long">
         <generator class = "increment"/>
        </id>
        <property name = "title" column = "title" type = "string"/>
        <property name = "author" column = "author" type = "string"/>
        <property name = "publisher" column = "publisher" type = "string"/>
        <property name = "pages" column = "pages" type = "short"/>
        <property name = "borrowed" column = "borrowed" type = "byte"/>
        <property name = "borrowedTo" column = "borrowedTo" type = "string"/>
   </class>

</hibernate-mapping>

Here is where i load the books:

Session s = sessionFactory.openSession();

Query q = s.createQuery("FROM Book WHERE idGenre = ?").setLong(0, g.getId());

books = new TreeSet<Book>(q.list());

Here is the Book class:

public class Book implements Comparable<Book>
{
    private long id;
    private String title;
    private String author;
    private String publisher;
    private short pages;
    private byte borrowed;
    private String borrowedTo;
    private long idGenre;

public Book(){}
    public Book(String title, String author, String publisher, short pag, byte borrowed, String borrowedTo, long idGenre)
    {
        this.title = title;
        this.author = author;
        this.publisher = publisher;
        this.pages = pag;
        this.borrowed = borrowed;
        this.borrowedTo = borrowedTo;
        this.idGenre = idGenre;
    }

    public long getId()
    {
        return id;
    }

    public String getTitle()
    {
        return title;
    }

    public String getAuthor()
    {
        return author;
    }

    public String getPublisher()
    {
        return publisher;
    }

    public short getPages()
    {
        return pages;
    }

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

    public void setTitle(String title)
    {
        this.title = title;
    }

    public void setAuthor(String author)
    {
        this.author = author;
    }

    public void setPublisher(String publisher)
    {
        this.publisher = publisher;
    }

    public void setPages(short pages)
    {
        this.pages = pages;
    }

    public byte getBorrowed()
    {
        return borrowed;
    }

    public void setBorrowed(byte borrowed)
    {
        this.borrowed = borrowed;
    }

    public String getBorrowedTo()
    {
        return borrowedTo;
    }

    public void setBorrowedTo(String borrowedTo)
    {
        this.borrowedTo = borrowedTo;
    }

    public long getIdGenre()
    {
        return idGenre;
    }

    public void setIdGenre(long idGenre)
    {
        this.idGenre = idGenre;
    }

    @Override
    public String toString()
    {
        return title;
    }

    @Override
    public int compareTo(Book b)
    {
        if (this.title == b.getTitle() && this.author == b.getAuthor() && this.publisher == b.getPublisher() && this.pages == b.getPages())
            return 0;
        return 1;
    }

}

Here is how i make a new instance of a Book:

Book b = new Book("AddedBook", "A", "A", (short) 555, (byte) 0, "", 1);
Genre g = ((Genre) cmbGenres.getSelectedItem());
g.addBook(b);
control.saveGenre(g);

And the saveGenre(g) method is (without initialisation of SessionFactory and session and transaction):

   t = session.beginTransaction();
   Genre gr = (Genre) session.merge(g);
   t.commit();
  • 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-17T02:23:54+00:00Added an answer on May 17, 2026 at 2:23 am

    Your mapping looks OK to me at first sight. My suspicion is that you may not properly associate your Book entities with the corresponding Genre. Could you post the code to create and persist Books?

    Update: your code looks like it could work, although it is not clear why you merge your genre (if you work with your entities in detached state during conversations spanning multiple sessions, it’s OK, otherwise may be overcomplicating the picture). And the definition of Genre.addBook is missing, but I assume you did it right 🙂

    My new observation is that you haven’t mapped Book.idGenre. Try extending your mapping like this:

    <class name = "Book" table = "virtual_bookcase.books">
        ...
        <many-to-one name="idGenre" column="idGenre" class="Genre" not-null="true"/>
    </class>
    

    As a side note, this also makes some of the attributes on the other side of the association redundant – you could simplify it like this:

      <set name = "books" cascade = "all-delete-orphan">
        <key column = "idGenre"/>
        <one-to-many class = "Book"/>
      </set>
    

    Update2: oops, and one more thing: replace long idGenre with a Genre property in Book, like

    private Genre genre;
    

    and of course update getter/setter, and mapping accordingly.

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

Sidebar

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.