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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:17:09+00:00 2026-05-30T18:17:09+00:00

I am trying to make a simple example of Hibernate. I have two entities:

  • 0

I am trying to make a simple example of Hibernate. I have two entities: User and Note. They have relation a one to many (one user can have a lot of notes). Please help me to correctly display these relationships in a database using annotations.But I don’t want to create third table for implementation of the relation. I need to have only two tables:screen

Here are my classes:

User.java:

@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue
@Column(name = "id")
private Long id;

@Column(name = "name")
private String name;

@OneToMany(cascade = CascadeType.ALL, mappedBy="user") //Is it right value for  mappedBy-parameter?
private List<Note> notes = new ArrayList<Note>();

    // getters and setters

Note.java:

@Entity
@Table(name = "note")
public class Note {

@Id
@GeneratedValue
@Column(name = "id")
private Long id;

@Column(name = "content")
private String content;

@ManyToOne
private User user;

    // getters and setters

Main.java:

public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();

        List<Note> notes = new ArrayList<Note>();
        Note note1 = new Note();
        note1.setContent("my first note");
        Note note2 = new Note();
        note2.setContent("my second note");
        notes.add(note1);       
        notes.add(note2);   
        User user = new User();
        user.setName("Andrei");
        user.setNotes(notes);
        session.save(user);

        transaction.commit();
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

}

hibernate.cfg.xml:

<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="connection.pool_size">1</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">true</property>        
    <property name="hbm2ddl.auto">create-drop</property>
    <mapping class="com.vaannila.blog.User" />
    <mapping class="com.vaannila.blog.Note" />

After executing this code in my database Hibernate has created and has filled two tables:
user
note

But I encounter a problem: the field user_id value is null in note table.Although it must be equal to the user id (in this case 1).

What do I need to add to annotations for to solve this problem and this example to work correctly? But without creating additional tables.

I would really appreciate any help!

  • 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-30T18:17:11+00:00Added an answer on May 30, 2026 at 6:17 pm

    You must set the User inside each note as you have defined a bidirectional relationship. Instead of letting clients pass in a list of notes directly, create User.addNote and have it set the relationship correctly.

    class User {
        ...
        public void addNote(Note note) {
            note.user = this;
            notes.add(note);
        }
    }
    

    Your test code thus becomes

    Note note1 = new Note();
    note1.setContent("my first note");
    Note note2 = new Note();
    note2.setContent("my second note");
    User user = new User();
    user.setName("Andrei");
    user.addNote(note1);
    user.addNote(note2);
    session.save(user);
    

    You can further improve this by adding the basic fields to the constructors of your objects simplifying the above to

    User user = new User("Andrei");
    user.addNote(new Note("my first note"));
    user.addNote(new Note("my second note"));
    session.save(user);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a simple example with QProgressDialog. So I have one
Trying to make simple minesweeper game in python, but have one problem. I have
I'm trying to make work the example from hibernate reference. I've got simple table
I'm trying to just make a simple asyncore example where one socket is the
Have been struggling all day trying to make this simple example work using socket.io.
I m trying to make a simple fragment example where one fragment will show
So Im trying to make a simple sticky-note thingy using localstorage where you can
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a simple program to catalogue books. Something like this, for example:
I'm trying to make simple cms; the code below is index.php. Now I can't

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.