Auto generate data schema from JPA annotated entity classes - java

I'm using JPA (Hibernate's implementation) to annotate entity classes to persist to a relational database (MySQL or SQL Server). Is there an easy way to auto generate the database schema (table creation scripts) from the annotated classes?
I'm still in the prototyping phase and anticipate frequent schema changes. I would like to be able to specify and change the data model from the annotated code. Grails is similar in that it generates the database from the domain classes.

You can use hbm2ddl from Hibernate. The docs are here.

Generate create and drop script for given JPA entities
We use this code to generate the drop and create statements:
Just construct this class with all entity classes and call create/dropTableScript.
If needed you can use a persitence.xml and persitance unit name instead. Just say something
and I post the code too.
import java.util.Collection;
import java.util.Properties;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.dialect.Dialect;
import org.hibernate.ejb.Ejb3Configuration;
/**
* SQL Creator for Tables according to JPA/Hibernate annotations.
*
* Use:
*
* {#link #createTablesScript()} To create the table creationg script
*
* {#link #dropTablesScript()} to create the table destruction script
*
*/
public class SqlTableCreator {
private final AnnotationConfiguration hibernateConfiguration;
private final Properties dialectProps;
public SqlTableCreator(final Collection<Class<?>> entities) {
final Ejb3Configuration ejb3Configuration = new Ejb3Configuration();
for (final Class<?> entity : entities) {
ejb3Configuration.addAnnotatedClass(entity);
}
dialectProps = new Properties();
dialectProps.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
hibernateConfiguration = ejb3Configuration.getHibernateConfiguration();
}
/**
* Create the SQL script to create all tables.
*
* #return A {#link String} representing the SQL script.
*/
public String createTablesScript() {
final StringBuilder script = new StringBuilder();
final String[] creationScript = hibernateConfiguration.generateSchemaCreationScript(Dialect
.getDialect(dialectProps));
for (final String string : creationScript) {
script.append(string).append(";\n");
}
script.append("\ngo\n\n");
return script.toString();
}
/**
* Create the SQL script to drop all tables.
*
* #return A {#link String} representing the SQL script.
*/
public String dropTablesScript() {
final StringBuilder script = new StringBuilder();
final String[] creationScript = hibernateConfiguration.generateDropSchemaScript(Dialect
.getDialect(dialectProps));
for (final String string : creationScript) {
script.append(string).append(";\n");
}
script.append("\ngo\n\n");
return script.toString();
}
}

As Hibernate 4.3+ now implements JPA 2.1 the appropriate way to generate DDL scripts is to use following set of JPA 2.1 properties :
<property name="javax.persistence.schema-generation.scripts.action" value="create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="target/jpa/sql/create-schema.sql"/>
As it will be run at runtime, you may want to execute this DDL generation at build.
There is no supported official maven plugin anymore for Hibernate4 probably because Hibernate team is moving to Gradle.
Anyway, this is the JPA 2.1 approach to generate this script programmatically :
import java.io.IOException;
import java.util.Properties;
import javax.persistence.Persistence;
import org.hibernate.jpa.AvailableSettings;
public class JpaSchemaExport {
public static void main(String[] args) throws IOException {
execute(args[0], args[1]);
System.exit(0);
}
public static void execute(String persistenceUnitName, String destination) {
System.out.println("Generating DDL create script to : " + destination);
final Properties persistenceProperties = new Properties();
// XXX force persistence properties : remove database target
persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO, "");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "none");
// XXX force persistence properties : define create script target from metadata to destination
// persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS, "true");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION, "create");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET, destination);
Persistence.generateSchema(persistenceUnitName, persistenceProperties);
}
}
As you can see it's very simple !
You can now use this in an AntTask, or MAVEN build like this (for MAVEN) :
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-ddl-create</id>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- ANT Task definition -->
<java classname="com.orange.tools.jpa.JpaSchemaExport"
fork="true" failonerror="true">
<arg value="${persistenceUnitName}" />
<arg value="target/jpa/sql/schema-create.sql" />
<!-- reference to the passed-in classpath reference -->
<classpath refid="maven.compile.classpath" />
</java>
</target>
</configuration>
</execution>
</executions>
</plugin>

As a related note: Documentation for generating database schemas using EclipseLink JPA can be found here.

Here's an explaination of how to use the hibernate SchemaExport class to do exactly what you want.
http://jandrewthompson.blogspot.com/2009/10/how-to-generate-ddl-scripts-from.html

If you prefer configuring in Spring then this should be helpful:
<!-- CONTAINER-MANAGED JPA Entity manager factory (No need for persistence.xml)-->
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<!-- Fine Grained JPA properties Create-Drop Records -->
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<!-- The JPA vendor -->
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- <property name="database" value="MySQL"/> -->
<property name="showSql" value="true"/>
<!-- <property name="generateDdl" value="true"/> -->
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>

You can use maven plugin to achieve this.
<plugin>
<!-- run command "mvn hibernate3:hbm2ddl" to generate DLL -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>3.0</version>
<configuration>
<hibernatetool>
<classpath>
<path location="${project.build.directory}/classes" />
<path location="${project.basedir}/src/main/resources/META-INF/" />
</classpath>
<jpaconfiguration persistenceunit="galleryPersistenceUnit" />
<hbm2ddl create="true" export="false" destdir="${project.basedir}/target" drop="true" outputfilename="mysql.sql" format="true" console="true"/>
</hibernatetool>
</configuration>
</plugin>

<property name="hibernate.hbm2ddl.auto" value="update"/>
Add the above code in the persistence.xml under properties tag.
"update" will create the table when first time you run your code, after that, only update the table structures if any changes in domain object.

With EclipseLink, you should add property:
<property name="eclipselink.ddl-generation" value="create-tables"/>
As it is said here:
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/p_ddl_generation.htm
My persistence.xml:
<?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="appDB" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>LocalMySQL</jta-data-source>
<class>entity.Us</class>
<class>entity.Btl</class>
<class>entity.Co</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>

Related

Configure HSQLDB with maven

I'm developing a Spring application where everything is configured with maven (in pom.xml). My application uses a PostgreSQL database, but unit tests use an in-memory HSQLDB database.
I just run into an issue with TEXT columns because they are not supported natively by HSQLDB. In my entity class I have :
private #Column(columnDefinition = "text") String propertyName;
This works fine with Postgres, but HSQLDB is generating the following error : type not found or user lacks privilege: TEXT. The table is not created, and of course as a result most of my tests fail.
I found that I need to activate PostgreSQL compatibility in order for this to work by setting sql.syntax_pgs to true.
My question is : where do I put this setting ? I would like to put it in pom.xml because everything is configured there, but I don't know where.
For exemple I have :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dspring.profiles.active=test</argLine>
</configuration>
</plugin>
Can I somehow add an <argLine> with this setting ?
When you add hsqldb dependency it uses default connection properties. You can override these properties in property file or through other configuration as per your requirement. You can set "sql.syntax_pgs=true" to HSQLDB connection url. For example in case of spring boot this will be like below.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dspring.datasource.url=jdbc:hsqldb:mem:PUBLIC;sql.syntax_pgs=true</argLine>
</configuration>
</plugin>
you can set it in the Datasource configuration as given here
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:PUBLIC;sql.syntax_pgs=true" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>

JPA MetaDataException for different datasources

I have the problem with JPA Criteria API while using in my project different datasource persistance.
There are two PU uses different datasources:
<persistence-unit name="analysis" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<non-jta-data-source>AnalysisDS</non-jta-data-source>
<class>entity1</class>
<class>entity2</class>
<class>entity3</class>
and
<persistence-unit name="reaction" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<non-jta-data-source>ReactionDS</non-jta-data-source>
<class>someEntity1</class>
<class>someEntity2</class>
<class>someEntity3</class>
Spring load it, in applicationContext
<bean id="defaultAnalysysDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"
lazy-init="default">
<property name="jndiName" value="AnalysisDS"/>
<property name="lookupOnStartup" value="false"/>
<property name="cache" value="true"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
<bean id="defaultReactionDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"
lazy-init="default">
<property name="jndiName" value="ReactionDS"/>
<property name="lookupOnStartup" value="false"/>
<property name="cache" value="true"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
In my DAO I can work with this PU with EntityManager, for example for
ReactionDS I'Am using
#PersistenceContext(unitName = "reaction")
private EntityManager entityManager;
And all work done - simple query's and JPQL expressions.
But when I want to introduce to my DAO JPA Criteria API
Like this :
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
...
I have exception after getCriteriaBuilder() method works:
Caused by: <openjpa-2.4.0-r422266:1674604 fatal user error> org.apache.openjpa.util.MetaDataException: Errors encountered while resolving metadata. See nested exceptions for details.
at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:675)
at org.apache.openjpa.meta.MetaDataRepository.getMetaDataInternal(MetaDataRepository.java:418)
at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:389)
at org.apache.openjpa.persistence.meta.MetamodelImpl.(MetamodelImpl.java:86)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:348)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.getCriteriaBuilder(EntityManagerFactoryImpl.java:332)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
...
Caused by: <openjpa-2.4.0-r422266:1674604 fatal user error> org.apache.openjpa.util.MetaDataException: Table "ANALYSIS.ENTITY1" given for "entity1" does not exist.
at org.apache.openjpa.jdbc.meta.MappingInfo.createTable(MappingInfo.java:532)
at org.apache.openjpa.jdbc.meta.ClassMappingInfo.getTable(ClassMappingInfo.java:317)
at org.apache.openjpa.jdbc.meta.ClassMappingInfo.getTable(ClassMappingInfo.java:339)
at org.apache.openjpa.jdbc.meta.strats.FullClassStrategy.map(FullClassStrategy.java:73)
at org.apache.openjpa.jdbc.meta.ClassMapping.setStrategy(ClassMapping.java:392)
at org.apache.openjpa.jdbc.meta.RuntimeStrategyInstaller.installStrategy(RuntimeStrategyInstaller.java:55)
at org.apache.openjpa.jdbc.meta.MappingRepository.prepareMapping(MappingRepository.java:410)
at org.apache.openjpa.meta.MetaDataRepository.preMapping(MetaDataRepository.java:769)
at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:658)
... 147 more
The problem root cause in JPA, because his trying to use a tables from Analys in Reaction PU and extracts all meta-classes for entities that are located in different datasources, but access to them is doing in one.
But when I granted select on Entity1 to ReactionDS - all works done. (because I can use Select * from Analysis.Entity1 from reaction)
The question - how to make the metamodel classes to choose working only within the specified DS in EntityManager (in current example - Reaction, not together with Analysis) ?
p.s Database is Oracle, using Weblogic 12.1.3 and OpenJpa2.4.
Metamodel is generated automatically with maven plugin on compile:
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.apache.openjpa.persistence.meta.AnnotationProcessor6</processor>
</processors>
<optionMap>
<openjpa.metamodel>true</openjpa.metamodel>
</optionMap>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>${openjpa.version}</version>
</dependency>
</dependencies>
</plugin>
I think you may be confused by your Spring Framework datasource declarations.
These beans do not define your datasource, they only provide a way for other Spring components to access the datasources that have been configured in your server. JPA does not use these at all.
Therefore, your problem lies in the datasources that you have defined in your WebLogic server. It looks like you have defined both datasources to reference the same database instance.

Hibernate with Sql Server fail for nvarchar field with "No Dialect mapping..."

I'm using Hibernate's JPA-Implementation to access our SQL Server 2012 database.
When trying to select a nvarchar field in a native query, I get an exception "No Dialect mapping for JDBC type: -9".
It looks much like No Dialect mapping for JDBC type: -9 with Hibernate 4 and SQL Server 2012 or No Dialect mapping for JDBC type: -9 but I couldn't find a solution for me there (both are not using JPA).
My database setup:
CREATE TABLE NvarcharExample(
exampleField nvarchar(20) PRIMARY KEY
)
INSERT INTO NvarcharExample(exampleField) VALUES ('hello')
My code:
import java.io.IOException;
import javax.persistence.*;
#Entity
class NvarcharExample {
#Id
public String exampleField;
}
public class NvarcharTest {
public static void main(String[] args) throws IOException, InterruptedException {
String queryString = "SELECT e.exampleField FROM NvarcharExample e";
// establish connection
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistenceUnit");
try {
EntityManager entityManager = entityManagerFactory.createEntityManager();
// access data using JPQL
entityManager.createQuery(queryString).getResultList(); // works
// access data using SQL (native query)
entityManager.createNativeQuery(queryString).getResultList(); // fails
} finally {
entityManagerFactory.close();
}
}
}
My persistence.xml
<?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="persistenceUnit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<!-- database connection settings -->
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://<servername>:<port>;databaseName=<databasename>" />
<property name="javax.persistence.jdbc.user" value="<user>" />
<property name="javax.persistence.jdbc.password" value="<password>" />
</properties>
</persistence-unit>
</persistence>
With sql logging enable, I get this output in my console
select nvarcharex0_.exampleField as col_0_0_ from NvarcharExample nvarcharex0_
SELECT e.exampleField FROM NvarcharExample e
I'm using
hibernate-core-4.3.10.Final.jar
hibernate-entitymanager-4.3.10.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-commons-annotations-4.0.5.Final.jar
sqljdbc41.jar
What I've tried:
using a varchar instead of nvarchar makes it work, but I need nvarchar
using jpql instead of sql works (see my example code), but I need a native query
I tried sqljdbc4.jar in Version 4.0 and 4.1 and I tried sqljdbc41.jar
I head about subclassing the SQL Server Dialect class, but did not have any success with that
I added <property name="dialect" value="org.hibernate.dialect.SQLServerDialect" /> to my persistence.xml (right behind the password property)
I added <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" /> to my persistence.xml
I changed the persistence provider to <provider>org.hibernate.ejb.HibernatePersistence</provider>
Using #Nationalized attribute helped me to map String to nvarchar for MS SQL 2012 without dialect subclassing.
At the same time setting the hibernate.use_nationalized_character_data property to true did not worked for me.
For futher information watch docs National Character Types.
I was able to resolve that issue by subclassing the SQLServerDialect:
package packagename;
import java.sql.Types;
public class SqlServerDialectWithNvarchar extends org.hibernate.dialect.SQLServerDialect {
public SqlServerDialectWithNvarchar() {
registerHibernateType(Types.NVARCHAR, 4000, "string");
}
}
and referencing it in my persistence.xml:
<property name="hibernate.dialect" value="packagename.SqlServerDialectWithNvarchar" />
PS: It seems to be fixed with hibernate 5.1 according to this ticket: https://hibernate.atlassian.net/browse/HHH-10183

Generate java classes with JAXB2 in another maven project

Can someone please help me with my problem?
I have two maven projects A and B.
I want project A to contain all my model classes and in project B i'm creating a contract-first web service with Spring WS. So in project B, I use maven-jaxb2-plugin to generate classes from my schema. It happens that the generate classes in my webservice project (project A) are identical to the ones in my model project (project A) (with no XML annotations).
Because i don't want to have duplicate classes in my web service project (project B), i decided to make this project depends on the model project (project A) and what i want next is, not anymore generate classes to the webservice project but to the model project (project A).
Do you think there isn't another way to do this ?
Can someone please help me doing this if it's possible?
Project A
package project.a;
public class Client {
//...
}
Project B
Class
package project.b;
public class Compte {
//This class manipulates a Client object
//...
}
JAXB2 Maven Plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>src/main/java</outputDirectory>
<packageName>project.a</packageName>
<schemaDirectory>src/main/webapp/WEB-INF/schemas</schemaDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
<plugin>
Schema
<element name="client">
<complexType>
<sequence>
<!-- -->
</sequence>
</complexType>
</element>
Project B spring bean configuration
<bean class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="marshaller" ref="marshaller"/>
<property name="unmarshaller" ref="marshaller"/>
<property name="defaultUri" value="http://localhost:8080/project/" />
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="project.a"/>
</bean>
You don't have to recreate model classes again. Just place them in Project A and then import those beans into Project B as:
<import resource="classpath:projectABeanDefinitionFile.xml" />
Assuming you are implementing/going to #XmlRootElement on Compte class (else suggest to go through a tutorial like here),
then change project B file as:
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound" value="project.b.Compte"/>
</bean>

How to use HSQLdb with Ibatis

I want to use in memory database to query for data in my unit testing, my project is Ibatis (with annotation) for querying actual database which I want to mimic with the help of HSQLDB.
Please help me with how to configure iBatis with HSQLDB.
Also is there any way to these better for unit testing with code which is strongly dependent on database in its functions.
You can make an iBatis sqlMappings.xml config file something like this:
<sql-map-config>
<properties resource="configuration.properties" />
<!--The datasource for you application is configured here: -->
<datasource name = "hsql"
factory-class="com.ibatis.db.sqlmap.datasource.SimpleDataSourceFactory"
default="true">
<property name="JDBC.Driver" value=""/>
<property name="JDBC.ConnectionURL" value=""/>
<property name="JDBC.Username" value=""/>
<property name="JDBC.Password" value=""/>
</datasource>
<!--Declare the SQL Maps to be loaded for this application.
Be sure it's in your classpath. -->
<sql-map resource="maps/beanMappings.xml"/>
</sql-map-config>
plus a congifuration.properties file like this:
JDBC.Driver=org.hsqldb.jdbcDriver
JDBC.ConnectionURL=jdbc:hsqldb:hsql://localhost/myDb
JDBC.Username=sa
JDBC.Password=
and then use it like this:
String resource = "maps/SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlMap sqlMap = XmlSqlMapBuilder.buildSqlMap(reader);

Categories