A couple of questions about the use of Hibernate and ORM with Oracle

I am thinking of writing a tool that will list all the tables in an oracle database. Clicking on any of the links for each of the table will load the data from that table.

Usually i just use plain old jdbc with standard sql queries or stored procedures in the code. This has worked fine for me so far but i am wondering if using hibernate will help and reduce the work load. It is also a good way to learn about hibernate.

Could you please let me know if hibernate can help and how. i can think of the following reasonings

  • No need to write the queries
  • No need to manage queries
  • Managing the transactions will probably be easier.

There are also some issues which im not sure what the answers are. For example, the database i will be working with is quite old and not all the table have primary keys. Reading up on tutorials about hibernate, the following questions have arisen

  • Do all tables have to have a primary key named "id"? None of my tables have a column called id. The primary keys are named something else
  • Do tables have to have primary keys? Not all of my tables have primary keys. Especially tables with standing data.
  • Some tables have primary keys as compound keys (The primary key is comprised of 2 columns). Would these be allowed?

I would also be interested in any simple tutorials. I have seen a couple but none are intended for newbies to hiberate.

Thanks


Could someone please provide situations/example scenarios where hibernate becomes invaluable

Thanks

Answers


Usually i just use plain old jdbc with standard sql queries or stored procedures in the code. This has worked fine for me so far but i am wondering if using hibernate will help and reduce the work load.

This would be pretty straightforward to implement with JDBC and won't require writing many queries if you use DatabaseMetaData and ResultSetMetaData to do things dynamically.

With Hibernate, you would have to generate entities from the physical model. That's possible, Hibernate provides tooling for this. But I'm not convinced Hibernate will give you any advantage here (not a CRUD app, JDBC would just work). So I agree with @duffy, Hibernate might even give you more work.

It is also a good way to learn about hibernate.

I can understand that. But this is not the best application to learn Hibernate in my opinion.

Do all tables have to have a primary key named "id"? None of my tables have a column called id. The primary keys are named something else

No, this is not required, you can map any column name as primary key.

Do tables have to have primary keys? Not all of my tables have primary keys. Especially tables with standing data.

If you don't have any unique column, it will be a problem, Hibernate expects entities to have an identifier.

Some tables have primary keys as compound keys (The primary key is comprised of 2 columns).

That's supported.


I think Hibernate will increase your workload. ORM means objects, so you'll have to write objects to map the Oracle tables to.

I don't think this is an appropriate use of Hibernate. JDBC is fine for this case. By all means proceed if you'd like to learn Hibernate, but I can think of several smaller problems where it would be more appropriate.


Consider Hibernate as an investment. Just like learning Spannish or Martial Arts. It's hard at the beginning, but after you passed the dip, you'll get huge benefits.

To answer your questions:

  • You can use any primary key you want with Hibernate

  • Hibernate doesn't support tables without a primary key, but why you would want that?

  • Yes compound keys are supported

Consider buying Hibernate in Action. Start from there.


In some cases we opt for using unique constraints over primary keys to improve performance especially on tables that have no relationship to any other tables. In some cases we avoid primary keys and even normalising a table to prevent unique indexes being created and the need for Oracle to update and re-calculate indexes when there are changes.

I am not a dba and i know there are several arguments for and against the use of primary keys but i dont really want to go into that for now.

To be honest, the reason i want to use hibernate is purely for my own benefit in that i would like to learn how to use it. Given that i have an opportunity to write this tool and that it is not a mission critical tool and will only be used "in-house" i decided that i should try hibernate and by learning it i might find it usefull later on if i need it on other projects.

I dont currently have a requirement to write to the database so ill only be reading from the database. As not all tables have primary keys, is it possible to trick hibernate into using the the unique column as the primary key? I think it might be possible to create VIEWS and add an id column in the view for tables that dont have a primary key or a unique constraint.


Need Your Help

How to implement basic data type for my Java project

java c++

I normally work for C++. Now coding for Java, I know that Java doesn't have typedef and I have to implement my own object for my basic data type object as

Testing rest services by using the client

java unit-testing rest

I have been an old school type of programmer and didn't really use tests in my projects. I want to change that now but since it's not an industrial project but my own I would like to ask: