Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Present scenario: I am trying to use hql in place of sql.
(1)I need to know,In order to use hql what all basic jars or basic setup I will be needing.
I have basic java knowledge,But new to hibernate.
(2) And how to import a hibernate project into eclipse.
For instance i am using eclipse mars.
SQL (Structured Query Language) is a standard query language used by multiple relational database systems.
HQL (Hibernate Query Language) is a SQL-like language used by Hibernate, to retrieve and populate objects with data from a relational database. HQL-queries are translated to SQL queries by Hibernate.
So these are not equivalent by far. In order to even use HQL your application would have to use Hibernate.
In a Hibernate application, the advantages of HQL is its brevity, the way HQL queries automatically populate entity objects, and the fact that it is database vendor-agnostic (meaning that you should be able to change database provider without changing your queries.) The disadvantages are mainly related to performance, where complex queries/data models might end up slow.
Using SQL in a Hibernate setting on the other hand has the advantage of being fast, as you control what is being queried in the database, and the disadvantages compared to HQL that you have to do your own database-to-object mappings, as well as being more verbose. In addition, changing database vendors might lead to rewriting your queries.
Since your use case is that of pure data migration as far as I understand it, I would use SQL as it is a simpler approach to these kind of problems. If you build a data migration application, HQL might be right, but that really depends on your case.
If you want to use HQL you should start by reading up on JPA and Hibernate.
Related
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 yesterday.
Improve this question
I understand that there are so many ways we can get data from the Db, one is using JPA or Typed/Named Query.
Wish to know on an Enterprise level do we use JpaRepository or custom repository and if custom what exactly do we use to fetch data is it Stream API or Criteria API or something else .
I have tried to use JpaRepository, getById() and also Stream API filter
I have been doing enterprise programming for a long time and the best choice for me was JPA because JPA allows you to avoid writing DML in the database specific dialect of SQL. JPA allows you to load and save Java objects without any DML language at all. When you do need to perform queries JPQL allows you to express the queries in terms of the Java entities rather than the (native) SQL tables and columns.
Also many helpful annotations like #DynamicUpdate and #DynamicInsert help ORM to create more efficient queries.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I read about the runtime query builder querydsl. But I could not figure out the best cases or examples, where querydsl is most useful. If somebody can explain, it would be really good.
Querydsl helps you to get rid of hard-coded queries and provides a library that encapsulates the functionality of SQL through the hierarchy of corresponding types and interfaces.
It is a better practice to code your queries using interfaces and parametrize the calls.
You will be able to auto-test your changes during the build time using JUnits.
The above makes querying more robust and improves the maintainability of your codebase.
QueryDSL is type-safe queries purely in Java, no strings involved.
repository.findAll(QProduct.product.owner.name.startsWith("Hello"));
So when is it a good idea to use it?
Whenever you need to query from a database that is supported by QueryDSL, of course. Why use SQL when you can use QueryDSL?
If you need to create dynamic database queries, you should use Querydsl.
The pros of using Querydsl are:
It supports dynamic queries.
It has a very clean API. In other words, it is easy to create complex
queries with Querydsl, and the query generation code is easy to read.
It also supports JDO, Lucene, and MongoDB.
The only “problem” of Querydsl is that it isn’t the standard way to create dynamic queries with the Java Persistence API.
Quite fine tutorial with very good explanation you can find here.
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 8 years ago.
Improve this question
I am building a web service using the Dropwizard framework (version 0.7.0). It involves executing some read-only queries to the database, manipulating the result set and then returning that data set. I am using MySQL as a database engine. Since I am new to this framework, I want to know which option I should choose: Hibernate or JDBI.
I've used both of these. I've used Hibernate with GORM in Grails as well as in a traditional Spring app and I've used JDBI in Dropwizard.
I have really enjoyed the simplicity of JDBI and here are a couple of reasons why I prefer it over Hibernate.
I know exactly what SQL is going to be executed to acquire the data I'm requesting. With Hibernate, you can sometimes have to do a lot of messing around with HQL and configuring your objects to what you intended to have returned. You ultimately resort to SQL, but then have the difficultly of properly mapping your results back to your domain objects, or you give up and allow hibernate to fetch them one by one.
I don't need to worry about lazy/eager fetching and how that is going to affect my query time on large data sets.
Mappings aren't complicated because you manage them on your own and you don't have to rely on getting the right combinations of annotations and optimizations.
For your case in particular, it sounds like you'd want something lightweight because you don't have a lot of use cases and that would definitely be JDBI over Hibernate in my opinion.
Really, both of these solutions are just "lock-in".
If you want to go with a persisted model type interface, write your code against JPA (if you are sure it's only going to back to a relational database) or JDO (if you might want to back to relational and other-type databases, like the no-SQL movement). This is because with either of these solutions, when problems occur you can switch persistence providers without rewriting the bulk of your code.
If you want to go with a procedural persistence model (dealing with SQL queries directly and such), then go with JDBi or perhaps even JDBC. JDBi provides a very nice abstraction over JDBC; however, there are cases where you want the lower level access (for performance reasons, of the kind were you are tuning the queries and database in concert). Again JDBC is a standard such that you can swap out one database for another with some ease; however, the SQL itself won't be as easy to swap out.
To amend the SQL swap out problems, I recommend using sets of property files to hold the queries, and then a Resource loader type mechanisim to bind the SQL for the right database to the code. It isn't 100% foolproof; but it does get you a bit further.
Now, if you ask me what I'd use, I highly recommend JDO.
if you have very few work upon database then use JDBI else go for Hibernate as it is very strong and provide many additional features to your persistence logic.
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 8 years ago.
Improve this question
My team is working on a medium size application (OLTP style). We were interested by switching to JPA instead of only using JDBC queries. Mostly for performance and practical reason. I'm not looking for a tutorial that shows me how to create a persistence.xml or Entity class in Eclipse. What i would like to know is what would be the steps to convert all the database queries into the JPA format. I know that the whole application must use JPA.
Many programmers has worked on this project over the years, so not everyone has the same SQL knowledge or the same programming skills. So there must be in this application 1000+ customs queries, using multiple tables (something that native JPA does not support very well), or query that is selecting only a few fields in a table... This is getting a bit out of control and i think that JPA would create a nice toolbox to make sure that everyone is going the same direction.
What should i look for to make sure that i'm not going into a process (convertion) that will never end ? Some sort of guideline.
(Again, i'm not looking for programming exemples, nor Eclipse tutorial.)
Thanks!
First step is convert you database schema into database model using JPA, you need to be clear what are the table, sequences, database objects that you are using in your existing application and start modeling all the schema with JPA you should consider use JPA annotation.
The step above will determine what will be your entities, embeddables and mapped superclass, their properties and the relationships they have, this step is very crucial as your logic will depends on the correctness of this model.
Then start looking for all the queries that are involved in your project, as you said that you have 1000+ queries consider use two scenario, convert all of them in JPQL queries or use a mix between native queries and named queries, I really prefer to convert all in JPQL unless are very database dependent. A step you must follow is find all of them, probably are some existing tool that convert from SQL to JPQL but I believe is better idea make by your own.
Once you have queries and model for the database start the creation of your new DAO using JPA and EntityManager stuff, I should recommend extract the interface for your exisiting DAO and start moving to a JPA implementation using the same interface, this will avoid break some code on your own, don't forget unit and IT test for your new DAO.
Also with the above approach you could start moving the application module by module, DAO or by DAO does not require to move full application at once. This will give you a kind of process in which you will see some progress each time you finish a new DAO or module.
Not sure what you mean about programming examples, I think those are the required steps but each project is different from each other, so consider this as some kind of guidelines.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I currently have an application, where it's primary performance issue is using file-based database consisted of JSON responses.
I'd like to rewrite my application to use SQLite database feature.
Since I'm lazy, I'd like to use some kind of ORM.
So far I have found only two big ORM libraries:
ORMLite
GreenDAO ORM
DB4O
ActiveAndroid
My primary goal is to raise performance on working with data as much as possible
But I've found two possible issues with those libraries.
ORMLite uses annotations, which is big performance issue in pre-honeycomb due to this bug
GreenDAO is using some kind of code generator, and that would slow me down on development as I would have to write generator, and then use generated code. And I don't very like this idea.
DB4O is JPA, which I've always considered as slow and heavy on memory usage, therefore unsuitable for low-end devices (remember the Android API v7)
ad #ChenKinnrot:
The estimated load should be sufficient to think about using an ORM.
In my case it is about 25-30 unique tables, and at least 10 table joins (2 - 4 tables at a time). About 300-500 unique fields (columns)
So my questions are:
Should I use ORM/JPA layer in Android application?
If so, what library would you recommend me to use? (and please add some arguments too)
I've used ORMLite and found it straightforward once you got the hang of it (a few hours), quite powerful and didn't cause any performance problems (app tested in Gingerbread on HTC desire and HTC Hero).
I will be using it again in any projects I need to use a DB for.
A ORM layer is appealing.
However, in practice I either write simple ORM myself or use the Content Provider paradigm, which does not cooperate well with ORM.
I have looked into some existing ORM libraries (mainly ORMLite ,activeAnroid) but they all scared me away
as they seems not so easy to get started.
"We're talking about 25-30 unique tables, and at least 10 table joins.
About 300-500 unique fields (columns)"
If you have fixed and limited patterns of how the data will be queried, I would recommend to write the ORM/sql yourself.
My 2 cents.
If you are worried about your app's performance, I'd recommend greenDAO. It will save you from writing lots of boring code, so code generation should not be an issue. In return, it will generate also entities and DB unit tests for you.
I got some knowledge to share so:
ORM by definition is slower than writing your own sql, it's suppose to simplify the coding of data access, and provide a generic solution, generic = runs slower than you write your queries, if you know sql well.
The real question is how good performance you want to get, if it's the best possible, don't consider any data mapping framework, only sql generation framework that will help you write stuff faster, but gives you full control of everything.
If you don't want to get the most out of the sql db, use orm, I got no experience with this orm you mentioned, so I can't say what to choose.
And your DB is not so big and complex so the time you'll save with orm is not an issue.
In my experience, I had a lot of benefits from using ORM engines. However, there was the case when I had to deal with performance problems.
I had to load about 10 000 rows from the database, and with a standard implementation (I was using ORMLite), it took about 1 minute to complete (depends on the device CPU).
When you need to read a lot of data from the database, you can execute plain SQL and parse the results yourself (in my case, I only needed to query for 3 columns from the table). ORMLite also allows you to retrieve raw-results. By this, the performance has increased by 10 times. All 10 000 rows were loaded in 5 seconds or less!