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.
Related
In a maven-java-sql project using hibernate, when I try to run the project this error message appears. The MySQL server is running but the the maven-java project is not able to comunicate with the MySQL server. I am using the Apache NetBeans IDE and Workbench as the server tool. Here is my persistence class
<?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="Persistence" transaction-type="RESOURCE_LOCAL">
<!-- Persistence provider -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/technikon"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
the error message
The error message tells you that:
Caused by: java.sql.SQLSyntaxErrorException: Unknown database 'technikon'
So, you will need to connect to your RDBMS via Workbench (or CLI or whatever) and run
show databases;
See what databases you are finding in the list (beware db names like Technikon) and change your settings accordingly.
Hi guys I have a big problem: I developed my webservice with eclipselink JPA + mongoDb, everything works fine on my local machine but when I try to set the persistence.xml for the connection to a remote NoSql Database it doesn't work.
Here's 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="introsde-final-jpa">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
<property name="eclipselink.nosql.property.mongo.host" value=<my_host>/>
<property name="eclipselink.nosql.property.mongo.port" value=<my_port>/>
<property name="eclipselink.nosql.property.mongo.db" value="mydb"/>
<property name="eclipselink.nosql.property.mongo.user" value=<my_user>/>
<property name="eclipselink.nosql.property.mongo.password" value=<my_password>/>
</properties>
</persistence-unit>
</persistence>
any suggestion is appreciated!!
I faced with some weird behavior: my IDE can't recognize xml document if it named "persistence.xml" and all features like syntax coloring & code completion doesn't work for this file.
But when I rename it, idea start to support this xml document.
So, what can be the reason of this behavior?
(Debian, IDEA 14)
Updated
persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ex01" transaction-type="JTA">
<jta-data-source>java:global/jdbc/ch08</jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation-target" value="database-and-scripts"/>
<property name="javax.persistence.schema-generation.scripts.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="create.sql"/>
<property name="javax.persistence.sql-load-script-source" value="sql/initData.sql"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:07DB;create=true"/>
<property name="eclipselink.logging.level" value="OFF"/>
</properties>
</persistence-unit>
</persistence>
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
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