Hibernate org.hibernate.MappingException: invalid configuration - java

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>

Related

How to connect with hibernate file to SQL server database?

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

Caused by: org.postgresql.util.PSQLException: database doesn't exist when trying to auto-create database

I have this hibernate.cfg.xml:
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">
jdbc:postgresql://localhost/DatabaseName?createDatabaseIfNotExist=true
&useUnicode=yes&characterEncoding=UTF-8
</property>
<property name="connection.username">postgres</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">5</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</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="com.main.User"/>
</session-factory>
</hibernate-configuration>
which is supposed to create both tables (for the User entity and the database DatabaseName). However, it doesn't create a database and fails with an error on this line:
sessionFactory = configuration.buildSessionFactory();
What can I do to make it autocreate database titled DatabaseName?
To create database you have to create is manually you can use IDEs for that or cmd to create your DB
And To create Tables you can use create in place of update
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
It will create a schema and then you will use an update for the next startup if you don`t want to recreate your tables

Hibernate configuration error while reading schema

Hey stackoverflow,
I tried to add a Hibernate configuration to my workspace in order to reverse engineer my database.
When I am opening the database point in the 'Hibernate Configurations' view, i get a Reading schema error: null with the following exception:
java.lang.NullPointerException
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter$1.compare(LazyDatabaseSchemaWorkbenchAdapter.java:76)
at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.toArray(BasicWorkbenchAdapter.java:75)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.getChildren(LazyDatabaseSchemaWorkbenchAdapter.java:74)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:104)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:238)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Has someone experienced this before?
I am running a fresh installation of Eclipse Luna / Newest JBoss Tools and trying to connect to a PostgreSQL 9.3 DB.
Thank you in advance
EDIT:
My hibernate.cfg.xml:
<?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">org.postgresql.Driver</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.url">jdbc:postgresql://10.244.7.77:5432/netview</property>
<property name="hibernate.connection.username">admin</property>
<property name="hibernate.default_schema">public</property>
</session-factory>
</hibernate-configuration>
EDIT 2:
I just realized, if i add a default schema <property name="hibernate.default_schema"> it works, but just for this schema.
EDIT 3:
It works with an old version of Hibernate tools (3.6.0.M1-v20120827-0757-H1125). Now i am confused.
Try out this config:
<!-- Database connection settings -->
org.postgresql.Driver
jdbc:postgresql://host:port/database
postgres
password
1
org.hibernate.dialect.PostgreSQLDialect
thread
org.hibernate.cache.internal.NoCacheProvider
true
Try out this config:
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://host:port/database</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</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>
</session-factory>
So i found out that it might be a bug in JBoss Tools. So I opened a ticket: https://issues.jboss.org/plugins/servlet/mobile#issue/JBIDE-19830

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!

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

Categories