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

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 ?

Related

SQLite dialect into Hibernate-Maven project

i am a java student and i have to do a small project. I have to use Maven and Hibernate ( no Spring frameworks). I use IntelliJ as IDE. The thing is that my teacher recomended me to use SQLite as RDBMS because its very simple, 1 file and there is no need to implement a server inside my app. ( i have no idea to do the last point).
The problem is that when i try to do the "hibernate.cfg.xml " i have no way to configure it because of the lack of information. Seems there is no dialect supported from Hibernate and the info i could find on internet is outdated. Any idea on how i can configure it? Do i really need to use another RDBMS ?
there is a picture of my project structure
this is my hibernate.cfg.xml:
<?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>
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite://db/appiculturedb.sqlite3</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- maping with xml-->
<mapping resource="Bodega.hbm.xml" />
<!-- maping with anotations-->
<!-- <mapping class="com.stephane.Bodega" /> -->
</session-factory>
</hibernate-configuration>
After searching for few days, i found that you can use SQLite with Hibernate 5 if you create your own dialect. You also can try to use someone else dialect wich may be risky or outdated. Check this link for more info.
For the moment, hibernate do not support oficially SQLite. Maybe in the future that will change.
If you are searching for an embedded database, you can use H2 wich is supported by Hibernate and seems to be recommended by them.
You also have HSQLDB or Apache Derby.

Hibernate xml file issue

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.

Spring Bootstrap Context for dropping Tables should they exist

I am trying to create an application bootstrap that will drop all the tables in the application if they exist and then intialise them with fresh data.
I have created a Spring Context that loads the datasource context - however I dont know how to override the initialisation of the datasource such that the behaviour can be customised depending on how the datasource is loaded. So.. using Hibernate as my JPA implementation..
If the datasource is loaded from the application - then I would like the schemas to update:
<persistence-unit name="myDB" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
If the datasource is loaded from the bootstrap - then I need to overload this behaviour somehow so that the database is always created from scratch before fresh data is loaded:
<persistence-unit name="myDB" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
The approach I have been taking doesn't work as I would load the datasource using the 'update' setting and then drop the tables if they exist before attempting to load new data. However - the tables no longer exist for writing data !
Thanks in advance
Simon
You can pass JPA properties from Spring configuration instead of persistance.xml and use placeholder that can be configured by PlaceholderConfigurer (possibly system-properties="OVERRIDE"), or Spring profiles (since 3.1) or using Maven filtering:
<util:map id="jpaPropertyMap" key-type="java.lang.String" value-type="java.lang.Object">
<entry key="hibernate.hbm2ddl.auto" value="${database.ddl.mode}" />
</util:map>
<bean id="managementEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:jpaPropertyMap-ref="jpaPropertyMap" />

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

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