I have JSF Application with Hibernate. I use Tomcat connection pool in hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
<!-- using Tomcat connections -->
<property name="connection.datasource">java:/comp/env/jdbc/netstat</property>
<!-- Parameters for Hibernate connection provider
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/netstat</property>
<property name="connection.username">netstat</property>
<property name="connection.password">netstat</property>
-->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- configuration pool via c3p0 -->
<!--
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">200</property>
<property name="c3p0.timeout">600</property>
-->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping classes -->
<mapping class="ru.gooamoko.model.Group" />
<mapping class="ru.gooamoko.model.Host" />
<mapping class="ru.gooamoko.model.Traffic" />
<mapping class="ru.gooamoko.model.DailyTraffic" />
</session-factory>
</hibernate-configuration>
Project can be build with maven with all tests skipped and runs normal. But for tests souch Hibernate configuration is not applicable.
Is any way for this case to specify different configuration file for JUnit tests?
Best regards.
put the test file in src/test/resources and that will take precedence in classpath while executing tests
Related
I have a problem because I don't know how to connect to SQL server with hibernate file I try this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database Connection settings -->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=RentApp</property>
<property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
<property name="hibernate.connection.password"></property>
<!-- Echo SQL wyswietlenie zapytania tabeli bazy danych -->
<property name="hibernate.show_sql">true</property>
<!-- SQL Dialect implementacja silnika bazo danego -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- database intialize -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- mappings -->
<mapping class="pl.edu.wszib.model.Vehicle" />
<mapping class="pl.edu.wszib.model.User" />
<mapping class="pl.edu.wszib.model.GroupVehicle" />
<mapping class="pl.edu.wszib.model.Rent" />
<mapping class="pl.edu.wszib.model.Buy" />
</session-factory>
</hibernate-configuration>
In SQL server my database name is RentApp and I use windows authentication in the Microsoft SQL Server.
So
<property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
<property name="hibernate.connection.password"></property>
username must be from Microsoft SQL Server?
And I use in my pom
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.2.2.jre11</version>
</dependency>
I have an error right that but I have username: LAPTOP-SJJ2BT5V\Konrad
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'LAPTOP-SJJ2BT5V\Konrad'.
I try this
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test;integratedSecurity=true</property>
But I have error:
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database Connection settings -->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test;integratedSecurity=true</property>
<property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
<property name="hibernate.connection.password"></property>
<!-- Echo SQL wyswietlenie zapytania tabeli bazy danych -->
<property name="hibernate.show_sql">true</property>
<!-- SQL Dialect implementacja silnika bazo danego -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- database intialize -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- mappings -->
<mapping class="pl.edu.wszib.model.Vehicle" />
<mapping class="pl.edu.wszib.model.User" />
<mapping class="pl.edu.wszib.model.GroupVehicle" />
<mapping class="pl.edu.wszib.model.Rent" />
<mapping class="pl.edu.wszib.model.Buy" />
</session-factory>
</hibernate-configuration>
and i must download sqljdbc_auth.dll and put this file to C:\Program Files\Java\jdk-11.0.1\bin
I am writing service in hibernate with c3p0 concept.And this service deployed to Raspberry pi.The raspbery pi having 512 mb RAM.I am hit the service continuously, when the processlist goes above 100 connections the mysql hanging up.And my service are not working. After restarting mysql only all processlist killed them self.Then services are working properly till the processlist reach above 100 connections.
My c3p0 configurations are(hibernate.cfg file),
<?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>
<!-- Database connection settings -->
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">mani</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<!-- <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.maxIdleTime">1800</property>
<property name="hibernate.c3p0.maxIdleTimeExcessConnections">300</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">30</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1</property>
<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">false</property>
<!-- Mention here all the pojo classes along with their package name -->
<mapping class="com.nutans.objects.Dialog" />
<mapping class="com.nutans.objects.Office" />
<mapping class="com.nutans.objects.Welcome" />
<mapping class="com.nutans.objects.Sports"/>
</session-factory>
</hibernate-configuration>
Help me out!
I am new to hibernate framework. i made a sample project just to test hibernate. i set up the project. but when i run it i got this error
org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:966)
The Hibernate.cfg.xml file that i put in the main package of the project.
<?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="hibernate.dialect">
org.hibernate.dialect.DerbyDialect
</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">2</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--<property name="htm2ddl.auto">create</property>-->
<property name="hibernate.connection.driver_class">
org.apache.derby.jdbc.ClientDriver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:derby://localhost:1527/NetworkDB
</property>
<property name="hibernate.connection.username">
</property>
<property name="hibernate.connection.password">
</property>
<!-- List of XML mapping files -->
<!-- <mapping resource="Employee.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
I am using netbeans 7.0
As per DTD, <hibernate-configuration> should have a single <session-factory> but you have declared it twice.
Regarding second error:
org.hibernate.HibernateException: /hibernate.cfg.xml not found
Hibernate looks for the configuration file at the root of the classpath, so check if you have placed this file at root of your classpath.
So once you build your project the hibernate.cfg.xml file should be in classes directory.
Add the following in first line of your xml:
<?xml version="1.0" encoding="UTF-8"?>
I followed this tutorial: http://manikandanmv.wordpress.com/2011/04/13/hibernate-basics-simple-example/
This is my file structure
http://i.stack.imgur.com/sX3nn.jpg
When I run Java Applet, I get this error
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: src/com/bookstore/bookapp.hbm.xml not found
However, I have that file there as you can see it. When I put the file under
workspace-windows\226project1\src\com\bookstore
I'm still facing this error even then, Can someone help?
This is my hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.username">postgres</property>
<property name="connection.password">rocker123</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="src/com/bookstore/bookapp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Move your mapping files into src/main/resources. Only .java files belong in src/main/java.
There is an example project on github showing how to correctly reference XML mapping files from a hibernate.cfg.xml. Please refer to https://github.com/zzantozz/testbed/tree/master/hibernate-with-xml-mappings
I'm using Tomcat server, I started it yesterday morning and it continuously runs until this morning, but when I tried to login in my application it shows:
org.hibernate.TransactionException: JDBC rollback failed
What is the problem - is any persistent session here or is sessionfactory null?
I got the problem ,
ie i attached a different jar named c3p0-0.9.1.jar file and add some lines in hibernate-cfg.xml file
The code is :
<?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="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/<your db name></property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">test</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.jdbc.batch_size">50</property>
//Here is the extra code for handling the above problem.....
**<property name="hibernate.c3p0.max_size">1</property>
<property name="hibernate.c3p0.min_size">0</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">1000</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.acquire_increment">1</property>**
</session-factory>
</hibernate-configuration>