Hibernate xml file issue - java

I have set up Tapestry 5 project and all went fine, until I deployed Hibernate. I have created hibernate.xml file and
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/project</property>
<property name="connection.username">root</property>
<property name="connection.password">password12</property>
<property name="connection.pool_size">5</property>
<!-- Print SQL to stdout. -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="generate_statistics">true</property>
<property name="hibernate.archive.autodetection">class, hbm</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<!-- Mapping files TODO: Classify those mappings in exact order and define the relations between them in entities some time later on.-->
<mapping class="rs.project.com.entities.Fruit"/>
<mapping class="rs.project.com.entities.Article"/>
</session-factory>
and it's OK as far as the implementation of it is concerned. However when I deploy the app it defines me some other config, which can be seen on my trace log, and uses some other xml file, based on the mappings it shows me on the log, and it's about some completely different project I used a while ago. The thing is I can't see what's causing such a behavior, and I am really frustrated. I am using Tomcat Apache Catalina and MySQL for Hibernate. Also, I did some research and found out that persistance.xml file is being used in my project.properties which is kinda strange.
persistence.xml.dir=${conf.dir}
Driver for connecting my app to MySQL is jdbc.mysql.driver.So my goal is to possibly define the matter that causes such behavior here with you, and to solve it.
Thanks in advance for your answers.

If your tomcat log is referring to a different project, maybe your context declaration is not right?
Check your contexts directory (for me it's $Tomcat_home\conf\Catalina\localhost) or the Server.xml (if that's what you're using). Make sure that the context file in the contexts directory is pointing to the right directory/project. This error has happened to me before when a previous project had the same context-name as my current one.

Related

Grails 4 not picking up mapping section of hibernate.cfg.xml file

We are migrating a Grails 2 application to Grails 4.
The application internally consists of a backend in spring/java and a grails front-end. As a result almost all database classes are defined as java classes with the correct hibernate annotations. To support gorm we added the required <mapping .../> entries for the packages and the classes to hibernate.cfg.xml, which is working correctly in grails 2.
To migrate hibernate.cfg.xml we did set the location property in application.yml:
---
hibernate:
cache:
queries: false
use_second_level_cache: false
use_query_cache: false
configLocations: classpath:hibernate.cfg.xml
and we moved the actual hibernate.cfg.xml file to the /src/main/resources folder.
However, the application does not seem to be able to read anything from the database. There is no error, but also no data.
When debugging, I noticed that entityPersisterMap in the MetamodelImpl class only had 8 persisters, and they were all for classes defined in the grails/domain folder (we have 8 classes there). But we would expect several hundreds of entries in this map, as there are that amount of tables.
the xml file looks like:
<hibernate-configuration>
<session-factory>
<property name="hibernate.session_factory_name">Hibernate_Session_Factory</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</property>
<!-- connection settings -->
<property name="hibernate.connection.release_mode">after_transaction</property>
<!-- This should be equal to Constants.HIBERNATE_BATCH_SIZE -->
<property name="hibernate.jdbc.batch_size">100</property>
<!-- Debugger helpers -->
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<!-- https://hibernate.atlassian.net/browse/HHH-9106 -->
<property name="hibernate.event.merge.entity_copy_observer">allow</property>
<!-- Mappings, only needed to help GROM, otherwise all the mappings are done with annotations -->
<mapping package="com.server.metafields.db"/>
<mapping class="com.server.metafields.db.MetaField"/>
<mapping class="com.server.metafields.db.MetaFieldValue"/>
<mapping class="com.server.metafields.db.MetaFieldGroup"/>
...
</session-factory>
</hibernate-config>
We are using grails 4.0.6
Anybody seeing anything we are doing wrong ?

How to use SSL encryption when connecting Vaadin App with Hibernate to MariaDB on Azure

I am currently working on a Java 8 app with Vaadin and Hibernate which I am trying to migrate to Azure for testing purposes.
Everything worked out so far except for one thing:
When I activate the require SSL option in The MariaDB on Azure, I can't connect anymore:
Caused by: java.sql.SQLException: SSL connection is required. Please specify SSL options and retry.
Unfortunately I didn't write this app myself and I am not too familiar with neither Vaadin nor Hibernate or even Java in general and how to establish ssl or db connections with those.So I need some help:
This is the JDBC Connection string in the Servlet.java file, where for my understanding the db init happens:
Connection con = DriverManager.getConnection("jdbc:mysql://<username>.mariadb.database.azure.com:3306/<db>?autoReconnect=true", "<username>", "<password>");
which I changed by just adding this to the URL: &useSSL=true&requireSSL=true&verifyServerCertificate=true
Now the app doesn't stop at the DB init anymore but still crashes before the page is fully rendered, with the same error message.
I found a hibernate.cfg.xml with the following content (I removed the mappings to keep it shorter):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Hibernate -->
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- Hikari -->
<property name="hibernate.connection.provider_class">com.zaxxer.hikari.hibernate.HikariConnectionProvider</property>
<property name="hibernate.hikari.dataSourceClassName">com.mysql.jdbc.jdbc2.optional.MysqlDataSource</property>
<property name="hibernate.hikari.dataSource.url">jdbc:mysql://dbname.mariadb.database.azure.com:3306/db</property>
<property name="hibernate.hikari.dataSource.user">username</property>
<property name="hibernate.hikari.dataSource.password">password</property>
<property name="hibernate.hikari.dataSource.cachePrepStmts">true</property>
<property name="hibernate.hikari.dataSource.prepStmtCacheSize">250</property>
<property name="hibernate.hikari.dataSource.prepStmtCacheSqlLimit">2048</property>
<property name="hibernate.hikari.dataSource.useServerPrepStmts">true</property>
</session-factory>
</hibernate-configuration>
What do I have to change there to make use of SSL for the connection?
You may try:
<property name="hibernate.hikari.dataSource.url">jdbc:mysql://dbname.mariadb.database.azure.com:3306/db?useSSL=true</property>
Per my understanding, the properties in hibernate.cfg.xml will finally be used to generate a whole connection string. So, if there is no direct property for useSSL, you may add it to the url manually.

Hibernate fails to open Connection with Oracle 11g?

I made a basic JUnit test to set up this Oracle database on my computer with hibernate. The database works and everything, but trying to hook it up to Hibernate is proving to be a challenge. My config file can be here:
The JUnit test is fairly straight forward and I'm sure it should work, but I'm getting this JUnit failure:
org.hibernate.exception.JDBCConnectionException: Cannot open connection
Any ideas what's wrong with it?
Connection properties in Hibernate config file:
<session-factory>
<property name="hibernate.connection.driver_class">
oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">
jdbc:Oracle:thin:#127.0.0.1:8080/slyvronline</property>
<property name="hibernate.connection.username">
YouNoGetMyLoginInfo</property>
<property name="hibernate.connection.password">
YouNoGetMyLoginInfo</property>
<property name="dialect">
org.hibernate.dialect.OracleDialect</property>
<!-- Other -->
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<!-- Mapping files -->
<mapping class="com.slyvr.pojo.Person"/>
</session-factory>
It's unlikely (but possible) your DB is listening on port 8080. Oracle defaults to port 1521. Start there.
(Since it's a connection issue, relevant portions of Hibernate config are useful; I've edited to reflect.)
There are possible two issues in your connection string
first is the port that Dave Newton, second that after port you should add the sid after : not /.
So try this as a solution:
jdbc:Oracle:thin:#127.0.0.1:1521:slyvronline
When you are connecting with oracle, no need to mention the schema name so the connection URL looks like as below
jdbc:oracle:thin:#<hostname>:<port>:<sid>
ex:
jdbc:oracle:thin:#localhost:1521:xe

Different persistence.xml properies for test run / JPA Fixtures

I develop some Java EE/Spring web-app. I use JPA 2.0 - Hibernate. For integration tests I need to use different database. Those tests require Jetty to run application, but I managed to override web.xml for such run, there I can modify my Spring context files, it's ok.
But I need each time a clean database (and load some data into it).
As my database name and address are configured in sprig context I just switched them as I described above - but how can I change some of my persistence.xml properies for this tests only to have database drop and recreated?
I tried to make another persistence.xml in /src/test/resources/META-INF (and checked that test-classes are first in classpath) but it is not loaded and only the 'master' version is used (from /src/main/resources/META-INF). Any help?
With spring you usually define your data source as a spring bean. The database url and credentials are usually included form an external file, for example application.properties.
If you put a new applicaiton.properties in src/test/resources it will work. See also here.
You can define org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager :
<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>/path/to/my/test-persistence.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="dataSource" value-ref="dataSource"/>
</map>
</property>
<!-- if no datasource is specified, use this one -->
<property name="defaultDataSource" ref="dataSource"/>
</bean>
Then, link it to your entityManagerFactory :
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
...
...
<property name="persistenceUnitManager" ref="pum"/>
</bean>
I used this to make my own persistence.xml linked to a HSQL in-memory db, preloaded with DBUnit (using hibernate.hbm2ddl.auto=create-drop).
It Works perfectly.

Memcached Provider for NHibernate not working

I've been at this for days. I have configure my web/app config to use the second level cache with a Memcached server and the provider from NHContrib. I don't get any exceptions yet in testing I see that it does not use the cache for my queries that I have set cacheable = true.
If I switch the provider to the NHibernate.Cache.HashtableCacheProvider and test it works as expected.
here are the relevant config sections I am using
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
<section name="memcache" type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" />
</configSections>
<memcache>
<memcached host="192.168.215.60" port="11211" />
</memcache>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
MT.Core.Persistence.Dialect, MT.Core
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Server=192.168.1.1;Initial Catalog=Test;User ID=TestUser;Password=fakepassword;
</property>
<property name="show_sql">true</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu</property>
<property name="cache.provider_class">NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache</property>
<!--<property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</property>-->
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
</session-factory>
</hibernate-configuration>
</configuration>
The problem ended up being due to a connectivity problem. I used log4net to log any errors to the console and to the application log. It was then I finally saw the errors regarding connecting to the memcached server. Once the code was promoted to a server in the same location the errors were gone. I should have learned to use log4net ages ago.
For memcache the property is 'default_expiration' not 'expiration'. I am not sure about SysCache. But I have used this property for memcache and it works for me.
Initailly I also faced the same error that CountCet mentioned. The attribute 'expiration' is not recognized by the MemCache provider. Later I checked the code and found that it use the property 'default_expiration' and its default value is 300 sec.
I think that the expiration property should set for the memcache provider on the session factory level and not on the provider configuration like others (SysCache)
<property name="expiration">300</property>

Categories

Resources