Here have the getSessionFactory method with the default bootstrap in the user guide, works perfect in the Standard Edition environment:
Code:
public class HibernateUtil {
private static StandardServiceRegistry standardServiceRegistry;
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null)
try {
Map<String, Object> settings = new HashMap<>();
settings.put(DRIVER, "com.mysql.jdbc.Driver");
settings.put(URL, "jdbc:mysql://localhost:3306/blomster_db");
settings.put(USER, "root");
settings.put(PASS, "root");
settings.put(HBM2DDL_AUTO, "update");
settings.put(SHOW_SQL, true);
settings.put(FORMAT_SQL, true);
standardServiceRegistry = new StandardServiceRegistryBuilder()
.applySettings(settings)
.build();
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
metadataSources.addAnnotatedClass(Person.class);
Metadata metadata = metadataSources.getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch (Exception e) {
shutdown();
}
return sessionFactory;
}
public static void shutdown() {
if (standardServiceRegistry != null)
StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
}
}
I have one hibernate.cfg.xml, works perfect in the Standard Edition environment to:
Code:
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/blomster_db</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="tasl.thiagoapp.domain.entity.Person"/>
</session-factory>
</hibernate-configuration>
When I move to the Enterprise Environment, i make that simple, one index.xhtml with one button, the action you can see below:
Code:
@Named
@RequestScoped
public class PersonJSF {
@EJB
private PersonService personService;
public void createPerson() {
personService.create();
}
}
I've created one DataSource for the persistence.xml using IntelliJ and the persistence.xml looks:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="taslPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>blomster_db@localhost</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Below, the 'logic':
Code:
@ApplicationScoped
public class SessionProducer {
@PersistenceUnit
private SessionFactory sessionFactory;
@RequestScoped
@Produces
public Session createSession() {
return sessionFactory.openSession();
}
public void destroySession(@Disposes Session session) {
if (session.isOpen())
session.close();
}
}
The same above have been made for the EntityManager, and is good because I don't get this error:
Code:
[2017-11-20 11:42:51,466] Artifact ThiagoApp:Web exploded: Error during artifact deployment. See server log for details.
[2017-11-20 11:42:51,466] Artifact ThiagoApp:Web exploded: java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while deploying the app [ThiagoApp_Web_exploded] : JNDI lookup failed for the resource: Name: [java:module/env/tasl.thiagoapp.domain.view.PersonJSF/personService], Lookup: [tasl.thiagoapp.domain.view.PersonService#tasl.thiagoapp.domain.view.PersonService], Type: [Session]. Please see server.log for more details.
The entire problem is here, the TransactionAttribute doesn't makes difference yet because nothing is working.
The EntityManager works really well, really do... it's working!!!!!! No it's not____ nothing happens in the database. PersistenceUnit, PersistenceContext... nothing happens.
The error above happens when I try this:
Code:
@Stateless
@TransactionAttribute(REQUIRED)
public class PersonService {
// @PersistenceContext
// private EntityManager entityManager;
@PersistenceContext
private Session session;
public void create() {
Person person = new Person("Ligia");
// entityManager.persist(person);
session.save(person);
}
}
I've lost my entire sunday trying this. I'll take some rest now and do another thing, if you could give me some tip I'll accept thanks!