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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:31:36+00:00 2026-05-27T21:31:36+00:00

I am trying to setup my hibernate application to persist a new Notification entity

  • 0

I am trying to setup my hibernate application to persist a new Notification entity everytime an Activity Entity is created – at the moment, everything i have tried the Notification just fails to persist silently (no errors in the logs but sql is never executed).

Can anyone confirm that it is even possible to persist additional entities in the Hibernate pre/postPersist listeners?

I have read in the documentation:

A callback method must not invoke EntityManager or Query methods!

But I have read several other discussion threads that seem to indicate that it is possible.

For reference, the two approaches I have attempted are:

  1. @PrePersist method – setting a cascade.ALL relationship between Activity and Notification, and in the PrePersist method simply creating a new Notification and linking it to the Activity being created in a hope that the Notification would be persisted.

  2. @PostPersist method – using @Configurable and a ListenerClass, wiring in a service and creating a new Notification entity and then explicitly calling the entityManger persist

Can someone confirm what I am trying is possible?

  • 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-27T21:31:37+00:00Added an answer on May 27, 2026 at 9:31 pm

    Why do you have to persist Notification in @PrePersist or @PostPersist function? The following code should persist both entities:

    @Entity
    public class Activity implements Serializable {
       @OneToOne(cascade={CascadeType.PERSIST})
       private Notification notification;
    }
    
    @Entity
    public class Notification implements Serializable { }
    
    @Stateless
    public class MrBean implements MrBeanInterface {
       @PersistenceContext()
       private EntityManager em;
    
       public void persistActivity() {
          Activity act = new Activity();
          act.setNotification(new Notification());
          em.persist(act);
       }
    }
    

    UPDATE: you can try creating the link inside the Activity’s constructor like this:

    @Entity
    public class Activity implements Serializable {
       @OneToOne(cascade={CascadeType.PERSIST})
       private Notification notification;
    
       public Activity() {
          this.notification = new Notification();
       }
    }
    
    @Entity
    public class Notification implements Serializable { }
    
    @Stateless
    public class MrBean implements MrBeanInterface {
       @PersistenceContext()
       private EntityManager em;
    
       public void persistActivity() {
          Activity act = new Activity();
          em.persist(act);
       }
    }
    

    One thing to note is that I think you cannot use @PostPersist. To be more precise, you must link Notification to Activity before persisting Activity in order for cascade={CascadeType.PERSIST} to work.

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

Sidebar

Related Questions

I'm trying to setup Spring using Hibernate and JPA, but when trying to persist
I have a Grails 1.3.7 application and am trying to setup log4j for production
I'm trying to setup my hibernate entity to auto create the table IFAS_util_max_object_v in
I'm trying to setup CruiseControl.net at the moment. So far it works nice, but
I'm trying to setup a TransactionManager in my Web Application (powered by Spring MVC
I'm trying to setup a Postgres trigger in my Hibernate mapping file, but it
I'm trying to set up a project using hibernate.I have two tables : Users
I am trying to setup SpringMVC 3.0 + Hibernate + MySql with Maven. Having
I'm trying to do an initial setup with Hibernate in Eclipse, while deploying with
I have what I would assume is a pretty standard setup...spring, jpa (hibernate) 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.