How to run update query with JPA?

This is a quick Tip.

Earlier today, I ran into a situation where the seeming straightforward query to udpate a record using JpaRepository did not update the database record. The following was the query I was using.

@Query(value = "update book p set p.primary_category = :primaryCategory where p.primaryCategoryCode = :primaryCategoryCode", nativeQuery = true)
void updateCategoryName(@Param("primaryCategoryCode")String primaryCategoryCode, @Param("primaryCategory")String primaryCategory);

Upon looking into the console, I noticed the following error.

2020-12-10 19:17:54.323  WARN 15912 --- [  NFAsyncTask-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: S1009
2020-12-10 19:17:54.323 ERROR 15912 --- [  NFAsyncTask-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Can not issue data manipulation statements with executeQuery().

Looks like all I needed to was to add the @Modifying annotation to the query that did an update.

React / Angular App with Spring Boot Backend – How to Fix BrowserRoute URLs in Production for SEO

Update: 12/20/2020
After I converted all my HashRoute URL to BrowserRoute URL, and did some mod rewrite in tomcat, Google has successfully crawled my website and indexed it.

So you got your react app running locally (and probably angular too) with the backend service or REST API running on Spring Boot with Embedded Tomcat.

Then one day, you deployed your code to production that had external tomcat. The app home page came up but the URL links to your other react routes don’t seem to work !! If so you have landed on the right page to understand the problem and fix it.

First of all let’s touch base little bit on the Reac Routes. The ReactJS mainly provides two major kinds of Routes for your Components. These routes make your URLs looks slightly different.

Let’s say you have a site called http://www.nepalflow.com and your UI is running at the root context.

If you used a HashRouter, your URLs to other Routes (or pages) will looks something like

http://www.nepalflow.com/#/ui/contact_us

However, if you used a BrowserRouter, then your url fo the same Route would look like.

http://www.nepalflow.com/ui/contact_us

Rendering the HashRouter should not be much of a problem because the navigation is controlled by the JavaScript solely in the Browser itself.

However, the BrowserRouter will hit the backend server (also is the case if the site is bookmarked). Now suddenly, you might not have that Request Mapping on the API or the backend side, since you were not serving the UI from the backend and the Spring Boot or other frameworks will throw 404 error.

Well, at least that’s not how you intended your app to look like, right?

So here are a couple of solutions, I have done for my project to fix this issue of broken BrowserRouter links for React and I am pretty sure you can follow similar concept if you are working in an Angular App also.

SOLUTION FOR EMBEDDED TOMCAT CONTAINER:
Tomcat 8+ provides something called Valve, the equivalent of apache mode_rewrite. So in order for the backend to handle the UI url (which you need to forward to the SPA index.html), you can define a rewrite.config file.

Here is a sample rewrite.config file which I am dropping to the /resources folder. Inside this config file, all I am doing is forwarding certain URLs to index.html, in this case, anything that starts with /ui right after domain name. It is important to write this rule properly because you would still want the backend to serve other APIs. for that reason, I have all my backend APIs with the context /api/, which will be untouched by the /ui/ rules below.

RewriteRule ^/ui/(.*)$ /index.html [NC]

This rule is simply saying, if any request comes with a /ui/, send it the index.html and then the ReactJs or Angular will take care of rendering the proper component based on the route.

But simply having that rule is not enough. You need to tell the spring boot to configure the embedded container based on that file. So here is a little bit of code that I got from StackOverflow and added a few features, specially the Profile annotation to use it only for local embedded container. This is because the container is provided for me in production and I am using a different solution (described later in this article) for production.

Now when I start the appliation in embedded tomcat container (not the React’s 3000 port), I am getting the proper URLs rendered because of this rewrite feature of the container.

SOLUTION OR FOR PROVIDED CONTAINER:
As I mentioned above, I have an external tomcat in production, so this solution did not work. However, if you have an embedded tomcat container in production, it should work.

So for the provided tomat container, I had to go some extra miles. The idea is the same, you just rewrite your urls to forward the ui routes to React or Angular’s SPA index.html. However, the where to make this configuration change is what you probably need to know.

First of all I had to enable the Valve in my tomcat. For that I had to add the following to the server.xml (inside Host node) and in the context.xml just before the ending </context>

 <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />

The configuration above will enable Valve or mode rewrite in your tomcat. However, it needs to know the rewrite rules. After making the changes above, I had to drop the rewrite.config file with the following rule to /WEB-INF folder for the unarchive WAR file.

RewriteRule ^/ui/(.*)$ /index.html [NC]

Hope this helped some of you !! Have a good day.

Spring Boot Application – View Resolver for Static Angular Pages.

Build Your Angular Project Into resources/static folder. You can specify the folder in angular.json file.

Here is the generated files.

Have your controller return a string “index”

@Controller
@CrossOrigin
public class HomepageController {


@RequestMapping({"/"})
public String index() {
return "index";
}
}

Then enable WebMVC and tweak some configuraitons. This should serve the index.html from the resources/static folder.

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setSuffix(".html");
return bean;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
}