How to create Objects in database if does not exists with JPA? - java

I have a web application that I use JPA / Hibernate I want to know how to hibernate can generate the non-existent tables from entities.
the persistence file is as follows:
What I can add as property ?
<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_1_0.xsd"
version="1.0">
<persistence-unit name="BankingApp">
<provider> org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:#localhost:1521:XE"/>
<property name="hibernate.connection.username" value="user"/>
<property name="hibernate.connection.password" value="password"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
</properties>
</persistence-unit>
</persistence>

You can add this property
<property name="hibernate.hbm2ddl.auto" value="update"/>
The possible values for this property are:
validate: Only validates the schema
update: Update the schema
create: Create the schema, override previous data
create-drop: Create the schema and drop the schema at the end of the session

I found the solution, simply add the following property:
<property name="hibernate.hbm2ddl.auto" value="create"/>

Related

Intellij IDEA Generating Persistence Mappings by database schema

I am brand new to Hibernate and I am trying to generate enitty classes, by database schema in IntelliJ IDEA Ultimate 2016.1. I create a new project with Java EE Persistence persistence.xml version 2.0, Hibernate provider and use library JPA 2.1-2.1. I have included in project library "hibernate-entitymanager-4.3.11.Final.jar".
But in Persistence tool window there is no child nodes: persistence.xml and NewPersistenceUnit. When i right-click on parent node "Generate Persistence Mappings" -> "By Databse Schema" nothing happens.
My persistence.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="NewPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value=""/>
<property name="hibernate.connection.driver_class" value=""/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Does anybody know why "By Database Schema" doesn't work?

Hsqldb driver being randomly null

I am involved in writing a project which requires servlet->database connection. I am collaborating with another person who have designed a database using HyperSQL (hsqldb), I am now trying to merge my project with his by adding his code to mine.
Further to my issue though. When I copy the code, it works, usually. I have few methods that use the data from the database and compare them with user input.
When attempting to connect to the database, I would randomly succeed or fail, getting the following error ;
Unable to acquire a connection from driver [null]
I of course initialise a driver ;
Class.forName("org.hsqldb.jdbcDriver").newInstance();
Now, when running my method, it sometimes succeeds, and sometimes fails, here is the XML file for the DB;
<?xml version="1.0" encoding="UTF-8"?>
<!--
Creates both the HyperSQL databases using hibernate. No password or username is set.
-->
<persistence version="1.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_1_0.xsd">
<persistence-unit name="monsters" transaction-type="RESOURCE_LOCAL">
<class>databaseManagement.Monster</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:monsters"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
<persistence-unit name="users" transaction-type="RESOURCE_LOCAL">
<class>databaseManagement.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:users"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Probably an attempt is made to open the database in a readonly directory.
You need to specify a username for both persistence units. The default username is "SA".
The file path of the database that you specify in the URL is relative. It resolves to the execution directory. As you are developing a web application, you need to specify a directory that can be written to.
One way of doing this is by including a variable in the path, such as "jdbc:hsqldb:file:{$directorypath}/monsters" where the directorypath is the name of the web application's data dirctory, as specified in your web.xml file.

Provider error: DatastorePersistenceProvider

I'm trying to convert my hibernate-based GWT implementation into a JPA2 version. I've added the persistence.xml, the specific libraries and added some code just to open (by getting an entityManager). I just do a local employment, I do not use google's app engine (and I'm not intended to use it).
I get the following message:
Cannot connect to MySQL database
server (Provider error. Provider:
org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider),
exiting
My persistence.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd"
version="1.0">
<persistence-unit name="mydatabase_dev">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>mypackage.Product</class>
<class>mypackage.Expert</class>
<class>mypackage.Person</class>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://myhost.mydomain/MYDATABASE"/>
<property name="hibernate.connection.username" value="myusername"/>
<property name="hibernate.connection.password" value="topsecret"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
<!-- Disable the second-level cache -->
<property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
</properties>
</persistence-unit>
</persistence>
There is no other error message, warning, stacktrace, etc.
What is wrong or in what way can I examine what the real problem is?
GAE/J doesn't do MySQL, nor JPA2 so why are you including it/specifying it in your persistence.xml? Perhaps if you post your persistence.xml then people can make an informed opinion ...

How to define an non-JTA datasource for JPA-Hibernate? org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource:

I am trying to change my web-app's JDBC code to JPA using Hibernate as provider. I am using Eclipse IDE. In that i have defined a MySQL data source. I added it in the persistence.xml.
But, I am getting the below error.
6640 [30289364#qtp-7494106-7] ERROR org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: tamSql
javax.naming.NameNotFoundException; remaining name 'tamSql'
My persistence.xml looks like,
<?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="ExpensePersistentUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>tamSql</non-jta-data-source>
<class>com.pricar.JPAInteg.Role</class>
<class>com.pricar.JPAInteg.User</class>
<class>com.pricar.JPAInteg.Userdetail</class>
<class>com.pricar.JPAInteg.Category</class>
<class>com.pricar.JPAInteg.Expens</class>
<class>com.pricar.JPAInteg.Leavetable</class>
<class>com.pricar.JPAInteg.Permissiontoken</class>
<class>com.pricar.JPAInteg.Roletokenassociation</class>
<class>com.pricar.JPAInteg.UserPK</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/officemgmt"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
Any Suggestions!!!
Thanks in Advance!
You don't need <non-jta-data-source> when your datasource is configured in <properties>. <non-jta-data-source> is needed when datasource is configured in application server configuration and obtained via JNDI.

Hibernate/JPA: Mapping entities to different databases

I have an application which manages 3 databases. I use hibernate with JPA on seam framework.
So I have a persitence.xml file with three persitence-unit like this (I remove properties for db2 and db3):
<persistence-unit name="db1" transaction-type="JTA" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>db1source</jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class"
value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.default_schema" value="SI_TEC" />
<property name="hibernate.validator.apply_to_ddl" value="false" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
</properties>
</persistence-unit>
<persistence-unit name="db2" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>d2source</jta-data-source>
</persistence-unit>
<persistence-unit name="db3" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>d3source</jta-data-source>
</persistence-unit>
In my seam components.xml file, I create 3 managed-persistence-context to map seam with my hibernate configuration.
Finally, I have several entities and my problem is here. I need to persist some entities in db2 and other in db3. So database schema are different and when I deploy my application, I get this error:
org.hibernate.HibernateException: Missing table: PORTAILPERMISSION
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1113)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:349)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
Truncated. see log file for complete stacktrace
Because, the table PORTAILPERMISSION doesn't exist in db2.
My question is:
How to specify in entity class what database (or persitence-unit) must be used to validate entity in startup ?
Thanks for your help.
You try to explicitly list classes (<class>..</class>) in each persistence unit. And use
<exclude-unlisted-classes>true</exclude-unlisted-classes>

Categories

Resources