47809

How can I get the org.hibernate.cfg.Configuration in Play Framework 2 using the JPA plugin

I can get the JPA Config like this:

Configuration jpaConf = Configuration.root().getConfig("jpa");

But how can I get the org.hibernate.cfg.Configuration which I need to do a Schema Export like in this Question (base on Play Framework 1): Using SchemaExport in Play Framework

My Play Framework 2.x application.conf has this:

# Database configuration # ~~~~~ db.default.driver=org.postgresql.Driver db.default.url="jdbc:postgresql://localhost:5432/dpma" db.default.user=bp db.default.jndiName=DefaultDS jpa.default=defaultPersistenceUnit

my persistence.xml looks 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="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <non-jta-data-source>DefaultDS</non-jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.use_sql_comments" value="true"/> <!--<property name="hibernate.default_schema" value="&quot;legalEntitiesTest&quot;"/>--> <property name="hibernate.globally_quoted_identifiers" value="false"/> </properties> </persistence-unit> </persistence>

**For those unfamiliar with Play Framework: ** https://playframework.com/documentation/2.2.4/JavaJPA

Answer1:

You can create hibernate configuration object yourself, using hibernate configuration file:

Configuration c = new Configuration(); c.configure("path to hibernate config").getProperty("hibernate property");

If you do not use hibernate cfg file, you can instead add JPA entities to configuration this way (<strong>it assums all your JPA classes belong to one package</strong>):

final Configuration configuration = new Configuration(); final Reflections reflections = new Reflections(EntityClass.class.getPackage().getName()); final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class); for (final Class<?> clazz : classes) { configuration.addAnnotatedClass(clazz); }

Before you execute schema export, remember to set dialect:

configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");

Recommend

  • Xamarin Android | Layout style
  • Sending cookie value via httpget but not getting the desired response
  • How to convert SOAP response with xsi values to json in WSO2esb
  • C# fibonacci function returning errors
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • How to get current document uri in XSLT?
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • What is the purpose of TaskExecutor in spring?
  • JBoss External Properties Files in Classpath
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • Xcode 4 NSLog Macro link in Xcode 3
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • Jquery UI tool tip close icon
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • Dynamically accessing properties of knockoutjs observable array
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • Uncaught Error: Could not find module `ember-load-initializers`
  • script to move all files from one location to another location
  • WinForms: two way TextBox problem
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • Can I make an Android app that runs a web view in Chrome 39?
  • Is there a mandatory requirement to switch app.yaml?
  • How to include full .NET prerequisite for Wix Burn installer
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize