Configuring hibernate.cfg.xml file - java

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).

Related

SQLite dialect into Hibernate-Maven project

i am a java student and i have to do a small project. I have to use Maven and Hibernate ( no Spring frameworks). I use IntelliJ as IDE. The thing is that my teacher recomended me to use SQLite as RDBMS because its very simple, 1 file and there is no need to implement a server inside my app. ( i have no idea to do the last point).
The problem is that when i try to do the "hibernate.cfg.xml " i have no way to configure it because of the lack of information. Seems there is no dialect supported from Hibernate and the info i could find on internet is outdated. Any idea on how i can configure it? Do i really need to use another RDBMS ?
there is a picture of my project structure
this is my hibernate.cfg.xml:
<?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="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite://db/appiculturedb.sqlite3</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- maping with xml-->
<mapping resource="Bodega.hbm.xml" />
<!-- maping with anotations-->
<!-- <mapping class="com.stephane.Bodega" /> -->
</session-factory>
</hibernate-configuration>
After searching for few days, i found that you can use SQLite with Hibernate 5 if you create your own dialect. You also can try to use someone else dialect wich may be risky or outdated. Check this link for more info.
For the moment, hibernate do not support oficially SQLite. Maybe in the future that will change.
If you are searching for an embedded database, you can use H2 wich is supported by Hibernate and seems to be recommended by them.
You also have HSQLDB or Apache Derby.

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>

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 !

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.

MappingNotFoundException when running queries in IntelliJ Hibernate console

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

Categories

Resources