Hi all! I was trying to run this simples code using Hibernate 3.6.0:
Code:
public class TestEMF {
public static void main(String[] args) {
EntityManager em = new JPAUtil().getEntityManager();
Account a = new Account();
a.setOwner("Test1");
em.getTransaction().begin();
em.persist(a);
em.getTransaction().commit();
em.close();
System.out.println(JPAUtil.entityManagerFactory.isOpen());
JPAUtil.entityManagerFactory.close();
Account a2 = new Account();
a2.setOwner("Test2");
EntityManager em2 = new JPAUtil().getEntityManager();
em2.getTransaction().begin();
em2.persist(a2);
em2.getTransaction().commit();
System.out.println(JPAUtil.entityManagerFactory.isOpen());
}
}
I thought that according to JPA2 this code was supposed to throw an exception, like it is described here:
"Get the properties and associated values that are in effect
* for the entity manager factory. Changing the contents of the
* map does not change the configuration in effect.
* @return properties
* @throws IllegalStateException if the entity manager factory
* has been closed"
(JPA2 pdf specification page 290 -
http://download.oracle.com/otn-pub/jcp/ ... 838dec6ba4 )
Unfortunately, when you run this code no exception is thrown. My persistence.xml:
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="money">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>br.com.model.Account</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/accounts"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence>
And my JPAUtil class:
Code:
public class JPAUtil {
public static EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("money");
public EntityManager getEntityManager(){
return entityManagerFactory.createEntityManager();
}
}
Any thoughts?
Thanks a lot!