Hibernate/JPA not validating against DB schema on startup - java

For some reason hibernate is not catching issues like mapping entities to tables that do not exist. My persistence.xml file looks like this...
<?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">
<!-- A JPA Persistence Unit -->
<persistence-unit name="printLogixJpaUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.printlogix.rp.server.domain.User</class>
<class>com.printlogix.rp.server.domain.UserImage</class>
<class>com.printlogix.rp.server.domain.TemplateInstance</class>
<class>com.printlogix.rp.server.domain.UserOrder</class>
<class>com.printlogix.rp.server.domain.OrderLineItem</class>
<class>com.printlogix.rp.server.domain.OrderStatus</class>
<class>com.printlogix.rp.server.domain.AgencyImage</class>
<class>com.printlogix.rp.server.domain.Agency</class>
<class>com.printlogix.rp.server.domain.Brokerage</class>
<class>com.printlogix.rp.server.domain.SystemTemplate</class>
<class>com.printlogix.rp.server.domain.TemplateType</class>
<class>com.printlogix.rp.server.domain.Product</class>
<class>com.printlogix.rp.server.domain.ProductDefinition</class>
<class>com.printlogix.rp.server.domain.ExpeditedAddress</class>
<class>com.printlogix.rp.server.domain.BrokerageUser</class>
<class>com.printlogix.rp.server.domain.StateProvince</class>
<class>com.printlogix.rp.server.domain.Country</class>
<class>com.printlogix.rp.server.domain.AwardClub</class>
<class>com.printlogix.rp.server.domain.SuperUser</class>
<class>com.printlogix.rp.server.domain.AgencyUser</class>
<class>com.printlogix.rp.server.domain.RememberMe</class>
<class>com.printlogix.rp.server.domain.PasswordResetRequest</class>
<class>com.printlogix.rp.server.domain.SystemTheme</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="true"/>
<!--Begin Credentials -->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/realtorprint_dev?autoReconnect=true"/>
<property name="hibernate.connection.username" value="test"/>
<property name="hibernate.connection.password" value="test"/>
<!--End Credentials -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
</properties>
</persistence-unit>
</persistence>

Try adding the property "hibernate.hbm2ddl.auto" with value of "validate":
<property name="hibernate.hbm2ddl.auto" value="validate"/>

Related

Hibernate errors : No Persistence provider for EntityManager named my-unit

i've tried to put my persistence.xml in various parts of my Java project and i call it with this command:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("my-unit");
but it returns me an error:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named my-unit
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at main.MainClass.main(MainClass.java:81)
Process finished with exit code 1
my persistence.xml file:
<?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="my-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.CharSet" value="utf8"/>
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- use encache provider-->
<!--<property name="hibernate.cache.region.factory_class"-->
<!--value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>-->
<!--<property name="hibernate.javax.cache.provider" value="org.ehcache.jsr107.EhcacheCachingProvider"/>-->
<!--<!–use second level cache–>-->
<!--<property name="hibernate.cache.use_second_level_cache" value="true"/>-->
<!--<!–use 3rd level cache–>-->
<!--<property name="hibernate.cache.use_query_cache" value="true"/>-->
</properties>
</persistence-unit>
I use IntelliJ as IDE for Java and the persistence.xml file is under META-INF folder in resources folder, i have tried to check the project structure and all looks like nice.
I use hibernate as maven repository.
Thanks for helping
You need to define the persistence provider name
<provider>org.hibernate.ejb.HibernatePersistence</provider>
That will solve the problem.

Spring Data JPA example fail with user lacks privilege [PUBLIC.AUDITABLE_USER]

The example is from here
jpa\example\src\test\resources\META-INF\persistence.xml
<?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="jpa.sample.plain">
<class>example.springdata.jpa.simple.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:spring" />
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
I still see the issue after versions in jpa\pom.xml were updated and look like:
<properties>
<hibernate.version>5.2.10.Final</hibernate.version>
<spring.version>4.3.8.RELEASE</spring.version>
</properties>

WildFly giving error for JNDI Type mismatch

I am trying to look-up jndi on wildfly 10 for my datasource, but it is giving type mismatch error.
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'corePU' must be of type [javax.persistence.EntityManagerFactory], but was actually of type [org.jboss.as.connector.subsystems.datasources.WildFlyDataSource]"},"WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./MyDataSource"],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}
How to get JNDI of type javax.persistence.EntityManagerFactory
My persistence.xml is:
<?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="corePU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/MyDataSource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<!--<shared-cache-mode>NONE</shared-cache-mode>-->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.transaction.jta.platform" value="${jta.platform}"/>
<property name="org.hibernate.envers.audit_table_prefix" value="z_"/>
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
</properties>
</persistence-unit>

Changes in persistence.xml causes IncompatibleClassChangeError error

When I change my persistence.xml to a JNDI lookup a IncompatibleClassChangeError is being thrown.
This is the one that is working
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:orm="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
<persistence-unit name="report-pu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>
<class>com.citi.listener.model.TATReportItem</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.id.new_generator_mappings" value="false"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
</properties>
</persistence-unit>
</persistence>
The one that causes the issue is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:orm="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
<persistence-unit name="report-pu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.citi.listener.model.TATReportItem</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="X" />
<property name="javax.persistence.jdbc.user" value="X" />
<property name="javax.persistence.jdbc.password" value="X" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
It's not obvious to me why this is causing the issue?
Issue here was that the hibernate-entitymanager scope was not set to provided in the pom

Hibernate with Glassfish 4.1

I've setted up Hibernate on Glassfish 4.1 but I'm having problems with persistence.
I'm able to read data, but cannot write to BD (changes appear to not be commited).
My current persistent.xml looks like this:
<?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://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myDataSource</jta-data-source>
<properties>
<property name="transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>
</properties>
</persistence-unit>
</persistence>
My connection pool config on Glassfish is:
<jdbc-connection-pool datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerDataSource" steady-pool-size="2" name="myPool" res-type="javax.sql.DataSource">
<property name="TrustServerCertificate" value="false"></property>
<property name="User" value="sa"></property>
<property name="LastUpdateCount" value="true"></property>
<property name="ResponseBuffering" value="adaptive"></property>
<property name="URL" value="jdbc:sqlserver://server\bd"></property>
<property name="XopenStates" value="false"></property>
<property name="PacketSize" value="8000"></property>
<property name="Password" value="mypass"></property>
<property name="ApplicationName" value="Microsoft JDBC Driver for SQL Server"></property>
<property name="DatabaseName" value="MyDB"></property>
<property name="Encrypt" value="false"></property>
<property name="LockTimeout" value="-1"></property>
<property name="SendStringParametersAsUnicode" value="true"></property>
<property name="MultiSubnetFailover" value="false"></property>
<property name="ApplicationIntent" value="readwrite"></property>
<property name="LoginTimeout" value="15"></property>
<property name="WorkstationID" value="My-MacBook-Pro.local"></property>
<property name="ServerName" value="xpto"></property>
<property name="PortNumber" value="1433"></property>
<property name="SelectMethod" value="direct"></property>
<property name="SendTimeAsDatetime" value="true"></property>
</jdbc-connection-pool>
Datasource config:
<jdbc-resource pool-name="myPool" jndi-name="jdbc/myDataSource"></jdbc-resource>
My EJB looks like this:
#PersistenceContext
private EntityManager em;
public void updateUser(User u) {
em.merge(u);
}
Any idea how I can fix that?
Thanks!
In my case, I was running the Hibernate 5 with tomcat and stop working when I changed to glassfish 4.1
The reason was the oldest jboss-logging.jar at: "YOUR_GLASSFISH_FOLDER/glassfish/modules"
Why? The hibernate 5 has dependency with the newest version of jboss-logging, and the glassfish uses the oldest version even if you declare inside your POM file the newest version. Actually I'm using:
org.jboss.logging jboss-logging 3.3.0.Final
Then I downloaded and replace the old .jar inside modules path and back to work, I spent 2 days trying to solve that and I hope it helps some future issues =D
I used this link to help me: https://medium.com/#mertcal/using-hibernate-5-on-payara-cc242212a5d6#.npq2hdprz
Could you please try the following configuration:
<?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="YOUR_PERSISTANCE_NAME" transaction-type="RESOURCE_LOCAL">
<provider>YOUR_PROVIDER</provider>
<!-- ENTITIES -->
<class>com.company.project....EntityA</class>
<class>com.company.project....EntityB</class>
<class>com.company.project....EntityC</class>
<properties>
<property name="javax.persistence.jdbc.url" value="YOUR_URL_TO_DB" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.user" value="USER" />
<property name="javax.persistence.jdbc.password" value="PASS" />
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.target-database" value="PostgreSQL" />
</properties>
</persistence-unit>

Categories