0

On work, im using Eclipse, Jboss and Hibernate JPA. For a smaller, private project I like to use Netbeans, GlassFish and Hibernate JPA.

Problem: I want hibernate to generate the tables automagically - but it won't do that for me.

What i did:

  • First, i installed - obvious is obvious - Netbeans, Glassfish and a local MySQL-DB.
  • I created a JDBC-Connection for Glassfish:

    url: jdbc:mysql://localhost:3306/myDatabase?zeroDateTimeBehavior=convertToNull

    name: myDatabaseJDBC

    Driver: com.mysql.jdbc.Driver

The connection seems fine, "testing" it resolved to a successful connection.

  • Now i created the persistance.xml like this:

    <?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="primary" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>myDatabaseJDBC</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
    

  • Then i added the required depoendencies to my Project (using maven) and the hibernate Plugin to Glassfish

What works: When ive created a table, in Netbeans i can Select New -> Other -> Persistance -> Entity Class from Database. The connection shows the tables, i select one, click okay, and i got the entity.

However i usually work the other way round and let hibernate generate my tables from the created entities... That whoever won't work. (It even looks like Hibernate is not even invoked, when building the project)

Did I miss any configuration step?

update: -------------

I wanted to test if hibernate is "active" in any way. So i created a simple entity, a controller and deployed the application with a single button.

public void doSth() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("primary");
    EntityManager em = emf.createEntityManager();

    CEntity c = new CEntity();
    c.setName("Test");

    em.persist(c);
}

first, i received an exception:

Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:376)

Overhere: hibernate, mysql, glassfish v3, and JTA datasource i found the solution to add

// For GlassFish:
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.SunONETransactionManagerLookup

to the persistance.xml. The exception is now gone, but i received another one: Unknown Entity: CEntity.

I figured out, that hibernate can NOT find my entities. (Yes, i used javax.persistance.Entity and not the one from the hibernate namespace). However "adding" the entity manually to the persistance.xml solves the issue and also the automatic table-creation is invoked.

However, now im looking for the correct configuration, so adding every Entity to persistance.xml is NOT required.

I set <exclude-unlisted-classes>false</exclude-unlisted-classes> but hibernate seems to ignore that...

Community
  • 1
  • 1
dognose
  • 20,360
  • 9
  • 61
  • 107

1 Answers1

1

The key was to add:

<property name="hibernate.archive.autodetection" value="class"/>

to persistence.xml's properties-collection. After all:

<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="primary">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>myDatabaseJDBC</jta-data-source>

        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
            <property name="hibernate.archive.autodetection" value="class"/>
        </properties>
    </persistence-unit>
</persistence>
dognose
  • 20,360
  • 9
  • 61
  • 107