[2.0-java] Play2 + JPA + Hibernate / AUTO-CREATE or AUTO-UPDATE of tables

1,579 views
Skip to first unread message

brainwatch

unread,
Jun 21, 2012, 4:11:33 AM6/21/12
to play-fr...@googlegroups.com
Hi Players

I have ported application from 1.2.4 to 2.0.1 without issues except JPA + Hibernate integration. I have multiple issues to resolve, but will split the issues as different posts for future reference.

I'm not able to achieve auto-create or auto-update of table structure from entity models. I'm using hibernate.hbm2ddl.auto property with create/update (tried both) values, but no success.

1. conf/META-INF/persistence.xml
------------------------

<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="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>        
         </properties>
    </persistence-unit>
   
</persistence>

2. application.conf
---------------------------

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"

# You can expose this datasource via JNDI if needed (Useful for JPA)
 db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit

# Evolutions
# ~~~~~
# You can disable evolutions if needed
# evolutionplugin=disabled

# Ebean configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
#
# ebean.default="models.*"

And have entity models.

Is there something that I'm missing to enable the auto-generate behavior ?

Guillaume Bort

unread,
Jun 21, 2012, 10:32:08 AM6/21/12
to play-fr...@googlegroups.com
I don't think so, it should work this way. Try to ask the hibernate
mailing list.
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/e1H-qL4WveIJ.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.



--
Guillaume Bort, http://guillaume.bort.fr

Martín Bigio

unread,
Jun 21, 2012, 11:06:00 AM6/21/12
to play-fr...@googlegroups.com
I had the same problem a few days ago. Anyway the
hbm2dd.auto=create/update only is used for development (for deploying
you'll need the actual queries Hibernate executes), so I've wrote a
small program that creates hibernate Configuration object and then
dumps the update script you should include in evolutions/N.sql.

package test;
import java.util.Properties;

import models.Comment;
import models.Hotel;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;

public class ExportSchema {

public static void main(String[] args) {
// TODO: configure log4j so that it doesnt't log hibernates info messages
// BasicConfigurator.configure();
Configuration conf = new Configuration();
conf.addAnnotatedClass(Hotel.class);
conf.addAnnotatedClass(Comment.class);
Properties hibernateProperties = new Properties();
// TODO: read some of these properties from application.conf
hibernateProperties.put("hibernate.hbm2ddl.auto", "");
hibernateProperties.put("hibernate.show_sql", "true");
hibernateProperties.put("hibernate.dialect",
"org.hibernate.dialect.PostgreSQLDialect");
hibernateProperties.put("hibernate.connection.driver_class",
"org.postgresql.Driver");
hibernateProperties.put("hibernate.connection.url",
"jdbc:postgresql://localhost/HotelApp");
hibernateProperties.put("hibernate.connection.username", "rbigio");
hibernateProperties.put("hibernate.connection.password", "rbigio");
conf.setProperties(hibernateProperties);

SchemaUpdate update = new SchemaUpdate(conf);
// to system.out, but not to the database
update.execute(true, false);
}
}

You could with some small changes enhance this class to fetch all your
entities from your models package.

Hope this helps you,
Martín
Reply all
Reply to author
Forward
0 new messages