How to connect with hibernate file to SQL server database? - java

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

Related

How to add JNDI Based on my Config file

I have been trying since hours to set up JNDI in my project
My Hibernate file is like this :
<?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.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">mypass</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#big-data-3.logti.etsmtl.ca:1521:LOG660</property>
<property name="hibernate.connection.username">myTeam</property>
<property name="hibernate.default_schema">teamAlpha</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<mapping resource="Scenariste.hbm.xml" />
<mapping resource="Utilisateur.hbm.xml" />
<mapping resource="Annonce.hbm.xml" />
<mapping resource="Genre.hbm.xml" />
<mapping resource="Location.hbm.xml" />
<mapping resource="Pays.hbm.xml" />
<mapping resource="Personnelfilm.hbm.xml" />
<mapping resource="Clientenretard.hbm.xml" />
<mapping resource="Film.hbm.xml" />
<mapping resource="Forfait.hbm.xml" />
<mapping resource="Employe.hbm.xml" />
<mapping resource="Client.hbm.xml" />
<mapping resource="Personneltournage.hbm.xml" />
</session-factory>
I'm aware to add JNDI it has to be something similar to this
hibernate.connection.datasource = java:/comp/env/jdbc/test
hibernate.transaction.factory_class = \
org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
But how to fill the datasource and factory class and lookup class based on my info? my database name is teamAlpha
I'm using JBOSS pluggin for eclipse ,
Thank you

Mysql database hang when processlist goes above 100 connections in raspberry pi

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!

What is the configuration to use for the combination MySQL-Server/Apache Tomcat and a Tapestry project using Hibernate/JPA?

I have a MySQL Server running on an Apache Tomcat.
I also have created a Tapestry project via Quickstart Archetype. I have then added dependencies to tapestry-hibernate and and the mysql connector in the POM.
<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-hibernate</artifactId>
<version>${tapestry-release-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
I have created an entity with the appropriate annotations (Entity, Id, Table, Column, etc.).
I do not want to use the Hibernate API, but the Hibernate JPA "subset".
I have a persistence.xml in the java/main/resources/META-INF folder and a hibernate.cfg.xml in the java/main/resources folder.
The persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="2.0">
<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value = "true" />
<property name="hibernate.format_sql" value = "true" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/someDB" />
<property name="javax.persistence.jdbc.user" value="someUser" />
<property name="javax.persistence.jdbc.password" value="somePassword" />
</properties>
</persistence-unit>
</persistence>
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>
<!-- For use of Hibernate with a MySQL-DB over a webserver -->
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/someDB"</property>
<property name="hibernate.connection.username">someUser</property>
<property name="hibernate.connection.password">somePassword</property>
<!-- List of XML mapping files -->
</session-factory>
</hibernate-configuration>
I started the tapestry application using jetty:run.
The problem: The app starts, I can view the index page, but the database isn't accessed or modified.
I want the database and tables to be created automatically. I know that hibernate.hbm2ddl.auto=create creates the tables. Does it also create the database or does it have to be created manually?
I only have one entity and the basic tapestry pages in my code. Do I explicitly have to at least define an EntityManager to make the program create the tables?
Do I have to have both the persistence.xml and the hibernate.cfg.xml? It seems some properties are redundant...
If I want to use Hibernate just as a JPA implementation and not the hibernate-specific API, are the properties in the configuration files the correct ones?
First of all add the dependencies in the pom for the mysql -
<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-hibernate</artifactId>
<version>${tapestry-release-version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hibernate-tapestry-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-version}</version>
</dependency>
and modify your hibernate.cfg.xml file with the following configuration
<!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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tapestry_db?createDatabaseIfNotExist=true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
This is working for me.

How to make different Hibernate configuration for tests?

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

Hibernate org.hibernate.MappingException: invalid configuration

I started learned hibernate. But when i try use hibernate.cfg.xml. I see on the console:
Exception in thread "main" org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1931)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1910)
at testHibernate.HibernateStart.main(HibernateStart.java:28)
Caused by: org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 49; Attribute "class" must be declared for element type "mapping".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(XMLDTDValidator.java:1253)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1917)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:763)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:353)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2715)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2011)
... 3 more
And hibernate.cfg.xml file.
> <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE
> hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration
> DTD 2.0//EN"
> "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
>
> <hibernate-configuration
> xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
> xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration
> hibernate-configuration-4.0.xsd"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <session-factory>
> <!-- Database connection settings -->
> <property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
> <property name="connection.url">jdbc:derby:/home/user/DataBase/db;create=true</property>
> <property name="connection.username"></property>
> <property name="connection.password"></property>
>
> <!-- JDBC connection pool (use the built-in) -->
> <property name="connection.pool_size">1</property>
>
> <!-- SQL dialect -->
> <property name="dialect">org.hibernate.dialect.DerbyDialect</property>
>
> <!-- Enable Hibernate's automatic session context management -->
> <property name="current_session_context_class">thread</property>
>
> <!-- Disable the second-level cache -->
> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
>
> <!-- Echo all executed SQL to stdout -->
> <property name="show_sql">true</property>
>
> <!-- Drop and re-create the database schema on startup -->
> <property name="hbm2ddl.auto">update</property>
> <mapping class="testHibernate.UserDetails"/> </session-factory> </hibernate-configuration>
I solve my problem, file shoud looks like:
?xml version='1.0' encoding='utf-8'?>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="connection.url">jdbc:derby:/home/user/DataBase/db;create=true</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="testHibernate.UserDetails"/>
</session-factory>

Categories