Create Tables From Entity Definitions From JPA Spring

How to create database tables from JPA using Hibernate

We know ORM. It will be a nightmare or a bliss based on the angle you are looking at it. This time a bliss :)

If you have started development from the java side and you have all the entities figured out, then hibernate can create the database for you from the entities.

For this example I will be assuming you are using Spring and Hibernate, with that, how do you create the database from the entities

Initial Setup assumptions

So, I will be assuming you have the spring being setup already, the configuration file for your application would look like:


jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/database
jdbc.username = root
jdbc.password = 123
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = create|create-drop|update|none

Here the values to hibernate.hbm2ddl.auto could be create – if you want to crate during the sessionFactory initiation, create-drop, update when you want to update the exiting database when there is a change on the schema or none if you don’t want to do anything with it.

java class not found exception

spring error ClassNotFoundException org.springframework.web.context.ContextLoaderList

ClassNotFoundException org.springframework.web.context.ContextLoaderList

When working on spring framework, you might get this error specially on eclipse. I am assuming you are working on maven and eclipse environment.

The ClassNotFound exception is a bit self explanatory that eclipse could not find the mentioned class ContextLoaderList

The web.xml file for the spring configuration might look like


<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/application-context.xml</param-value>
 </context-param>

 <listener>
   <listener-class>
         org.springframework.web.context.ContextLoaderListener
   </listener-class>
 </listener>

With this, you might have similar file like application-context in the WEB-INF path

How to fix ClassNotFoundException for ContextLoaderList

The problem will during deployment. It is not basically compile time error. It is rather runtime error.

So, we have to make sure the jar responsible for this class is available in the classpath that eclipse is reading.

Pom dependency check

Make sure you have the same/similar configuration as the one listed below on your pom file.


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

You can have the right version for your spring in the parameter section or you can just replace it with the current version you have.

Using Deployment Assembly fix in eclipse

As we know, it is a deployment time problem, hence we have to tell the assembly where it can get its dependencies.

      Right click on the project
      Go to/search for Deployment Assembly
      Click on Add
      Select on Java Build path entries
      Select all the maven dependencies
      Finish
      Clean and build the project

Either of the above methods should fix the problem

spring boot

Spring Data error: getOutputStream() has already been called for this response

Spring data with spring boot error when using mapping

Spring boot has made life of the java developers a bliss. Thanks a million for the dedicated guys in spring.

Saying that, if you are working typical database related project with spring boot, you might have some error like this


java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:624) ~[tomcat-embed-core-8.5.16.jar!/:8.5.16]
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:211) ~[tomcat-embed-core-8.5.16.jar!/:8.5.16]
Continue reading Spring Data error: getOutputStream() has already been called for this response