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.
Related
I am currently working on a Java 8 app with Vaadin and Hibernate which I am trying to migrate to Azure for testing purposes.
Everything worked out so far except for one thing:
When I activate the require SSL option in The MariaDB on Azure, I can't connect anymore:
Caused by: java.sql.SQLException: SSL connection is required. Please specify SSL options and retry.
Unfortunately I didn't write this app myself and I am not too familiar with neither Vaadin nor Hibernate or even Java in general and how to establish ssl or db connections with those.So I need some help:
This is the JDBC Connection string in the Servlet.java file, where for my understanding the db init happens:
Connection con = DriverManager.getConnection("jdbc:mysql://<username>.mariadb.database.azure.com:3306/<db>?autoReconnect=true", "<username>", "<password>");
which I changed by just adding this to the URL: &useSSL=true&requireSSL=true&verifyServerCertificate=true
Now the app doesn't stop at the DB init anymore but still crashes before the page is fully rendered, with the same error message.
I found a hibernate.cfg.xml with the following content (I removed the mappings to keep it shorter):
<?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>
<!-- Hibernate -->
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- Hikari -->
<property name="hibernate.connection.provider_class">com.zaxxer.hikari.hibernate.HikariConnectionProvider</property>
<property name="hibernate.hikari.dataSourceClassName">com.mysql.jdbc.jdbc2.optional.MysqlDataSource</property>
<property name="hibernate.hikari.dataSource.url">jdbc:mysql://dbname.mariadb.database.azure.com:3306/db</property>
<property name="hibernate.hikari.dataSource.user">username</property>
<property name="hibernate.hikari.dataSource.password">password</property>
<property name="hibernate.hikari.dataSource.cachePrepStmts">true</property>
<property name="hibernate.hikari.dataSource.prepStmtCacheSize">250</property>
<property name="hibernate.hikari.dataSource.prepStmtCacheSqlLimit">2048</property>
<property name="hibernate.hikari.dataSource.useServerPrepStmts">true</property>
</session-factory>
</hibernate-configuration>
What do I have to change there to make use of SSL for the connection?
You may try:
<property name="hibernate.hikari.dataSource.url">jdbc:mysql://dbname.mariadb.database.azure.com:3306/db?useSSL=true</property>
Per my understanding, the properties in hibernate.cfg.xml will finally be used to generate a whole connection string. So, if there is no direct property for useSSL, you may add it to the url manually.
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 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 !
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).
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.