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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:27:03+00:00 2026-05-28T00:27:03+00:00

I have a weird error of double insert in the DB. I have the

  • 0

I have a weird error of double insert in the DB. I have the following classes:

  • TestEntity – entity with a @PrePersist and a @PostPersist methods.
  • Auditoria – audit entity
  • Dataset – interface of DatasetBean
  • DatasetBean – Stateless bean which implements Dataset
  • DatasetFactory – instance an EJB of Dataset (lookup)

I put the problem in a junit test (I’m using embedded Glassfish):

@Test
public void test() throws NamingException {
    Dataset<TestEntity> dataset = this.lookupBy(DatasetBean.class);
    Assert.assertNotNull(dataset);

    TestEntity t = new TestEntity();        
    t.setName(UUID.randomUUID().toString());

    dataset.insert(t);
    System.out.println("end");
}

The flow of the test is the following:

  1. After gettin a Dataset object, I try to insert a TestEntity object

    @Stateless
    @EJB(name = “…”, beanInterface = Dataset.class)
    public class DatasetBean implements Dataset {

    @PersistenceContext(type = PersistenceContextType.TRANSACTION)
    private EntityManager entityManager;
    
    @Override
    public void insert(T entidade) {
        LOG.info("Inserting: " + entidade);
        entityManager.persist(entidade);
    }
    //...
    

    }

  2. Using the DatasetFactory, I try to insert an Auditing entity in a @PostPersist method of the TestEntity

    public class DatasetFactory {
    public static Dataset createDataset() {
    try {
    return (Dataset) new InitialContext().lookup(“…”);
    } catch (Exception ex) {
    throw new RuntimeException(ex);
    }
    }
    }

    @Entity
    public class TestEntity implements MyEntity {
    @Id
    private Integer id;
    private String name;
    // sets and gets

    @PrePersist
    public void fillId() {
        if (getId() == null || getId() == 0) {
            Dataset d = DatasetFactory.createDataset();
            Integer i = (Integer) d.fetchJPQLFirstResult("SELECT MAX(te.id) FROM TestEntity te");
            if (i == null || i < 100) {
                setId(100);
            } else {
                setId(i + 1);
            }
        }
    }  
    
    @PostPersist
    public void audit() {
        Dataset<Auditing> dataset = DatasetFactory.createDataset();
        // dataset.getEntityManager().clear();
        Auditing auditing = new Auditing();
        auditing.setIdEntidade(String.valueOf(this.getId()));
        dataset.insert(auditing);
    }
    

    }

    @Entity
    public class Auditoria implements MyEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String idEntity;
    //sets and gets
    }

    public interface MyEntity extends Serializable {
    Integer getId();
    }

Log:

INFO: embedded was successfully deployed in 47.154 milliseconds.
PlainTextActionReporterSUCCESSDescription: deploy AdminCommandApplication deployed with name embedded.

2012-01-06 02:56:54,826 [main] INFO com.joaosavio.model.db.DatasetBean (DatasetBean.java:30) – Inserting: TestEntity{id=null, name=ea5c2af4-0ca7-48a2-a82a-dbf582c570a9}

Hibernate: select max(testentity0_.id) as col_0_0_ from TestEntity testentity0_

Hibernate: insert into TestEntity (name, id) values (?, ?)

2012-01-06 02:56:56,344 [main] INFO com.joaosavio.model.db.DatasetBean (DatasetBean.java:30) – Inserting: Auditoria{id=null, idEntidade=100}

Hibernate: insert into TestEntity (name, id) values (?, ?)

2012-01-06 02:56:56,350 [main] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper (SqlExceptionHelper.java:143) – SQL Error: 2627, SQLState: 23000

2012-01-06 02:56:56,352 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper (SqlExceptionHelper.java:144) – Violation of PRIMARY KEY constraint ‘PK_TestEntity_76818E95′. Cannot insert duplicate key in object ‘dbo.TestEntity’.

06/01/2012 02:56:56 com.sun.ejb.containers.BaseContainer postInvoke

WARN: A system exception occurred during an invocation on EJB DatasetBean method public void com.joaosavio.model.db.DatasetBean.insert(java.lang.Object)
javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
…

Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Violation of PRIMARY KEY constraint ‘PK_TestEntity_76818E95′. Cannot insert duplicate key in object ‘dbo.TestEntity’.
…

Caused by: org.hibernate.exception.ConstraintViolationException: Violation of PRIMARY KEY constraint ‘PK_TestEntity_76818E95′. Cannot insert duplicate key in object ‘dbo.TestEntity’.

Considerations:

Relying on the fact that everything works fine if I clear the entity manager before the insertion of the Auditing entity (comented code in @PostPersist method in TestEntity), I believe that the TestEntity is getting stuck in the transaction.

What am I doing wrong???

  • 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-28T00:27:04+00:00Added an answer on May 28, 2026 at 12:27 am

    Ive seen a very similar issue once…. You should —

    Be very careful with @PostPersist ! hibernate bean persist or save actions ARE NOT the same as database Insertions!

    The problem is likely that are that you are assuming that @PostPersist methods are invoked after the data has been inserted …. However , that is not always the case! PostPersist methods are callbacks BUT they are not callbacks from the database !!! As you know – hibernate may not have committed your transaction And flushed it completely. if you try to use PostPersist to coordinate barriers between your database transactions, you are making a mistake.

    The solution is to do all your inserts in a single properly planned and managed transaction, with keys and cascades worked out so that hibernate will be able to organize the inserts for you in the right way— or just hardcode a stored procedure to do the work for you.

    I think you might be merging stateful and stateless transaction logic here.

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

Sidebar

Related Questions

I have a weird error in my C++ classes at the moment. I have
I have just got weird error which involves protected modifier. I have following code:
So, I have this very weird error - Some advanced PHP devs might also
I am facing a weird error.I have set up my code in IntelliJ Idea.
I have come across a very weird error. I'm on Solaris 10, using Ruby
Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
I have a weird error, I am not able to find a force close
Hey guys I have a weird error I wrote this code against setbubblepopup $(document).ready(function
I've got a weird linker issue. I have code that looks like so: double
I have a weird error in my code. I'm trying to read some characters

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.