Generating DDL from entity class in intellij - java

Could you please let me know whether we have an option to generate ddl from entity class in intellij?
I could do the entity class generation from the table? I am looking to change the definition of a entity class and i need to re-generate the DDL for table?
I am using eclipse links for JPA.

I would recommend to use eclipse link (not intellij) to create the ddl script.
With the ddl-generation.output-mode in persistence.xml you can instruct eclipse link to create an ddl script.
<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 persistence_2_0.xsd" version="2.0">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
...
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.create-ddl-jdbc-file-name" value="createDDL_ddlGeneration.jdbc"/>
<property name="eclipselink.drop-ddl-jdbc-file-name" value="dropDDL_ddlGeneration.jdbc"/>
</properties>
</persistence-unit>
</persistence>
#see:
http://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/p_ddl_generation_output_mode.htm
http://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/p_ddl_generation.htm

Related

How to implement JPA in Play Framework in a standard way

I have gone though https://www.playframework.com/documentation/2.5.x/JavaJPA
But it doesn't feel like a stardard way. I would like fill my jdbc connection in persistence.xml instead of application.conf
like this
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="TestPersistence"
transaction-type="RESOURCE_LOCAL">
<class>com.example.pojo.Employee</class>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/jpadb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password"
value="mukesh" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
instead of filling the jdbc connection in application.conf like
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/jpadb"
db.default.user=root
db.default.password="mukesh"
db.default.jndiName=DefaultDS
jpa.default=TestPersistence
Also if there is no other way to get jdbc connection in persistence.xml how can we define more than one jdbc connection in application.conf. like if i have a mysql for users and oracle db for posts.
NOTE: the source code posted are just dummy copied from public domain to show the briefly problem.
You don't have to use Play's DB module. You can have your own module defined, and load your database connection and run it in isolation.
I would check out https://github.com/playframework/play-isolated-slick as an example -- here the UserDAO is bound through to SlickDAO in the Play module:
https://github.com/playframework/play-isolated-slick/blob/master/modules/play/app/Module.scala#L19
and the database provider is provided:
https://github.com/playframework/play-isolated-slick/blob/master/modules/play/app/Module.scala#L27
but you don't have to use Database.forConfig("myapp.database") -- you can define any config you feel like.

How to populate a DB with my JPA entities in IntelliJ

Is there a way in IntelliJ to create tables in a database from my entity classes? I mean without ER diagram. The IDE allows me to create entities from the DB, but not the other way around.
I used to use netbeans and it just lets you add entities to your persistense.xml and select mode (drop and create, create, etc), but I can't find these options in IntelliJ.
I have configures de DB connection and it works, it just doesn't allow me to populate it with my entities.
Here's my persistence.xml from NetBeans (works perfect)
<?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="ClubPU2" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>baseclub.entidades.Actividad</class>
<class>baseclub.entidades.Comercio</class>
<class>baseclub.entidades.Contacto</class>
<class>baseclub.entidades.Cuota</class>
<class>baseclub.entidades.DatosInstitucionales</class>
<class>baseclub.entidades.DatosPersonales</class>
<class>baseclub.entidades.Descuento</class>
<class>baseclub.entidades.Identificacion</class>
<class>baseclub.entidades.Miembro</class>
<class>baseclub.entidades.NotaEntrada</class>
<class>baseclub.entidades.Participacion</class>
<class>baseclub.entidades.Rol</class>
<class>baseclub.entidades.Secretaria</class>
<class>baseclub.entidades.Sesion</class>
<class>baseclub.entidades.Tutor</class>
<class>baseclub.entidades.Ubicacion</class>
<class>baseclub.entidades.Usuario</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/club_universitario?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="XXXXXX"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="club"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
And here's persistence.xml from IntelliJ:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="persistenceUnit">
</persistence-unit>
</persistence>
Is there any particular reason you want IntelliJ to create the database?
if you have selected Hibernate as ORM , you can simply add in your persistence.xml the following code:
<property name="hibernate.hbm2ddl.auto" value="update" />
Start your application server, deploy your app and the database will be created.

How to embed java database server into distributed jar?

I am working on a project that uses JDBC through derby.jar, and I am trying to make it usable without the database server running inside NetBeans. I already have a running database (wich is not embedded) and a complete code. Here is my xml that i use.
<?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="boltPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>entity.Termek</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/bolt;create=true"/>
<property name="javax.persistence.jdbc.password" value="asd"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="asd"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Any suggestion for its modification? I tried using EmbeddedDriver instead of ClientDriver, but i got an exception that it cannot be found.
Edit:
I tried putting all required jar files in the classpath and they are all added to the libraries.
Did you try putting the derby jar and other required jars by derby in the classpath of the app?
More instructions here
http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
Can you edit your question to include stacktrace of errors you get after trying above link

Play framework 2.2.1 always tries to create DB at startup

I have project in Play 2.1.5 and I've decided to migrate to Play 2.2.1. I followed http://www.playframework.com/documentation/2.2.1/Migration22 and everything works except that on startup it looks like the project is trying to create DB instead updating it. I always get duplicity errors. I didn't have this problem while I was using Play 2.1.5. I don't see any problem in my configuration, but maybe I overlooked something:
application.conf:
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit
db.default.url="jdbc:mysql://localhost/database"
db.default.driver=com.mysql.jdbc.Driver
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.MySQLDialect"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
Build.scala dependencies contain:
javaCore,
javaJdbc,
javaJpa,
cache,
"org.hibernate" % "hibernate-entitymanager" % "4.2.7.Final"
With
<property name="hibernate.hbm2ddl.auto" value="update"/>
you are telling to update your schema according to your models each time you start the application. If you won't modify the schema try to set the value to validate.
BTW Duplicate entry 'VALUE' for key 'COLUMN_NAME' error suggest you are trying to make some insert/update at start time, aren't you?

How to make Hibernate to create database SQL script for initialization and update

Hibernate is able to create database structure. Below is example how configuration looks using JPA2 with <property name="hibernate.hbm2ddl.auto" value="create"/>
( Use Hibernate Entity Manager in the case)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<!-- Uncomment the following two properties for JBoss only -->
<!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
<!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
</properties>
</persistence-unit>
</persistence>
How to make Hibernate to create database SQL script for initialization and update? (that could be run manually or via batch execution) and what tools to use for that.
Yes, there are SQL statements in log (Q Getting SQL script from Hibernate update ), but that is not clean solution.
The goal is to prepare solution for automated database upgrade with every version release.
For automated database upgrades, look at another tool specifically designed for the job. I would suggest Liqiubase.

Categories

Resources