What are some good use cases for Querydsl? [closed] - java

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.

Related

Retrieve Data From Database : Spring Boot [closed]

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.

Morphia vs Spring Data Mongo [closed]

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 last month.
Improve this question
I am using Java language.I have to use ORM framework with MongoDb as Database.I have two options Morphia or Spring Data Mongo support.As far i am able to get details , it has been found Spring Data Mongo is better to use since:
1)It provides better DAO out of box inbuilt classes.
2)It has larger community base.
Are there any performance based differences between the two.And if there which one is better in which condition.Also i have requirement of multitenancy .After little search i found that there is very simple custom implementation in Spring Data Mongo to do the same.But in Morphia it is somewhat difficult.Does achieving multitenancy in Morphia diificult(where we need to write a lot of boiler plate code)
I have been using Spring-data and I guess I feel somehow it lags as far as maturity is concerned.
It's good for all the practical purposes but the features mongo provides in it's full glory, Spring-data is slow to map that as a driver specially when it comes to aggregation.
As far as performance goes, Spring-data doesn't lag imo.
Sometime I get weird behaviors. Some of their annotation silently doesn't work at some places and for my life I cannot figure out why?
But as an overall implementation it's quite helpful in the way that it provides a robust structure on which your application can grow. It also is easy if you are coming from SQL background since you can draw a parallel between jdbcTemplate and mongoTemplate (though one needs to be cautious)
I seriously considered using Morphia, but dropped the idea since spring-data was providing a more structured ways. Looks like in Morphia we would have to implement some structure on our own which has pros and cons but You usually want to avoid doing it. Since there is a risk of boiler-plate codes, there is a learning curve for 'your' structure for new members.
On the pros side, I am sure Morphia provides more extensiblity leading you to enjoy the ability to suck most out of mongo features. It also is lightweight compared to Spring data.
Morphia is the way to go. Pretty stable, very good Play integration and offers access to all Mongo driver features if you need more torque. Reference resolution, entity embedding are working as expected. You get lifecycle annotations too, which are pretty useful for boilerplate persistence code.
I personally like spring-data because of the hades project... You don't need to implement the DAOs. You just write the interface and spring data automatically provides it to you. However Spring Data Mongodb implementation seems a little buggy in my initial trial. If you have hard dates and is working on a production quality product, probably it is wise to choose Morphia.

how to import a hql project in eclipse [closed]

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.

Hibernate vs JDBI [closed]

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.

How to use JPA in a existing project? [closed]

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.

Categories

Resources