Intellij IDEA Generating Persistence Mappings by database schema - java

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?

Related

Hibernate JPA - No Persistence provider for EntityManager error

I am trying to connect to sql server using JPA and hibernate with an Eclipse Maven project. I've tried moving the persistence.xml file to various locations but it doesn't seem to help.
My persistence.xml file is
<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="Trades" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver:/Trades\SQL00;databaseName=Trades" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="dev" />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="show_sql" value="true"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</properties>
</persistence-unit>
</persistence>
and my code is as follows and is located in src/main/resources/META-INF
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Trades");
System.out.println();
}
I have attached a screenshot which shows my eclipse set up
I solved this, turned out that Maven must have downloaded corrupt libraries - I was alerted this when I was unable to view the source for some classes in the Hibernates jars. I deleted the jars in my .m2 directory and when the jars were refreshed, it worked fine. A bit of tricky one.

Netbeans Persistence Unit Removal

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>
`

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

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"/>

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 ...

EclipseLink into OSGI Bundle persistence.xml not found

I'm having some problem with EclipseLink. I'm using GlassFish v3.1 and I'm trying to use EclipseLink for my persistence layer. I followed all tutorials available on the Eclipse wiki without luck. My persistence.xml file cannot be parsed and I receive this error while trying to create the EntityManagerFactory:
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: bundle://307.1:1/
Here is my persistence.xml located in /WEB-INF/classes/META-INF/:
<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="generic">
<class>com.generic.domain.Service</class>
<properties>
<!-- Embedded MySQL Login -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306"/>
<!-- TODO: replace with connection pool -->
<property name="javax.persistence.jdbc.userid" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.jdbc.read-connections.min" value="1"/>
<property name="eclipselink.jdbc.write-connections.min" value="1"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<!-- Logging Settings -->
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.thread" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.exceptions" value="true" />
<property name="eclipselink.logging.timestamp" value="false"/>
</properties>
</persistence-unit>
</persistence>
I added this line to my MANIFEST.MF:
JPA-PersistenceUnits: generic
I can now confirm that it's a bug in EclipseLink. The work around to your problem is to either get hold of EntityManagerFactory using JNDI lookup or #PersistenceUnit instead of doing Persistence.createEntityManagerFactory().
1) It looks like you are trying to use OSGi/JPA from your WAB. Correct?
2) Can you tell if you have installed any new eclipselink bundles in your system? If so, what are they?
3) Can you provide stack trace?

Categories

Resources