I can't seem to find an example of hibernate properties for mysql.
Is there a link that has an example?
I have one for hsql:
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/> -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.jdbc.batch_size=0
</value>
</property>
</bean>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost/firsthibernate</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">r</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- thread is the short name for
org.hibernate.context.ThreadLocalSessionContext
and let Hibernate bind the session automatically to the thread
-->
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>
<!-- mapping files -->
<mapping resource="de/laliluna/example/Honey.hbm.xml" />
</session-factory>
</hibernate-configuration>
Based on your current Spring config file I think it would be:
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/> -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.url=jdbc:mysql://localhost/mydb
hibernate.connection.username=dbuser
hibernate.connection.password=dbpass
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.jdbc.batch_size=0
</value>
</property>
</bean>
Related
I am using a 5.4.15.Final version of hibernate. When I am running my application in create mode it is not dropped and create the tables. Is there any way I can do it? I remember I was able to do the same in older version(don't exactly remember the version). My hibernate.cfg.xml file is below:
hibernate.cfg.xml
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/test</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">5</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.default_schema">test</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="output.record.batch.size">10</property>
<property name="javax.persistence.schema-generation.create-source">metadata</property>
<property name="javax.persistence.schema-generation.scripts.action">create</property>
<property name="javax.persistence.schema-generation.database.action">create</property>
<property name="hibernate.hbm2dll.create_namespaces">true</property>
<property name="javax.persistence.schema-generation.scripts.create-target">sql/executors_create.sql</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL81Dialect</property>
</session-factory>
</hibernate-configuration>
I am creating the sessionFactory object as below:
SessionFactory sessionFactory = new Configuration().configure().addAnnotatedClass(Test.class).buildSessionFactory();
Is there any way to solve this?
I have the following sessionFactory Bean definition:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jtaTransactionManager" ref="transactionManager" />
<property name="annotatedClasses">
<list>
<value>com.badmitrii.db.entity.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- Here is a crowd of props -->
</props>
</property>
</bean>
I think it would be unconvinient to put all of the properties I need into the spring configuration file. Is there a way to move the crowd of props into a separate resource file and apply it load the bean?
You need to use the configLocations LocalSessionFactoryBean property and configure it to where the hibernate.cfg.xml file is located:
classpath:hibernate.cfg.xml
The hibernate.cfg.xml will contain all your Hibernate properties:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
</session-factory>
</hibernate-configuration>
I am trying to implement C3P0 into my hibernate. I have as follows:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://IPaddress</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.current_session_context_class">thread</property>
<!-- JDBC connection pool (use the built-in) -->
<!--<property name="connection.pool_size">20</property>-->
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">1000</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">864000</property>
<property name="hibernate.c3p0.idleConnectionTestPeriod">30</property>
<property name="hibernate.c3p0.initialPoolSize">10</property>
<property name="hibernate.c3p0.maxPoolSize">100</property>
<property name="hibernate.c3p0.minPoolSize">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Mappings -->
<mapping class="com.nebuilder.ats.pojo.TopicsDetails"/>
<mapping class="com.nebuilder.ats.pojo.GroupsDetails"/>
<mapping class="com.nebuilder.ats.pojo.ModulesDetails"/>
<mapping class="com.nebuilder.ats.pojo.TraineesDetails"/>
<mapping class="com.nebuilder.ats.pojo.ColoursDetails"/>
<mapping class="com.nebuilder.ats.pojo.CustomersDetails"/>
<mapping class="com.nebuilder.ats.dao.MusicStoreDaoImpl"/>
</session-factory>
</hibernate-configuration>
ApplicationContext.xml
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:connectionProperties="${jdbc.connectionProperties}"/>
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
Both of the files are in my resources folder. They seem to be working, but the problem is that my page keeps loading or refreshing without displaying any information when I try to access the database.
I am using jars as follow - hibernate-c3p0 3.6.3.Final, hibernate-core 3.6.3.Final, c3p0 0.9.1.2
Did you try to debug your application to see where is the problem ?
Moreover, it seems to be a little bit excessive to define a timeout of 864 000 ms, isn't it ?
I am getting the following error when I am using a combination of Spring, Hibernate and SQL Server.
19:17:09,137 ERROR [org.hibernate.tool.hbm2ddl.SchemaValidator] (MSC service thread 1-8) HHH000319: Could not get database metadata: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host falmumapp20/testdb, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
It could not be just a TCP IP problem, because if I work without Spring, I am able to connect to SQL Server using hibernate.
Below is my applicationContext.xml
<!-- Resource Definition -->
<!-- Data Source Connection Pool -->
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:microsoft:sqlserver://falmumapp20:1433" />
<property name="username" value="tima"/>
<property name="password" value="chalk#"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/trun/hbm</value>
</list>
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--Definition of Transaction Manager-->
<bean id="myTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="IDataService" class="com.trun.service.DataServiceImpl">
</bean>
<bean id="EventServlet" class="com.trun.servelet.LoginServelet">
<property name="IDataDAO" ref="IDataDAO" />
</bean>
And here is my Hibernate Configuration file -
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://falmumapp20:1433;databaseName=testdb; user=tima;password=chalk#;</property>
<property name="hibernate.connection.username">tima</property>
<property name="hibernate.connection.password">chalk#</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<!-- Mapping files -->
</session-factory>
You are not passing database name to the connection url
<property name="url" value="jdbc:sqlserver://falmumapp20:1433;databaseName=testdb" />
or
<property name="url" value="jdbc:sqlserver://falmumapp20:1433/testdb" />
i am getting Null user or password not supported in THIN driver when configure jpa with hibernate configuration in seam
components.xml
<persistence:hibernate-session-factory name="hibernateSessionFactory" cfg-resource-name="hibernate.cfg.xml"/>
<persistence:managed-hibernate-session name="session"
auto-create="true"
session-factory-jndi-name="java:/mobeeSessionFactory"/>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory name="java:/mobeeSessionFactory">
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:mobee</property>
<property name="hibernate.connection.username">mobeemigrate</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_entity_mode">pojo</property>
<property name="hibernate.session_factory_name">java:/mobeeSessionFactory</property>
<property name="hibernate.connection.datasource">mobeeadminDataSource</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Here are the mappings -->
<mapping class="tempCustomers" package="com.manam.mobee.persist.entity.TempCustomers"/>
<mapping class="tempAccounts" package="com.manam.mobee.persist.entity.TempAccounts"/>
</session-factory>
</hibernate-configuration>
projectname-ds.xml
<local-tx-datasource>
<jndi-name>mobeeadminDataSource</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:oracle:thin:#localhost:1521:mobee</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<user-name>mobeemigrate</user-name>
<password>mobeemigrate</password>
</local-tx-datasource>
The password is missing from hibernate.cfg.xml.
To fix include the following in your hibernate.cfg.xml file as shown below:
<property name="hibernate.connection.password">mobeemigrate</property>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory name="java:/mobeeSessionFactory">
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:mobee</property>
<property name="hibernate.connection.username">mobeemigrate</property>
<property name="hibernate.connection.password">mobeemigrate</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_entity_mode">pojo</property>
<property name="hibernate.session_factory_name">java:/mobeeSessionFactory</property>
<property name="hibernate.connection.datasource">mobeeadminDataSource</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Here are the mappings -->
<mapping class="tempCustomers" package="com.manam.mobee.persist.entity.TempCustomers"/>
<mapping class="tempAccounts" package="com.manam.mobee.persist.entity.TempAccounts"/>
</session-factory>
</hibernate-configuration>
Your hibernate.cfg.xml has the following property:
<property name="hibernate.connection.username">mobeemigrate</property>
But not the corresponding password one:
<property name="hibernate.connection.password">mobeemigrate</property>