I want to create a dynamic time table using pure JPA 2. Like in hibernate we create the dynamic table using SchemaExport.
The code:
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/employee;create=true"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name = "hibernate.show_sql" value = "true" />
<properties>
The following exception occur when I use create=true:
WARN: HHH000342: Could not obtain connection to query metadata : Unknown database 'employee;create=true'
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: EmployeeService] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
You can specify create=true in persistence.xml url.
<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
...
<properties>
---
<property name="javax.persistence.jdbc.url"
value="jdbc:???://localhost:port/DBName;create=true"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="pwd"/>
</properties>
</persistence-unit>
</persistence>
Related
My Java Servlet application currently has the database connection parameters hard-coded in the persistence.xml file like this:
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/XXX?useEncoding=true&characterEncoding=UTF-8"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="XXX"/>
<property name="javax.persistence.jdbc.password" value="XXX"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
I'd like to move those out to a separate configuration file so that users can change them on their own. What's the way to do that?
i am using Java Eclipse oxygen and Mysql for Database. I am trying to Persist data on my sql. It says "Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary." how can it be solved? Thank you.
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/studentdata"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="SEVERE"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
Remove
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
The ServiceLoader mechanisme will find the driver by its own.
In a JPA project when using the postgresql driver the tables are auto generated. When i switch to the h2 database driver, I get errors, that the tables are not found. This let me assume, that the tables are not generated.
Postgresql Config
<persistence-unit name="postgresql" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>...</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1/example"/>
<property name="javax.persistence.jdbc.password" value="example"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.user" value="example"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
</properties>
</persistence-unit>
h2 config
<persistence-unit name="OntoJobTestDB" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>...</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
</properties>
</persistence-unit>
It turned out, that the tables could not have been found, because they were never created. An early error caused by a #Converter annotation, which was postgresql specific prevented the creation of the tables.
My advice is to always double check the debug output.
I created a Persistence Unit in Netbeans and have since deleted the Persistence.xml file. Now whenever I run my project, I get tonnes of errors like "No Persistence provider for EntityManager named Hi__Score_OldPU". How can I get rid of my Persistence Unit entirely?
That error happen when persistence.xml is at the wrong place (Project-->WebContent-->META-INF-->persistence.xml).
It should be here (src-->META-INF-->persistence.xml)
If that folder doesn't exist you can create a folder "META-INF" at src and put persistence.xml on it.
here my persistence.xml look like this.
`
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="livraria" transaction-type ="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>br.com.k19.modelo.Editora</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/K21_livraria_bd"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
</properties>
</persistence-unit>
`
I'm using a HSQLDB for an application that stores research data and there is quite a lot of data. HSQLDB insists on always loading the tables into memory. I've tried fixing this by setting hsqldb.default_table_type=cached in my persistence.xml but that does not work.
Is this the wrong place?
persistence.xml
<persistence-unit name="Dvh DB" transaction-type="RESOURCE_LOCAL">
<class>com.willcodejavaforfood.dvh.entity.Patient</class>
<class>com.willcodejavaforfood.dvh.entity.Plan</class>
<class>com.willcodejavaforfood.dvh.entity.Dvh</class>
<class>com.willcodejavaforfood.dvh.entity.ImportSession</class>
<class>com.willcodejavaforfood.dvh.entity.Project</class>
<class>com.willcodejavaforfood.dvh.entity.Course</class>
<class>com.willcodejavaforfood.dvh.entity.Property</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:./myDvhDb"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hsqldb.default_table_type" value="cached" />
</properties>
</persistence-unit>
When my HSQLDB database is created I can see in its properties file:
hsqldb.default_table_type=memory
Thanks
Could you try putting the hsqldb.default_table_type=cached in the connection string? Like this: jdbc:hsqldb:./myDvhDb;hsqldb.default_table_type=cached