Hibernate queries really save time compared with SQL? I can see a very similar level of complexity. It solves the object-tables mapping, ok, but it is the same level of detail or it is considered a higher level abstraction?
Hibernate will save time to develop software. Caching improves performance. But nothing can beat native sql performance . ORM frame work helps in maintenance and sustainability with a performance over head compare to native sql using JDBC.
Hibernate and generally all the ORM tools uses cache mechanism to increase performance so for some cases it might gives you better performance then native sql.
In addition there are lots of benefits using ORM then writing sql queries directly like security, less complication while writing big queries etc.
But you can not conclude in native sql vs hibernate in terms of performance in yes or no. see Performance difference of Native SQL(using MySQL) vs using Hibernate ORM?
Related
I'm a Java developer and currently I was requested by my manager to evaluate Java ORM and SQL library (such as Hibernate, MyBatis, iBatis, JOOQ, DbUtils, and so on). I would like to run the test to compare the performance between the ORM libraries, and also the performance between the SQL library. However, I have no idea how to write the test case to evaluate their performance.
I found some articles are comparing the performance of 4 CRUD operations for different libraries, but I couldn't get its test case source code. I would like to have it as a reference.
Appreciate if anyone can share with me how to test the performance of the ORM and SQL library.
Note: The performance test is just part of my evaluation. I understand each library is designed for different purpose and I shouldn't compare them merely by their performance.
Thank you very much
I'm currently developing a Java Web Project and I have to choose either JPA with all the ORM stuff and the chance to set queries direct or direct MySQL queries with Stored Procedures. I have to consider the advantages and disadvantages of theses points in strict order:
Performance: Give results fastly
Complexity: Which solution has more learning curve?
Do the stored procedures are faster than JPA queries?
In terms of performance, I don't have any specific numbers.
Please elaborate on specific concerns you might have.
In general, with JPA you have far less control when it comes to performance tuning than you would if implementing a custom solution. However, JPA provides a solid, proven infrastructure with a boat load of functionality that you don't have to write yourself! JPA will definitely help you to more quickly get your application off the ground.
In terms of learning curve. If I assume you are starting fresh... there is a great deal to learn with either approach. Both require a working knowledge of SQL and entity relationship models. The JPA approach requires you learn JPA! Go figure! A MySQL approach requires knowledge of JDBC.
Your question, 'do stored procedure run faster than JPA queries' is not really the right question to ask. JPA 2.1 supports stored procedures. The better question would be, does a query in JPA run faster than a JDBC invoked MySQL query or does a stored procedure in JPA run faster than a JDBC invoked MYSQL stored procedure. All in all, a direct JDBC approach may be a bit faster than JPA, but only due to the small overhead of translating JPQL (JPA's SQL-like language) to SQL.
Currently I am migrating databases from Oracle to MySQL. I mainly use Java to send queries to the database using JDBC. In the process of migrating, I need to change a lot of my queries in the Java code (the queries are hard-coded) as they will not work in MySQL.
I want to be able to recode my queries in such a way that I can easily switch between the databases if problems arise; I am changing all my queries to standard SQL but there are areas where this is not possible. I am thinking of having two versions of the queries, one for Oracle and one for MySQL so I can switch between both (I will have two versions temporarily just to see if MySQL can cope with our needs). However this seems like a terrible idea - does any one have any advice on a better way they would do this?
You have a bunch of options.
Firstly, many people now use Object-Relational Mapping (ORM) tools to connect applications to SQL databases. These come in a variety of different flavours - Hibernate is popular - and allow you switch between databases at very little cost. However, they do have a fairly steep learning curve. Inexperienced developers often struggle with performance problems in ORM applications.
If you stick with "traditional" JDBC, I suggest you take the body of the SQL out of the Java code, and treat it like a resource. As Henry suggests, you could use property files, and use parameter placeholders (ideally named placeholders, using the Spring template). While this does spread the code for a given piece of functionality into two files, it makes it easy to quickly refine the SQL and test new versions.
One possibility is to store the queries in a properties file. You would have one for Oracle and one for MySql.
I must add - ORM is good advice and will work... when you are starting with a fresh application and you can design your application to work on a domain model.
In this case however there is an existing application that invokes a great number of SQL queries. ORM based queries (HQL, JPQL) translate to SQL just fine; SQL does not by definition translate to the ORM layer however, major changes will be needed to make it a more object oriented approach to the data.
The problem will still persist even when you do manage to work in an ORM layer. There is already a major difference between MySQL and Oracle in how primary key generation works for example; MySQL uses auto-numbering where Oracle uses a sequence. Likely you already have an existing datamodel that you need to reverse engineer into the ORM layer code; it isn't going to be cross-database code.
Greetings everybody,
I came across the feature of using java in oracle, a few days back. Ever since I am wondering about the
possibility of writing static methods in java replacing regular PL/SQL logic.
As I have reasonably adequate experience with
java plus the rich libraries it offers, I am tempted to write java methods instead of regular PL/SQL. Would this be
a good practice?. Will there be much performance overhead in doing so?. Thanks in advance.
There will be overheads involved in your decision to walk away from PL/SQL into Java code.
Although i am not a big believer of putting business logic into PL/SQL, i have seen just too many companies doing that, including my own.
The "performance consideration" of doing this does not come as whether or not to use static methods. For example, you may need to declare an arraylist to sort the values, and depending on the values, retrieve more results from the database in another query.
IMO, i would put business logic in my application, and not invest in PL/SQL. This also helps to ensure DBMS portability and not buy-in.
It depends!
They are two languages for different purposes!
What kind of operations do you want to do?
Every operations that touches data layer(I know it's debatable)
Massive Sql operations with large data processing( bulk operations)
To manage any kind of transaction
The job can be easily done in Pl/Sql
Are you writing code that is strictly tied to Sql data types?
Monitoring the relations between the code and dependent db objects
In this case, I believe Pl/SQL is the best choice for performance.
Attention, I wrote PL/SQL:
PL as procedure language(2nd choice)
SQL as data query language(1st choice)
The power of this two languages, strictly tied in Oracle, let you write application which access data in Oracle in faster and easier way than in any other language.
Do you need:
Operating system operations
Directory and file operations
Massive mails operations
Network operations
Other things you can't do in:
SQL (1st choice)
PL (2nd choice)
write code to reuse in architecture like Enterprise Java Beans
Write code to reuse elsewhere
In this case I think Java is a better choice, 3rd choice in Oracle.
But I'm not reinventing the wheel, this information are largely accessible and proved by Oracle experts like Tom Kyte etc.
Just some useful link to search:
PL/SQL advantages over java
Oracle Docs
Tom Kyte site
OTN forum
I think it can be acceptable if you store the CRUD operations as stored procs. If you use any ORM framework, then you might map the SPs. It is more usual that the database is the more stable part of the application. The application might be refactored or replaced, but the database does not change (it is just my experience).
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a several questions about hibernate.
In many questions here in stackoverflow, several people are saying that hibernate is not a good choise for very complex databases. If we have very complex database, hibernate is not the right choice. It better suits for green field projects, but it is not so good for complex legacy database.
Is this true?
Also hibernate generates queries.
Every project manager will like to have optimized queries (hibernate cannot generate more optimized queries than sql specialist!). So for big project it is not a problem to hire sql specialist. The sql specialist will optimize the queries (use explain sql, use joins ...)
My question is how come a huge and expensive project does not care about sql optimization?
(you will say that you can write HQL but as I have seen in a lot of posts that explains that HQL is not so powerful than sql and a lot of programmers get headache and several hours of tuning) (you like all your organs in your body to work ideally don't you?)
Also the second level cache helps hibernate a lot because hibernate knows to generate a lot of queries instead of complex join.
My question is: Is really a complex db only modified by one system (example the web site)? If we are talking about the enterprise system the db can be accessed via several processes, sharing different programming languages and platforms.
So in this case the second level cache does not help very much.
For what kind of projects hibernate is suitable for?
Is it for back office projects where nobody cares about the sql ?
What happens when your administrator says: please use memcached for caching and please use this optimized queries instead of yours?
If you are using oracle database, orache has the most advanced sql syntax. They have spend a lot of time and money on the syntax that is very powerful. What for is this syntax if it is not used.
The software is written only once (and then maintained) and used for a long time.
If I am a company that orders software I will say: I will use the software for a couple of years and I like to be fast, and if you spend 1 month for writing software with hibernate I will pay one more month for software that uses example IBATIS knowing that it will work better for years
(when you are buying a car you are interested in the car economy 1kg-oil/km, not how short and easy the manufacturer produced the car!). So as a software consumer I do not interested in your productivity, just how fast the software is. Of course also the price is relevant but if we are speaking about the price there are more complex mathematics.
Can we call something engineering when we really cannot predict some part of the system?
(can electrical engineer be really a engineer if he cannot predict the current)
Please share your opinion.
Regards
1) (...) Is this true?
No it isn't, Hibernate can deal with pretty complex databases, including existing ones. However, it might not deal very well with an heavily denormalized database or an exotic schema. This is different.
2) (...) My question is how come a huge and expensive project does not care about sql optimization?
This is non-sense, using Hibernate doesn't mean you don't care about optimization. I have worked on a huge and complex STP system (several hundreds millions € budget) and performance was definitely an important concern and we actually introduced Hibernate to benefit from things like lazy loading, second level cache (and speed up development).
Here is the deal when using an ORM like Hibernate (when suitable):
You'll be done faster with ORM than without ORM (or there wouldn't be any point at using them).
The vast majority of the generated queries will behave correctly (and the fact is that Hibernate generates better SQL than the average developer).
You can (and have to) tune queries and Hibernate to a certain degree.
Even if you spend some time on performance optimization (including falling back to native SQL for really problematic queries), you'll still be done faster.
3) (...) So in this case the second level cache does not help very much.
Well, you are right about the fact that using the second level cache ideally means using Hibernate APIs (although you can still evict the cache "manually" and although I tend to prefer using it for "mostly read" entities). But, more important, to my experience sharing data between many applications through the database just leads to unmaintainable applications (changing a single bit becomes impossible as it may impact several applications) and and should be avoided. Use an EAI/ESB and expose services of the main system through it. This way, you can reuse the business logic, the 2nd level cache, etc.
4) (...) For what kind of projects hibernate is suitable for? Is it for back office projects where nobody cares about the sql ?
Hibernate is indeed very nice for CRUD applications, but not only (see above) and your question shows some ignorance as I already said. However, it isn't suitable for any project:
I would probably not use it for a data warehouse or a big reporting application.
I might not use it with a heavily denormalized or exotic legacy database (a data mapper like mybatis might be a better choice in this case).
I might not use it with an existing system using stored procedure for everything.
I would not use it with a non RDBMS datastore :)
5) (...) What happens when your administrator says: please use memcached for caching and please use this optimized queries instead of yours?
I tell him that memcached is maybe not the best solution in our context (no, I don't want to always send my data over the wire and I don't care that Facebook/LiveJournal/Twitter/whatever are using it, our app might have different needs), there are other better cache implementations when working with Hibernate, I ask him to discuss problems with me and we discuss the various solutions, etc. We work as a team, not against each other.
To sum up, ORM solutions are not always suitable but I think that you currently have a biased opinion and my experience is different from the opinions (misbeliefs?) expressed in your question.
See also
When NOT to use O/R mapping in Java
It's good for green field projects, but it's also good for legacy projects. You may need to do some mapping tricks, but it offers reasonably flexible mapping.
Since you can use native queries, and since you can integrate it with your favorite caching solution, you don't need to suffer any performance problems just because you're using Hibernate. When your db administrator says that you should use memcached, you can use this memcached/Hibernate integration. You can write a caching implementation using your favorite cache and plug in into Hibernate. When she says you should use this optimized query, you say "great! Hibernate has a native SQL facility that will let me use that query". You can use native Oracle syntax, you can use the native syntax of whatever RDBMS you've chosen.
A multiple-application environment poses the same challenges to Hibernate as it does to any solution. If you want your application to perform well, you will use what amounts to a second-level cache. Hibernate happens to offer an ORM that is integrated with the cache. It doesn't solve the problem of coordinating a cache across multiple applications, but you'll have to solve that problem even if you don't use Hibernate.
Your question is probably too broad. I can tell you about my experience.
I worked on a project that adopted the .NET version (NHibernate). A naive implementation of loading a single row from a single table was almost two orders of magnitude slower than a raw ADO query. After much optimization I believe they got it down to merely one order of magnitude slower.
In java where the start up time is probably less of a factor. The web server loads java and hibernate at server start instead of while a user waits for a desktop app to start.
Personally I really dislike it. It hides implementation details that are necessary to efficiently manage your data. I've found no real world application that could perform acceptably with a vanilla implementation of a data layer that hides database details.
But that may be sour grapes on my part since I was forced to use it and blamed for
not being able to put enough lipstick on the pig.
No matter how complex database is. The most important question is how complex domain model of application is.
Is query select * from anytable where anycol = #anyvalue optimized? I have no idea. Nobody has. Because there is only one true criteria of optimization - this is performance of such queries. You can save a lot of time with hibernate or other ORM, then use this time to find actually slow queries. As far as I know Hibernate has some ways to use optimized query.
Third your question is good. But also there is no one answer to the question 'Is dirty data good every time everywhere?'. Strictly saying, until locked, any data read from database are dirty, no matter how its were read and where its were stored. Data blocking is not good thing for performance, so usually you should find compromisse between actual data and performance.
There is no silver bullet. ORM has a lot of advantages, but there is only one serious case when it is not suitable: it is dynamic resultsets depends of parameters (when different parameters returns data with different column sets). Because object structure are static at compile time (in static typed languages) ORM can't help in this case.
Every other case can be solved. Entity sevices (changes tracking etc.) can be off, second-level cache can be disabled, and optimized query can be used instead of generated. I have no idea how to do all that things in Hibernate, but I'm sure it is possible.
ORM has a great advantage it concentrate all data access logic in manageable form, and put it in specific place. Also it supports few things are not so easy and direct to implement in your own data access library, like transaction management (including nested transactions, etc), identity mapping (one row - one object), complex hierarchy persisting (if you use objects and object hierarchies), optimistic locking etc, and ORM can greatly helps you with it.