Last updated by mingfai 5 years ago
link JPA Plugin

The JPA Plugin Provides many different combination of configurations.

Overviews

There are many different ways to configure JPA and JDBC for the plugin. The following is a summary:
  1. Which JPA Vendor you want to use?
  2. Where you want to configure your data source / JDBC properties?
    • reference to a data source in Spring (recommended)
      • configure the data source in Grails' Official Way (recommended)
      • configure the data source with a Grails Bean Builder DSL in resources.groovy
      • configure the data source in Spring XML in resources.xml (or included xml)
    • define as Spring JPA properties
    • define as persistence.xml properties
remarks: You could define a JNDI data source via Spring easily.

Configuration

  • The plugin uses the main Grails /grails-app/conf/Config.groovy
  • For JPA configuration, the general structure is
    plugins{
     jpa{
      persistenceUnits{
        firstPersistenceUnitName{
          'persistenceUnit' { //stuff that will be generated to persitence.xml
              provider = ''
              classes = ['**']
              properties = [
                'hibernate.connection.url': ''
              ]
            }
            'spring' {//stuff for adding the Spring EMF bean
              id = 'entityManagerFactory'
            }
        }
        secondPersistenceUnitName{
        }
      }
     }
    }
    
  • You may configure zero or more node under 'persistenceUnits'
    • If no persitence unit configuration, a defaultPersistenceUnit with an entity manager factory bean named 'entityManagerFactory' will be created
    • If configured, each 'persietenceUnit' will be read to create persistence unit in persistent.xml, and create a corresponding EntityManagerFactory bean in spring.
    • See the example Config.groovy
  • More Notes
    • You may either provide the JDBC information in property, or use data source. The default EMF uses the default data source bean named 'dataSource'
    • NOTICE If you use JPA Properties to define JDBC information, you need to set the Spring dataSource to false. Otherwise, this plugin will intelligently inject the default data source for your EMF

Examples

  • Hibernate-Persistence.xml-MySQL
    • Remember to put the mysql jdbc to your classpath
    • Config.groovy
      plugins {
        jpa {
          'persistenceUnits' {
            'mysqlDb' {
              'persistenceUnit' {
                //transactionType = 'RESOURCE_LOCAL' //default, optional to define
                //provider = 'org.hibernate.ejb.HibernatePersistence'//default, optional to define
                classes = ['/**'] // change to your class
                properties = [
                        'hibernate.connection.url': 'jdbc:mysql://dbHost:3306/dbName',
                        'hibernate.connection.driver_class': 'com.mysql.jdbc.Driver',
                        'hibernate.dialect': 'org.hibernate.dialect.MySQL5InnoDBDialect',  // or org.hibernate.dialect.MySQLDialect if not innodb
                        'hibernate.hbm2ddl.auto': 'create-drop',
                        'hibernate.connection.username': 'dbUser',
                        'hibernate.connection.password': 'dbPassword'
                ]
              }
              'spring' {//stuff for adding the Spring EMF bean
                id = 'entityManagerFactory'
                dataSource = false //required when u use JPA properties to define JDBC info
              }
            }
          }
        }
      }