MappingNotFoundException when running queries in IntelliJ Hibernate console - java

I'm trying to run HQL queries in IntelliJ's Hibernate Console. I've added the datasource to the Data Sources view and added my hibernate.cfg.xml to the Hibernate facet such that in appears in the Persistence view. The content of hibernate.cfg.xml is:
<?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="connection.url">jdbc:mysql://localhost/mmanager</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<property name="dialect">com.puca.core.util.db.MySQLInnoDBDialect</property>
<!-- mapping files -->
<mapping resource="com/puca/messagemanager/api/model/XmlApiDlrMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The .hbm.xml is shown in green, whereas if I change it to a file that doesn't exist it is shown in red, so it seems that IntelliJ can find it. However, if I run a simple query in the console like:
from XmlApiDlrMapping xmlApiDlrMapping
where xmlApiDlrMapping.retries = 5
I get an error:
java.lang.RuntimeException: org.hibernate.MappingNotFoundException: resource: com/puca/messagemanager/api/model/XmlApiDlrMapping.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:563)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
I'm using IntelliJ 10.5.4.

It's exactly what it says in the error message. The mapping XML file is missing:
com/puca/messagemanager/api/model/XmlApiDlrMapping.hbm.xml
These files are used to map table columns to your entity class fields.
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/tutorial.html#tutorial-firstapp-mapping

Related

How to generate model code for "Single DB" using Hibernate Tools through Hibernate Reverse Engineering

I use the Hibernate Reverse Engineering through JBoss Tools > Hibernate Tools to generate Model Classes.
There, when I add the Hibernate Configuration and Run it / Rebuild It, it list all the DBs although I mention only one DB in the hibernate.cfg.xml's hibernate.connection.url.
eg : jdbc:mysql://localhost:3306/booksdb.
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"-//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.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/booksdb</property>
<property name="hibernate.connection.username">xxxx</property>
<property name="hibernate.connection.password">xxxx</property>
</session-factory>
</hibernate-configuration>
Then when I generate the entities code it scans all other Database tables also to generate the entity codes for all. Sometimes it impact by some duplicated tables in different DBs.
Question :
How to do this for single database mentioned in the hibernate.connection.url?
Hibernate Code Generation Configurations > Main
Hibernate Code Generation Configurations > Exporters
Hibernate version : 5.4
MySQL version Info
innodb_version:5.7.26
protocol_version:10
version:5.7.26-0ubuntu0.18.04.1
version_comment:(Ubuntu)
version_compile_machine:x86_64
version_compile_os:Linux
In this screen go to setup near reveng.xml. It will ask you to create a xml, from there you'll be able to exclude the databases you don't want.
<hibernate-reverse-engineering>
<table-filter match-name=".*" exclude="true" match-catalog="YOUR_DATABASE_NAME" />
</hibernate-reverse-engineering>

I'm getting XMLStreamException at [row,col]:[1,2]

I suddenly have started getting XMLStreamException in my project. Yesterday it worked just fine, today I made some minor changes and it's broken. I tried to rebase to previous versions, but nothing changed at all.
Here is my xml file:
<?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="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5433/logistica</property>
<property name="hibernate.connection.username">testUser</property>
<property name="hibernate.connection.password">pass</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
<mapping class="com.log.iei.logistica.data.entities.ClientEntity" />
<mapping class="com.log.iei.logistica.data.entities.ContractEntity" />
<mapping class="com.log.iei.logistica.data.entities.ContainerEntity" />
<mapping class="com.log.iei.logistica.data.entities.VehicleEntity" />
<mapping class="com.log.iei.logistica.data.entities.TransactionEntity" />
</session-factory>
</hibernate-configuration>
And exception:
org.hibernate.HibernateException: Error accessing stax stream
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:107)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:165)
at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
at com.log.iei.logistica.managers.HibernateSessionManager.getSessionFactory(HibernateSessionManager.java:18)
at com.log.iei.logistica.data.controllers.Services.GenericDao.getSession(GenericDao.java:78)
at com.log.iei.logistica.data.controllers.Services.GenericDao.findAll(GenericDao.java:58)
at com.log.iei.logistica.data.controllers.Services.VehicleService.findAll(VehicleService.java:50)
at com.log.iei.logistica.gui.cargo.CargoPage.init(CargoPage.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
.........
.......
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,2]
Message: The markup declarations contained or pointed to by the document type declaration must be well-formed.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
at com.sun.xml.internal.stream.XMLEventReaderImpl.peek(XMLEventReaderImpl.java:276)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:103)
... 36 more
I began having the same issue. I hadn't updated anything, but restarting tomcat failed with lots of new exceptions. I noticed the below file was updated today and wondered if it was the source (it is referenced in each of my entity mappings):
http://hibernate.org/dtd/hibernate-mapping-3.0.dtd
So I updated my mappings to use the DTD from here:"hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" - and that works. So it does appear to be caused by the Hibernate's updated DTD file.
I guess your options are:
figure out what hibernate wants in the updated dtd
use the dtd from another source like sourceforge
use the dtd from your hibernate jar like this: "classpath://org/hibernate/hibernate-mapping-3.0.dtd"

Netbeans 8.x + hibernate + postgreSQL : No tables found in reverse engineering

I use neatbeans with hibernate + postgreSQL.
In postgreSQL I have 1 table : "test" with 3 columns.
I created the hibernate.cfg.
<?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.PostgreSQLDialect</property>
<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>
</session-factory>
</hibernate-configuration>
Now I want to create the reverse engineering file for map my table, the problem is I haven't output (in table field).
In my libraries I have:
Hibernate 4.3.x
postgresql-9.3-1102.jdbc41
JDK 8
With NetBeans, when I go to Service area I can connect to my database, and see my tables with his values.
Maybe an idea, when I created the hibernate.cfg, I chose my new Driver -> Add postgresql-9.3-1102.jdbc41.jar and when I click on "find" Driver Class I have the error message: Driver class is missing. I don't know if that could be the problem.
Do you have any idea ?
Thanks for your help !

Configuring hibernate.cfg.xml file

I am new to Hibernate, so I have few dumb questions hope someone would be able to assist me. I have a query in regards to configuring hibernate file i.e. hibernate.cfg.xml file. Normally we configure it as below :
<?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="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Where the considers relevant mapping files. Lets say I have another mapping file, call it company.hbm.xml file and have a one-to-many relationship with employee.hbm.xml file. So, as and when the number of mapping files increases we include them into tag.
Query : How is it different to include multiple mapping files with individual tag from having just a single mapping file with all the relevant mappings.
Appreciate your response
Because such separation is cleaner. Single file with whole configuration tends to grow up and to become a yet another huge, unmaintainable and unreadable XML file.
Please also note, that using annotations right inside your model classes seems to be the right way for you (just as #Shiju Babu stated).

Hibernate: Can't connect to MySQL database

I'm struggling to get my program to connect to a MySQL database with Hibernate.
I simply want to connect to it so i can usee Hibernate tools to reverse engineer a few tables in the database.
When i use MySQL Workbench to connect to the database from the same machine, it works.
Here's my hibernate.cfg.xml 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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://www.soosthebasement.nl:3306/soos_thebasement</property>
<property name="hibernate.connection.username">removed</property>
<property name="hibernate.connection.password">removed</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
The password contains some weird chars, like >, which when creating the cfg file got parsed into >. Could that have something to do with it?
Turns out i used the wrong URL.
Even though the URL listed in the question points to the correct website, it doesn't actually point to the database host.

Categories

Resources