Hi, I have a project where I bootstrap Hibernate programatically using its native API (SessionFactory).
Code:
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)
Code:
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.
Code:
Session session1 = sessionFactory.openSession();
Bean bean = session1.get(Bean.class, 1);
session1.close();
Session session2 = sessionFactory.openSession();
session2.delete(bean);
The code above works when I bootstrap with native API, and doesn't work when I bootstrap with JPA API, raising this exception:
Code:
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?
Regards,
Fabiano