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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:25:56+00:00 2026-05-31T01:25:56+00:00

I have a general question about Hibernate that I’m batteling with. I have class

  • 0

I have a general question about Hibernate that I’m batteling with.

I have class A and class B, where B is dependent on A

In my code when I call em.persist(objOfTypeA) I would expect the insert to go and insert into both tables AAA and BBB. If I manually presist A get A’s ID and fill it in the list for each object and then persist that list, things are working.
But I would expect this to happen magically by Hibernate.

Am I doing something wrong?
Or am I just expecting too much of Hibernate?

Thanks

@Entity
@Table(name = "AAA")
@Veto
public class A {

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


    @NotNull
    @Column(name = "Name")
    private Long name;

    ...

    @OneToMany(mappedBy="a", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<B> b;
...
}

@Entity
@Table(name = "BBB")
@Veto
public class B {

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

    @NotNull
    @Column(name="AId")
    private Long aId;

    @NotNull
    @Column(name = "Name")
    private Long name;

    @JoinColumn(name = "AId", referencedColumnName="Id", updatable = false, insertable = false)
    @ManyToOne(optional = false)
    private A a;
...     
}
  • 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-31T01:25:57+00:00Added an answer on May 31, 2026 at 1:25 am

    I finally worked it out.
    None of the examples I was given or found told me exactly what was wrong, it was only after my own experimentation that I managed to come to this conclusion:

    Instead of having

    @OneToMany(mappedBy="a", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<B> b;
    

    that didn’t work (FK was still coming as NULL)

    This did work:

    @OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    @JoinColumn(name="AId")
    @NotNull
    private List<B> b;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a general question about the way that database indexing works, particularly in
I have a general question on the class definition and its use..THe below code
I have a general question about debugging a .NET process that has been built
This is a general question about writing web apps. I have an application that
I have a general question about rails controllers/models: I have a model Providers, that
I have general question about designated initializer. I have a some class and from
Hi I have a general question about Lists in C#. Here is my Code:
I have a general question about jQuery. Lets say I have the following code:
I have a general question about interpreters of functional languages: Are there actually any
I have a general question about the new Django 1.3 static file framework. I

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.