-->
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.  [ 6 posts ] 
Author Message
 Post subject: Using Hibernate with Java EE
PostPosted: Sun Nov 19, 2017 9:23 pm 
Newbie

Joined: Sun Nov 19, 2017 4:07 pm
Posts: 4
bernHi. I'm using the Hibernate 5.2.12.Final and works fine for me when used with the Standard Edition.
I've created the classic 'static getSessionFactory' and works really fine.

But the challenge now is learn all the Enterprise Edition environment, EJB, CDI, JSF, JPA & others.
So, if you could answer me, like the classic bootstrap configuration that i say above, I've seen that exists one classic EntityManagerProducer to.
Is one ApplicationScoped, which Produces (RequestScoped) and Disposes EntityManagers.

1. How can i use Session's instead of EntityManager's? Now 'Session = EntityManager' works, but where i put this? In one abstraction approach.
2. How to avoid transactions commit's and rollback's in try-catch block? Using Interceptors?
3. How the architecture really works, all these layers configurated properly? In one abstraction approach.
4. The StatelessSession exists for the Stateless bean? Why i can't assign it to one Session? One Collection supports different types such as List, Set... right?
5. I need to specify each entity class in my project? Of course not right? How can i eliminate this?

It's my first topic, I've already search for another answers about it, but I'm not really satisfied.
I'm using:
Maven 3.5;
GlassFish 5;
Hibernate 5.2.12.Final;
MySql Connector 5.1.44;
JEE 8;
Wish you could show to me one good configuration for one simple small business software. That's it.
Thanks for your attention.


Top
 Profile  
 
 Post subject: Re: Java Standard to Enterprise Edition Configuration
PostPosted: Mon Nov 20, 2017 2:44 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
1. Like this:

Code:
Session session = entityManager.unwrap( Session.class );


2. You don't have to. Just use transactional EJBs:

Code:
@TransactionAttribute(TransactionAttributeType.REQUIRED)


3. That's the goal of Java EE. You just have to focus on business logic.

4. The EJB Session is not the same as the Hibernate Session.

5. You need to specify each entity you need. Otherwise, how would the JPA provider know what database table or columns to use when persisting the entity?

Just read the documentation of the application server you want to use, and you will be fine.


Top
 Profile  
 
 Post subject: Re: Using Hibernate with Java EE
PostPosted: Mon Nov 20, 2017 10:08 am 
Newbie

Joined: Sun Nov 19, 2017 4:07 pm
Posts: 4
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!


Top
 Profile  
 
 Post subject: Re: Using Hibernate with Java EE
PostPosted: Mon Nov 20, 2017 11:16 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
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.


Of course, it's not working.

The Java SE bootstrap using JDBC DriverManager is totally different than the Java EE one.

In your case, the problem does not come from Hibernate, but from your Java EE configuration:

Quote:
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.


As I mentioned above, you should ask this question on the Application Server forum (GlassFish), rather than on the Hibernate one.


Top
 Profile  
 
 Post subject: Re: Using Hibernate with Java EE
PostPosted: Mon Nov 20, 2017 1:58 pm 
Newbie

Joined: Sun Nov 19, 2017 4:07 pm
Posts: 4
Thanks.


Top
 Profile  
 
 Post subject: Re: Using Hibernate with Java EE
PostPosted: Tue Nov 21, 2017 8:46 pm 
Newbie

Joined: Sun Nov 19, 2017 4:07 pm
Posts: 4
Nevermind. Sorry.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.