JPA: Difference between the createQuery vs createNamedQuery

Advertisements

Static method

One rule-of-thumb: ask yourself “does it make sense to call this method, even if no Obj has been constructed yet?” If so, it should definitely be static.

So in a class Car you might have a method double convertMpgToKpl(double mpg) which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But void setMileage(double mpg) (which sets the efficiency of one particular Car) can’t be static since it’s inconceivable to call the method before any Car has been constructed.

Json to Java object conversion

Let’s say you have rest service that posts some data to your server. The rest services uses JSON format to send data, but your server might not understand the JSON, therefore you will need to convert the JSON to Java object. Below is the sample of such service:

 

@POST
	@Path("/statistics")
	@Consumes(APPLICATION_JSON_UTF8)
	public Response getStatistics(String json)
												throws JsonGenerationException,
												JsonMappingException,

												IOException {

		ObjectMapper mapper = new ObjectMapper();
		
		SomeCustomerTO someUser = mapper.readValue(json, SomeCustomerTO .class);
		return null;
	}

Always good idea to include some logging to see if you receiving the correct json

Bug: projects in eclipse are not visible

Sometimes after checking out projects from the repository, they are not visible in the package explorer.

Solution:

Do this in either the Project Explorer, Java, Java EE or the Enterprise Explorer view. In the view, click the little down-arrow to open a menu, and choose Deselect Working Set. This worked for me, and my project that was already there that wasn’t showing up now shows up

Adding the Oracle connectors to IntelliJ IDEA

Libraries cannot be directly used in any program if not properly added to the project gradle files.

This can easily be done in smart IDEs like inteli J.

1) First as a convention add a folder names ‘libs’ under your project src file. (this can easily be done using the IDE itself)

2) then copy or add your library file (eg: .jar file) to the folder named ‘libs’

3) now you can see the library file inside the libs folder. Now right click on the file and select ‘add as library’. And this will fix all the relevant files in your program and library will be directly available for your use.