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?
Related
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.
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 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>
I am using this method in the following way
EntityManagerFactory emftemp = Persistence.createEntityManagerFactory("XYZ");
and my persistence.xml has the following entry for this unit
<persistence-unit name="XYZ" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.query.substitutions" value="true=1, false=0"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:#aesop-db.corp.nlg1.com:1521:TLCSDEV4"/>
<property name="hibernate.connection.username" value="abcd"/>
<property name="hibernate.connection.password" value="abcd"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect"/>
<property name="hibernate.connection.timeout" value="120"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.min_size" value="2"/>
<property name="hibernate.c3p0.max_size" value="2"/>
<property name="hibernate.c3p0.idle_test_period" value="60"/>
</properties>
</persistence-unit>
This is a Java application and I am using the hibernate3.jar and I am using ejb3-persistence.jar and JDK1.5.
The problem is that everytime I run the application, it hangs on this call . Its not throwing any exception or error . It just hangs at that point.
Can anyone help in identifying why it does not move forward ?
Two thoughts:
When something hangs it's often IO/networking issues to blame. So, try to make sure that the DB is really alive, and that you can connect to it from your computer.
This could be a duplicate of Persistence.createEntityManagerFactory takes ages
I had this same issue against an Oracle 11g database with only three entities defined but a very large DB. After commenting out the following line from persistence.xml, the issue was resolved.
<property name="hibernate.hbm2ddl.auto" value="update"/>
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