Sleep or JDBC

I have a thick client, a java swing application with a scheme of 25 tables and ~ 15 JInternalFrames (data entry forms for tables). I need to choose the design of direct JDBC or ORM (sleep mode with the spring framework in this case) for the interaction of the DBMS. Building outside the app will happen in the future.

Will hibernation overload a project of this size? An explanation of yes or no would be highly appreciated (or even a different approach, if warranted).

TIA.

+57
java jdbc hibernate
Aug 30 '09 at 5:00
source share
9 answers

Good question without a single simple answer.

I used to be a big fan of Hibernate after using it in several projects in a few years. I figured that any project should default to sleep mode.

Today I’m not sure.

Hibernate (and JPA) is great for some things, especially at the beginning of the development cycle. It is much faster to get to something that works with Hibernate than with JDBC. You get many features for free caching, optimistic blocking, etc.

On the other hand, it has some hidden costs. Sleep mode is deceptively simple at startup . Follow the instructions, add some annotations to your class - and you have perseverance. But it’s not easy, and to be able to write good code in it requires a good understanding of both its internal development and database design. If you are just starting out, you may not be aware of some problems that may bite you later, so here is a partial list.

Performance

Execution performance is good enough, I have yet to see a situation where sleep mode was the cause of poor production performance. The problem is startup performance and how it affects device testing time and development productivity. When hibernation loads, it analyzes all entities and does a lot of pre-caching - this can take about 5-10-15 seconds for a not-so-large application. So your 1 second unit test will now take 11 seconds. Not fun.

Database independence

This is great if you do not need to do fine tuning in the database.

Session in memory

For each transaction, Hibernate will store an object in memory for each row of the database that it “touches”. This is a good optimization when you do simple data entry. If for some reason you need to process many objects, this can seriously affect performance if you do not explicitly and thoroughly clear the session inside the memory.

Cascades

Cascades make it easy to work with object graphs. For example, if you have a root object and some children, and you save the root object, you can configure hibernation to save children. The problem starts when your graph of objects becomes complex. If you are not very careful and well versed in what is happening inside, you can easily ruin it. And when you do this, it is very difficult to debug these problems.

Lazy Loading

Lazy Loading means that every time you download an object, hibernate will not download all related objects, but instead will provide owners with places that will be allowed as soon as you try to access them. Great optimization? This is, except that you need to know about this behavior, otherwise you will receive critical errors. Google "LazyInitializationException" for an example. And be careful with performance. Depending on the order in which objects are loaded and the object graph, you can click "n + 1 selects problem". Google for more information.

Schema Update

Hibernate makes it easy to change the schema, just refactoring java code and restarting it. It's great when you start. But then you release version 1. And if you do not want to lose your customers, you need to provide them with schema update scripts. This means that there is no simpler refactoring since all schema changes must be performed in SQL.

Views and stored procedures

Sleep mode requires exclusive write access to the data it is working with. This means that you cannot really use views, stored procedures, and triggers, as they can lead to data changes when sleep mode is unaware of them. You may have some external processes that write data to the database in separate transactions. But if you do, your cache will have invalid data. This is another thing to take care of.

Single threaded sessions

Sleeping sessions are single-threaded. Any object loaded through a session can be accessed only (including reading) from only one stream. This is acceptable for server-side applications, but can complicate unnecessary things if you are using a GUI-based application.

I think my point is that there is no free food.

Hibernate is a good tool, but it is a complex tool, and it takes time to understand it. If you or your team members do not have such knowledge, it’s easier and faster to go with pure JDBC (or Spring JDBC) for one application. On the other hand, if you are willing to invest time in learning (including learning by doing and debugging) than in the future, you can better understand the trade-offs.

+161
Aug 30 '09 at 13:09
source share
— -

Hibernation may be good, but it and other JPA OPCs tend to dictate the database structure to some extent. For example, composite primary keys can be executed in Hibernate / JPA, but they are a bit inconvenient. There are other examples.

If you are comfortable with SQL, I would strongly suggest you take a look at Ibatis . It can do 90% + of what Hibernate can, but much easier to implement.

I cannot think of any reason why I would directly choose JDBC (or even Spring JDBC) over Ibatis. Hibernate is a more difficult choice.

Take a look at Spring and the Ibatis tutorial .

+16
Aug 30 '09 at 5:06
source share

No doubt Hibernate has its own complexity.

But what I really like about the Hibernate approach (some others too) is the conceptual model that you can get better in Java. Although I do not consider OO a panacea, and I do not seek theoretical purity of design, I have found so many times that OO really simplifies my code. As you specifically specify the details, here are a few examples:

  • complexity is not added to the model and entities, but to your structure for managing all objects, for example. For supporters, the hard part is not just a few classes of frameworks, but your model, so Hibernate allows you to keep the hard part (model) the cleanest.

  • if a field (for example, an identifier or audit fields, etc.) is used in all your entities, you can create a superclass with it. Therefore:

    • you write less code, but more importantly ...
    • there are fewer concepts in your model (a unique concept is unique in code)
    • for free, you can write a more general code, which is equipped with an entity (unknown, without switching types or cast), which allows you to access the identifier.
  • Hibernate also has many features for working with other model features that you may need (now or later, add them only as needed). Take it as extensibility quality for your design.

    • You can replace inheritance (subclassing) with a composition (several objects that have the same element that contains several related fields that are needed in several entities).
    • There may be inheritance between several of your entities. It often happens that you have two tables that have roughly the same structure (but you do not want to store all the data in one table because you will lose the referential integrity for the other parent table).
  • When reused between your objects (but only with the appropriate inheritance and composition ) there are usually some additional benefits. Examples:

    • There is often a way to read entity data that is similar but different from each other. Suppose I read the "title" field for three objects, but for some I replace the result with another default value if it is null. It is easy to have a signature "getActualTitle" (in a superclass or interface) and implement default processing in three implementations. This means that the code from my entities deals only with the concept of the "actual name" (I made this functional concept explicit), and the inheritance of the method takes care of the execution of the correct code (no more if there is no duplication of code).
    • ...
  • Over time, requirements evolve. There will be a point at which your database structure has problems. When using only JDBC, any change to the database should affect the code (i.e., double cost). With Hibernate, many changes can be absorbed by changing only the display, not the code. The same thing happens and vice versa: Hibernate allows you to change your code (for example, between versions) without changing your database (changing the display, although this is not always enough). To summarize, Hibernate allows you to independently grow your database and your code .

For all these reasons, I would choose Hibernate :-)

+10
Aug 30 '09 at 11:53
source share

I think this is a great choice, but personally, I would use sleep mode. I do not think that hibernation is unnecessary for a project of this size.

Where Hibernate really shines for me deals with relationships between entities / tables. Manually executing JDBC can take a lot of code if you are dealing with the simultaneous change of parents and children (grandchildren, siblings, etc.). Hibernate can do this a breeze (often just saving the parent object is enough).

Of course, there are difficulties when working with Hibernate, such as understanding how session cleanup works and working with lazy loading.

+4
Aug 30 '09 at 5:07
source share

Hibernate best suits for middleware applications. Suppose we create middleware on top of a database. Access to the medium is available for approximately 20 applications, in this case we can have a sleep mode that meets the requirements of all 20 applications.

+3
Sep 20 '13 at 7:01
source share

Direct JDBC would at best fit the simplest cases.

If you want to stay in Java and OOD, then your Hibernate or Hibernate / JPA or any other JPA provider / JPA should be in your own place.

If you are more comfortable working with SQL, then having Spring for JDBC templates and other SQL-based frameworks will not hurt.

On the contrary, in addition to transaction management, there is no support from Spring when working with JPA.

+2
Aug 30 '09 at 6:22
source share

... Session in memory ... LazyInitializationException ...

You can see the Ebean ORM , which does not use session objects ... and where lazy loading works. Of course, it’s an option, not a bust, and it will be easier to understand.

0
Nov 11 '09 at 11:47
source share
  • In JDBC, if we open a database connection, we need to write to try, and if any exceptions occur, the catch block will spoof it and finally use it to close the connections.

  • In jdbc, all exceptions are checked by exceptions, so we need to write code in try, catch and throws, but in sleep mode we only have canceled exceptions

  • Here, as a programmer, we must close the connection, or we may receive a message about the connections ...!

  • In fact, if we did not close the connection in the finally block, jdbc does not respond to close this connection.

  • In JDBC, we need to write Sql commands in different places, after the program created, if the table structure was changed, then the JDBC program does not work, again we need to change and compile and redeploy, which is tedious.

  • JDBC is used to generate database error codes if an exception occurs, but java programmers do not know about these errors correctly.

  • While we are inserting some record, if we do not have any specific table in the database, JDBC will raise an error, for example "View not exist", and throw an exception, but in case of sleep mode, if it is not found, any table in the database will create a table for us

  • Support JDBC LAZY loading and Hibernate support download

  • Hibernate supports inheritance, associations, collections

  • In sleep mode, if we save the object of the derived class, its base class object will also be stored in the database, which means that the inheritance supporting sleep mode

  • Hibernate supports one-to-many, one-to-one, many-to-many, many-to-one relationships

  • Hibernate supports the caching mechanism, the number of return flights between the application and the database will be reduced using this caching technique, the performance of the application will be automatically increased.

  • Getting pagination in sleep mode is pretty simple.

  • Hibernate has the ability to generate primary keys automatically while we save records in the database

0
May 22 '15 at 10:04
source share

if billions of users using an application or network, and then in a jdbc request, will be executed billions of times, but in a request for sleep mode will be executed only once for any number of users, the most important and easy advantages of sleep mode over jdbc.

0
Nov 24 '17 at 10:03
source share



All Articles