These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]

Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate does not delete detached entities via JPA
PostPosted: Mon Dec 12, 2016 6:24 am 

Joined: Mon Dec 12, 2016 5:53 am
Posts: 1
Hi, I have a project where I bootstrap Hibernate programatically using its native API (SessionFactory).

Configuration configuration = new Configuration();
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Now, I am changing the project to bootstrap Hibernate using JPA API (EntityManagerFactory)

PersistenceUnitInfoConcreto persistenceUnitInfo = new PersistenceUnitInfoImpl();
EntityManagerFactoryBuilder entityManagerFactoryBuilder = new EntityManagerFactoryBuilderImpl(new PersistenceUnitInfoDescriptor(persistenceUnitInfo), map);
EntityManagerFactory entityManagerFactory = entityManagerFactoryBuilder.build();
SessionFatory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);

(I am still using a SessionFactory with JPA for a while, until I can replace all SessionFactory occurrences in my application).

I am able to bootstrap and configure Hibernate in both cases, but when I run my test cases, I noticed there is a change in behavior between them that is producing some exceptions with JPA.

Session session1 = sessionFactory.openSession();
Bean bean = session1.get(Bean.class, 1);

Session session2 = sessionFactory.openSession();

The code above works when I bootstrap with native API, and doesn't work when I bootstrap with JPA API, raising this exception:

java.lang.IllegalArgumentException: Removing a detached instance br.com.personalsoft.wms.bean.pre_entrada_item_recebimento.PreEntradaItemRecebimento#3f8a64a9-0233-43aa-af6d-e77858f8f829

I understand what this exception mean and I see it is right, but the fact is that I was able to delete and update detached instances when bootstrapping with native API, and there are many places in my application relying on this, and it will take some time to fix all them.

So my questions are:

1) Is this change in behavior expected? Does Hibernate follows stricter JPA specs when bootstrapping with JPA?

2) Is it possible to make JPA to behave like native API, allowing to delete or update detached instances without the need to merge them previously, so I can get extra time to fix these spots in my application?



 Post subject: Re: Behavior change when bootstrapping Hibernate via JPA
PostPosted: Mon Dec 12, 2016 7:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
1. That's true. JPA is more strict and does not allow deleting detached entities. So if you bootstrap via JPA, you inherit all limitations imposed by JPA. However, you can open a new Jira issue so taht we should change this behavior if a certain property like: "hibernate.allow_remove_detached_entity" is being supplied.
2. See above. We could add this behavior if we have consensus within the Development team.

If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.

Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.