Recession Beauty and Retail Deals

With the current climate, retailers are feeling the pinch with less consumer spending. Here are a few online beauty deals to get you through the quarantine. Family-owned handbag company offering…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Toying around with Querydsl and MongoDB

I recently decided to brush up my coding skills and take my next venture into Java territory.

Good thing that I came across a challenge on the MongoDB Advocacy Hub which introduced me to Querydsl ;)

First off, what is Querydsl?

Querydsl claims to be “Unified Queries for Java”, “Instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API.”.

Delving deeper into it, Querydsl turns out to be an abstract data-access layer implementation for Java programs. By using annotations on POJOs (Plain-old Java Objects) in your domain model Querydsl generates all glue logic for data access behind the scenes.

This means that for you as API user Querydsl supplies you with an identical interface to query any data source. Convenient.

So the first venture involving Querydsl was “How to make it work?”.

For this I decided to set up a project using Maven.

This generates a project folder with a basic scaffold for a generic Java application. Opening it up in JetBrains IntelliJ was pretty simple by importing the “pom.xml”.

Next task was to add the dependencies for Querydsl with MongoDB support. This is what the dependencies block in my POM turned out finally after a bit of trying due to breaking changes in used external dependencies. Might not be perfect but works for me. Lots of trial and error here.

Also one needs to ensure that additional build steps are introduced in the POM, this is necessary for Querydsl’s code generation of data access classes prefixed with Q, e.g. QProduct for a product class.

Because I tend to stick to unit tests whenever learning a new framework I removed the default main class and started to add my domain objects in a package “.domain”. The class for a generic shop product in there turned out to look like this.

Notice the @Entity annotation with the value “products”. This specifies that all instances of the persisted product class are stored in a matching collection of the same name as the given value. Also the annotation is coming from the Morphia package to indicate it is an entity persisted in MongoDB.

Inside the previously maven-generated AppTest class I introduced two test cases. Take note of the fixed assertions, I was just too busy to write something more sophisticated for the first test :-)

Finally it was time to taking it for a test. Well, almost, because the database I wanted to test it with was empty.

So taking note of that I inserted two documents inside the products collection.

Excellent!

So finally ready to launch I build and test the whole thing by using

Tests running and green.

Using Querydsl in a Java example after some time of Java abstinance was a bit of a challenge for me.

Most difficult tasks were to properly setup the dependencies with their specific versions and getting code generation for Querydsl working. Setting up code generation was IMO the hardest part of it all and involved a lot of trial and error in the POM’s build section.

The uniform access to the data sources in persistence seems convenient and useful. All together this was a fun experience to try over the holidays, maybe I will have the opportunity to use Querydsl in a Java project sometime in the future.

Add a comment

Related posts:

Tips to Parenting

Having children is a blessing and individuals should strive to raise the kids in an upright manner that will ensure they are responsible and capable of behaving even when they are adults…

5 ONLINE BUSINESS IDEAS FOR 2018

That more than half of the world’s business is now done online is a fact that cannot be denied. Both traditional and new business owners have come to recognise the need to have a presence online…

How MoCaFi Founder Wole Coaxum is Charting a New Course for Financially Underserved Communities in the U.S.

While we may not know exactly how FinTech will impact our future, we have an idea as to who will be leading the charge. Our latest blog series, “The New Faces of FinTech”, spotlights some of the…