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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:00:24+00:00 2026-05-15T08:00:24+00:00

I’ve mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship

  • 0

I’ve mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship (I’ve simplified for presentation):

@Entity
public class A {

    @ManyToOne
    public B getB() { return b; }
}

@Entity
public Class B {

    @OneToMany(mappedBy="b")
    public Set<A> getAs() { return as; }
}

Now, I’m trying to create a relationship between two instances of these entities by using the setter of the one-side/not-owner-side of the relationship (i.e the table being referenced to):

em.getTransaction().begin();

A a = new A();
B b = new B();
Set<A> as = new HashSet<A>();
as.add(a);
b.setAs(as);

em.persist(a);
em.persist(b);
em.getTransaction().commit();

But then, the relationship isn’t persisted to the DB (the row created for entity A isn’t referencing the row created for entity B). Why is it so? I’d excpect it to work.

Also, if I remove the “mappedBy” property from the @OneToMany annotation it will work. Again – why is it so? and what are the possible effects for removing the “mappedBy” property?

  • 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-15T08:00:24+00:00Added an answer on May 15, 2026 at 8:00 am

    In bidirectional associations the JPA spec is defined such that the implementation only looks at the owning side of the association when it wants to see the current “state” of the association to determine what needs to be persisted and each bidirectional association has both an owning and an inverse side. This can avoid ambiguities (i.e. what to persist if both sides of the association are not consistent?) as well as give opportunities for optimizations and simpler implementations in the JPA implementors. Note that this is generally not a problem, because bidirectional associations should be maintained properly by you, not by JPA. When you always maintain bidirectional associations properly in your application (updating both sides and keeping them consistent), there is no problem.

    OneToMany with mappedBy is an inverse side, hence the JPA impl does not look at this side when determining the state of the association on flush/transaction commit. It only looks at A.getB() and this is null, so the association is null for JPA.

    OneToMany without mappedBy, so that is becomes the owning side, is only supported since JPA 2.0 but I think Hibernate supports it since ages. Thats why your example works if you remove mappedBy from OneToMany. In that case the OneToMany becomes the owning side and hence the implementation “looks at” this side to determine what to persist.
    This does not change the fact that your in-memory association is still incomplete. You should set both sides.

    UPDATE: I don’t know exactly what Hibernate does when you leave off the mappedBy from either side but I think it may result in less optimal SQL. See also: http://simoes.org/docs/hibernate-2.1/155.html , particularly the section about “inverse=”false””. “inverse” is the native Hibernate term for JPA “mappedBy”. That is, inverse=”true” in a Hibernate mapping is the same as using mappedBy=”other” in a JPA mapping, both mark this side as the inverse side that is ignored when determining updates to the association.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.