Changing Database from mysql to oracle using Hibernate - java

Hi I have a small java project and am using hibernate with it. Now I would like to change the database to oracle, what changes would I have to do in configuration file to make it possible.

In you hibernate.cfg.xml, you should have a dialect defined. The line looks like this:
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
change it to this:
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
Also change the database connection parameters in the same file.

Modify these properties. In hibernate.cfg.xml
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">your_new_password</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#loalhost:xe</property>
<property name="hibernate.connection.username">user_name</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

Related

Can't realize PUT requests with c3p0

After (early all works ok) setting c3p0 connection pool to PostgreSQL DB like this:
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">500</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
can't realize PUT (only) requests. Hibernate works ok, but there are no records in DB. I tried different sets without result. What's wrong?
UPD: as we see in DB: PUT requests comes (they correct) but Hibernate rollback them. Why?
Find solution:
<property name="hibernate.c3p0.autoCommitOnClose">true</property>
It's works. But i'am not sure in it correction.

Hibernate generates mapping files for the wrong database

I am trying to generate mapping files for DB_NAME, but hibernate generates mapping files for "master" database instead of "DB_NAME". What do I need to do to get hiberate to generate mapping files for "DB_NAME"?
Below is connection property I used in my hibernate.cfg.xml.
I created a TestConnection class to see if I can access DB_NAME. I am able to retrieve data from DB_NAME using the above connection url. I use sql server 2008 and sqljdbc4.jar is in my build path.
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.password">PASSWORD</property>
<property name="hibernate.connection.url">jdbc:sqlserver://SERVER_NAME\INSTANCE_NAME;DatabaseName=DB_NAME</property>
<property name="hibernate.connection.username">USERNAME</property>
<property name="hibernate.default_schema">DBO</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

Hibernate + SQLite db outside jar

After few years of PHP I decided to learn Java, so I'm writing simple desktop app that uses Hibernate and SQLite.
I'd like to keep database outside of the .jar generated by mvn package, so one can perform quick backup of the data just by copying one file. So far, my hibernate.cfg.xml looks like this:
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite:mydb.db</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.ex3v.hibernate.Contact"/>
</session-factory>
</hibernate-configuration>
what connection.url do I have to specify to make hibernate create db file next to .jar in working directory?
You can find your answer here:
Just a summary from above site,
different formats are :
jdbc:sqlite:mydb.db
jdbc:sqlite://dirA/dirB/mydb.db
jdbc:sqlite:/DRIVE:/dirA/dirB/mydb.db
jdbc:sqlite:///COMPUTERNAME/shareA/dirB/mydb.db
Use relative path like this.
jdbc:sqlite:./DB/mydb.db

Hibernate + embedded database - setup

I have created Java application using Hibernate with this configuration:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306 /bee</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="connection.username">root</property>
<property name="connection.password"/>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.useUnicode">true</property>
<mapping resource="DatabaseMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Everything works fine when Iam using jdbc:mysql://localhost..., but now I need to have the database embedded in my application. Which database should I use? I need to have all my data stored and load it after start the application, update data, save, delete. I use HQL query or SQL query.
What is the simplest way to make the database embedded? I donĀ“t want to change my queries. It would be fine to change only hibernate configuration and set it to the embedded database, is it possible?
One of the advantages of ORM's like Hibernate is to shield you from DB differences. You can use any one of the below as an embedded DB solution. Just change the dialect, driver and URL in hibernate cfg file.
H2
SQLite
HSQLDB
Hava a look at using the H2 database in embedded mode
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:~/test</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.default_schema">PUBLIC</property>
<mapping resource="DatabaseMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
If you don't want to change your queries, consider mysql-mxj (embedded mysql) http://dev.mysql.com/doc/connector-mxj/en/connector-mxj.html
It is not under active development (thanks Oracle). But is prefectly usable and the connector is open source. It is trivial to embed different versions of mysql, but the latest mxj connector embeds 5.5.9

How can I configure Hibernate to use SSL to talk to the DB server?

I have an existing java webapp that uses Hibernate for it's persistence. I've been told that I have to have to talk to the DB encrypted - so my first thought is to set it up to do the communication via SSL - and went through figured out how to set up Oracle to listen for JDBC over SSL -
http://www.oracle.com/technology/tech/java/sqlj_jdbc/pdf/wp-oracle-jdbc_thin_ssl_2007.pdf
And wrote a quick test class to verify that it was setup and working (connecting via standard JDBC). That left me with the issue of configuring Hibernate - unfortunately I don't see how hibernate supports it?
Hibernate works with standard JDBC data sources, so there is no need for Hibernate-specific configuration.
Here's an quick example that should work when configuring Hibernate with Spring:
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL"><value><!-- JDBC URL that specifies SSL connection --></value></property>
<!-- other relevant properties, like user and password -->
<property name="connectionProperties>
<value>
oracle.net.ssl_cipher_suites: (ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha)
oracle.net.ssl_client_authentication: false
oracle.net.ssl_version: 3.0
oracle.net.encryption_client: REJECTED
oracle.net.crypto_checksum_client: REJECTED
</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- classes etc -->
</bean>
Try this:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://blablaba:8443/dbname?useSSL=true</property>
<property name="hibernate.connection.verifyServerCertificate">false</property>
<property name="hibernate.connection.requireSSL">true</property>
<property name="hibernate.connection.autoReconnect">true</property>
<property name="hibernate.connection.username">bablablab</property>
<property name="hibernate.connection.password">clclclclc</property>
related links
http://www.razorsql.com/articles/mysql_ssl_jdbc.html
http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-using-ssl.html
http://www.javabeat.net/qna/164-hibernate-jdbc-and-connection-properties/
Please add following property in Hibernate configuration file to enable SSL :
<property name="hibernate.connection.verifyServerCertificate">false</property>
<property name="hibernate.connection.useSSL">true</property>
Should be handled by the driver but you may have to do some configuration. Oracle Docs
I had the jdbcURL
jdbc:postgresql://jdbcurl?sslmode=require&sslrootcert=location_to_cert1&sslcert=location_to_cert2&sslkey=location_to_cert3.
All I had to do was to replace all the & with &.
My new jdbcURL looks like
jdbc:postgresql://jdbcurl?sslmode=require&sslrootcert=location_to_cert1&sslcert=location_to_cert2&sslkey=location_to_cert3

Categories