1 2 3 Previous Next 37 Replies Latest reply on Aug 7, 2016 9:14 AM by royibernthal

    Setting up Hibernate

    royibernthal

      From what I understand Hibernate comes with Wildfly, correct me if I'm wrong.

      Is there anything I should do to set up Hibernate in my Eclipse project assuming I'm using Wildfly?

       

      Imports from hibernate cannot be resolved, e.g. "The import org.hibernate.HibernateException cannot be resolved".

       

      Should I follow a tutorial such as this to download and setup hibernate?

      http://www.tutorialspoint.com/hibernate/hibernate_environment.htm

       

      If so, how does it help that Hibernate comes with Wildfly by default? (assuming that's true)

       

      As you can guess I'm not only new to Wildfly but also to developing projects in the Java environment, so apologies if my questions are a bit "noobish".

        • 1. Re: Setting up Hibernate
          ctomc

          yes hibernate comes by default with WildFly.

           

          What is your use case? you want to use hibernate directly or via JPA?

          • 2. Re: Setting up Hibernate
            royibernthal

            I need it for JPA, frankly I don't even know what using it directly means.

            • 3. Re: Setting up Hibernate
              ctomc

              directly means you would be using classes from org.hibernate package.

              Such as you tried in your original question and you got class not found exception.

               

              If you are using JPA than just configure JPA with persistence.xml and that is it.

              Server will take care of setting up jpa properly by using hibernate as backend implementation.

               

              JPA is abstraction level over hibernate (or other jpa providers) so you don't really need any access to hibernate classes.

              you just use jpa api and configuration and that is it.

              • 4. Re: Setting up Hibernate
                royibernthal

                Wouldn't I need to use its classes like in this tutorial under "Create Application Class":

                http://www.tutorialspoint.com/hibernate/hibernate_annotations.htm

                 

                If not, how will I do db-related operations?

                 

                Regarding persistence.xml, do I simply create a persistence.xml file in the root of my project with content similar to what's in:

                1.2.1. The persistence.xml file

                • 5. Re: Setting up Hibernate
                  royibernthal

                  ctomc Tagging you in case you missed my reply.

                  • 6. Re: Setting up Hibernate
                    royibernthal

                    Does anyone else happen to know the answers to my questions? They should be pretty simple and straightforward no?

                    • 7. Re: Setting up Hibernate
                      mayerw01

                      As mentioned by Tomaz you will have to decide how to access the database (Hibernate APIs, JPA, JDBC or any other technologie).

                      Your example is using Hibernate directly. The access via JPA is usually done via an Entity Manager.

                      But the related documentation will provide a deeper understanding.

                      • 8. Re: Setting up Hibernate
                        royibernthal

                        I understand, I'm trying to access via an Entity Manager now instead of using Hibernate directly.

                         

                        An error is thrown:

                        org.jboss.resteasy.spi.UnhandledException: javax.persistence.PersistenceException: Unable to build entity manager factory

                         

                        I tried looking for an answer, but there seem to be different answers for different setups.

                         

                        Here's my setup, hopefully you'll be able to identify the problem:

                         

                        persistence.xml

                         

                        In this file I added references to org.hibernate even though I shouldn't be able to access it if I understood you correctly, perhaps that's the issue. The result is the same when omitting those references.

                         

                        <?xml version="1.0" encoding="UTF-8"?>
                        
                        <persistence version="2.0" 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">
                        
                           <persistence-unit name="Hibernate_JPA" transaction-type="RESOURCE_LOCAL">
                        
                              <provider>org.hibernate.ejb.HibernatePersistence</provider>
                        
                              <class>entities.User</class>
                        
                              <properties>
                                 <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
                                 <property name="javax.persistence.jdbc.user" value="root"/>
                                 <property name="javax.persistence.jdbc.password" value="root"/>
                                 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
                                 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
                              </properties>
                        
                           </persistence-unit>
                        </persistence>
                        
                        
                        
                        
                        
                        

                         

                         

                        entities.User.java

                         

                        package entities;
                        
                        import javax.persistence.*;
                        
                        @Entity
                        @Table(name = "users")
                        public class User {
                        
                           @Id @GeneratedValue
                           @Column(name = "id")
                           private int id;
                        
                        
                           @Column(name = "name")
                           private String name;
                        
                           public User() { }
                        
                           public String getName() {
                              return name;
                           }
                        
                           public void setName(String value) {
                              this.name = value;
                           }
                        
                        }
                        
                        
                        
                        
                        
                        

                         

                         

                        entities.Users.java

                         

                        package entities;
                        
                        import javax.persistence.EntityManager;
                        import javax.persistence.EntityManagerFactory;
                        import javax.persistence.Persistence;
                        
                        public class Users {
                        
                          private EntityManagerFactory emfactory;
                          private EntityManager entitymanager;
                        
                          public Users() {
                               emfactory = Persistence.createEntityManagerFactory("Hibernate_JPA");
                               entitymanager = emfactory.createEntityManager();
                          }
                        
                          public String getUserName() {
                               return entitymanager.find(User.class, 2).getName();
                          }
                        
                        }
                        
                        
                        
                        
                        
                        
                        • 9. Re: Setting up Hibernate
                          mayerw01

                          Before using JPA I would suggest to read the documentation. I personally still think that the Java EE Tutorial (Java Platform, Enterprise Edition: The Java EE Tutorial Release 7 - Contents) will provide a good understanding of the technology.

                          The JPA API documentation (The Java Community Process(SM) Program - communityprocess - final) contains also a lot of details.

                          Chapter 3.1 describes the EntityManager. In 3.1.2 Example of Use of EntityManager API you will find an example:

                           

                          @Stateless

                          public class OrderEntryBean implements OrderEntry {

                             @PersistenceContext

                             EntityManager em;

                             public void enterOrder(int custID, Order newOrder) {

                               Customer cust = em.find(Customer.class, custID);

                               cust.getOrders().add(newOrder);

                               newOrder.setCustomer(cust);

                               em.persist(newOrder);

                             }

                          }


                          Chapter 7.2 (Obtaining an EntityManager) explains when and how you should use the EntityManagerFactory interface instead.

                          • 10. Re: Setting up Hibernate
                            royibernthal

                            I definitely intend to dig deeper into JPA docs and tutorials, but at the moment I'm trying to get the simplest example to work so I can see my setup is correct.

                             

                            I downloaded the JPA API documentation as you suggested and read Chapters 7.1-7.3.

                            I'm not entirely sure I understood when I need EntityManagerFactory and when I don't, for lack of familiarity with the environment and terminology.

                             

                            With that said, even when I tried the following code, a NullPointerException was thrown, which if I understand correctly means the entity manager was not injected.

                             

                            package entities;
                            
                            import javax.persistence.EntityManager;
                            import javax.persistence.PersistenceContext;
                            
                            public class Users {
                            
                              @PersistenceContext
                              private EntityManager entitymanager;
                            
                              public Users() { }
                            
                              public String getUserName() {
                                   return entitymanager.find(User.class, 2).getName();
                              }
                            
                            }
                            

                             

                            org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
                            • 11. Re: Setting up Hibernate
                              mayerw01

                              I think you missed the @Stateless annotation in your Users class.

                              The transaction-type should be JTA when deploying to WildFly. So the persistence.xml could be quite simple like

                               

                              <?xml version="1.0" encoding="UTF-8"?>

                              <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"

                                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                                         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

                                <persistence-unit name="TestUsersPU" transaction-type="JTA">

                                  <jta-data-source>java:/jboss/datasources/test</jta-data-source>

                                  <exclude-unlisted-classes>false</exclude-unlisted-classes>

                                  <properties/>

                                </persistence-unit>

                              </persistence>

                               

                              The entry in <jta-data-source> should exist in WildFly

                              • 12. Re: Setting up Hibernate
                                royibernthal

                                I omitted the @Stateless annotation on purpose, as I'm trying to create an instance of Users in another class. Is that annotation a must for some reason?

                                 

                                What should I specify in jta-data-source?

                                 

                                Did you omit the properties on purpose? If so, how do I specify connection details?

                                 

                                <properties>

                                        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>

                                        <property name="javax.persistence.jdbc.user" value="root"/>

                                        <property name="javax.persistence.jdbc.password" value="root"/>

                                        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>

                                </properties>

                                 

                                Did you omit the class on purpose? If so, why? Is setting exclude-unlisted-classes to false the equivalent of listing all classes?

                                 

                                <class>entities.User</class>

                                • 13. Re: Setting up Hibernate
                                  mayerw01

                                  The data source should be configured in WildFly (Subsystems -> Datasources -> Non-XA -> Add)

                                  There you should also configure the Url as well as the credentials. Please remember that you should configure the driver before setting up the data source.

                                   

                                  exclude-unlisted-classes

                                  Do not check the main jar file for annotated classes. Only explicit classes will be part of the persistence unit.

                                  The exclude-unlisted-classes was defaulted to false in JPA 1.0 (actually this was inserted automatically by my NetNeans).

                                  But I understand this has no effect ([HHH-8364] change the "exclude-unlisted-classes" behavior in Java SE environments - Hibernate JIRA)

                                  • 14. Re: Setting up Hibernate
                                    royibernthal

                                    How do I configure the driver?

                                     

                                    Does this article cover mySQL driver? Should I follow it?

                                    https://docs.jboss.org/author/display/WFLY8/DataSource+configuration

                                     

                                    So it's safe to omit exclude-unlisted-classes, right?

                                     

                                    Also, if I understand correctly I should still explicitly list classes as follows:

                                    <class>entities.User</class>

                                    Correct?

                                    1 2 3 Previous Next