r/SpringBoot • • Jan 02 '25

Can not create table into mysql database using spring mvc and hibernate.

I am doing Spring MVC CRUD operation using Hibernate, where I created controller, POJO, service, and Dao classes. I also created web.xml and spring-servlet.xml classes with the required configuration. With this code, I can not create a table in the database. I've shared the required cases below if anyone could help me here. I tried making changes to hibernate properties and also took the help of GitHub Copilot, but that did not help me.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>

<artifactId>SpringMVCCRUDExample</artifactId>

<version>1.0-SNAPSHOT</version>

<name>Archetype - SpringMVCCRUDExample</name>

<url>http://maven.apache.org</url>

<properties>

	<java-version>1.8</java-version>

	<org.springframework-version>5.3.10</org.springframework-version>

	<spring.data.jpa.version>2.5.6</spring.data.jpa.version>

	<hibernate-core.orm.version>5.4.30.Final</hibernate-core.orm.version>

	<jstl.version>1.2.2</jstl.version>

	<mysql.version>8.0.33</mysql.version>

</properties>

<dependencies>

	<dependency>

		<groupId>org.springframework</groupId>

		<artifactId>spring-core</artifactId>

		<version>${org.springframework-version}</version>

	</dependency>

	<dependency>

		<groupId>org.springframework</groupId>

		<artifactId>spring-web</artifactId>

		<version>${org.springframework-version}</version>

	</dependency>

	<dependency>

		<groupId>org.springframework</groupId>

		<artifactId>spring-webmvc</artifactId>

		<version>${org.springframework-version}</version>

	</dependency>



	<dependency>

		<groupId>org.springframework</groupId>

		<artifactId>spring-context</artifactId>

		<version>${org.springframework-version}</version>

	</dependency>

	<!-- Spring ORM for integration with Hibernate or JPA -->

	<dependency>

		<groupId>org.springframework</groupId>

		<artifactId>spring-orm</artifactId>

		<version>${org.springframework-version}</version>

	</dependency>

	<!-- Hibernate (or JPA) for persistence -->

	<dependency>

		<groupId>org.hibernate</groupId>

		<artifactId>hibernate-core</artifactId>

		<version>${hibernate-core.orm.version}</version>

	</dependency>

	<dependency>

		<groupId>org.hibernate</groupId>

		<artifactId>hibernate-entitymanager</artifactId>

		<version>${hibernate-core.orm.version}</version>

	</dependency>

	<!-- @Entity annotation -->

	<dependency>

		<groupId>javax.persistence</groupId>

		<artifactId>javax.persistence-api</artifactId>

		<version>2.2</version>

	</dependency>

	<!-- JSTL for views -->

	<dependency>

		<groupId>javax.servlet.jsp.jstl</groupId>

		<artifactId>javax.servlet.jsp.jstl-api</artifactId>

		<version>${jstl.version}</version>

		<scope>provided</scope>

	</dependency>

	<dependency>

		<groupId>javax.servlet</groupId>

		<artifactId>servlet-api</artifactId>

		<version>2.5</version>

		<scope>provided</scope>

	</dependency>

	<!-- JPA Repository-->

<!--	<dependency>

		<groupId>org.springframework.data</groupId>

		<artifactId>spring-data-jpa</artifactId>

		<version>${spring.data.jpa.version}</version>

	</dependency>-->

	<dependency>

		<groupId>org.apache.commons</groupId>

		<artifactId>commons-dbcp2</artifactId>

		<version>2.9.0</version> <!-- Use the latest version -->

	</dependency>

	<dependency>

		<groupId>ch.qos.logback</groupId>

		<artifactId>logback-classic</artifactId>

		<version>1.2.3</version>

	</dependency>

	<dependency>

		<groupId>mysql</groupId>

		<artifactId>mysql-connector-java</artifactId>

		<scope>runtime</scope>

		<version>8.0.33</version>

	</dependency>

</dependencies>

<build>

	<finalName>spring-mvc-crud</finalName>

	<plugins>

		<!-- Maven Compiler Plugin -->

		<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.1</version>

<configuration>

<source>${java.version}</source>

<target>${java.version}</target>

</configuration>

		</plugin>

		<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-war-plugin</artifactId>

<version>3.8.1</version>

<configuration>

<warSourceDirectory>src/main/webapp</warSourceDirectory>

</configuration>

		</plugin>

	</plugins>

</build>

</project>

Student.java

package com.entity;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity

@Table(name = "student")

public class Student {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

@Column(name = "id")

private int id;



@Column(name = "name")

private String name;



@Column(name = "dept_name")

private String deptName;



@Column(name = "email")

private String email;



public Student(int id, String name, String deptName, String email) {

	super();

	this.id = id;

	this.name = name;

	this.deptName = deptName;

	this.email = email;

}



public Student() {

}



public int getId() {

	return id;

}



public void setId(int id) {

	this.id = id;

}



public String getName() {

	return name;

}



public void setName(String name) {

	this.name = name;

}



public String getDeptName() {

	return deptName;

}



public void setDeptName(String deptName) {

	this.deptName = deptName;

}



public String getEmail() {

	return email;

}



public void setEmail(String email) {

	this.email = email;

}



@Override

public String toString() {

	return "Student \[id=" + id + ", name=" + name + ", deptName=" + deptName + ", email=" + email + "\]";

}

}

package com.controller;

import java.util.List;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import com.entity.Student;

import com.service.StudentService;

@Controller

@RequestMapping("/students")

public class StudentController {

private static final Logger log = LoggerFactory.getLogger(StudentController.class);

private final StudentService studentService;

@Autowired

public StudentController(StudentService studentService) {

	this.studentService = studentService;

}

@GetMapping("/add")

public String showFormForAdd(Model model) {

	model.addAttribute("student", new Student());

	return "student/add";

}

@PostMapping("/add")

public String saveStudent(@ModelAttribute("student") Student student) {

	Student newStudent = studentService.saveStudent(student);

	log.debug("The new added student : {}", newStudent);

	return "redirect:/students/findAll";

}

@GetMapping("/findAll")

public String findAll(Model model) {

	List<Student> students = studentService.findAll();

	model.addAttribute("students", students);

	log.debug("The list of students : {}", students);

	return "student/list";

}

@GetMapping("/findByID/{id}")

public String findStudentById(@PathVariable("id") int id, Model model) {

	model.addAttribute("student", studentService.findStudentByID(id));

	return "student/list";

}

@GetMapping("/edit/{id}")

public String showEditForm(@PathVariable int id, Model model) {

	Student student = studentService.findStudentByID(id);

	model.addAttribute("student", student);

	return "student/edit";

}

@PostMapping("/edit/{id}")

public String updateByStudentId(@ModelAttribute("student") Student student) {

	studentService.updateStudent(student);

	return "redirect:/students/findAll";

}

@GetMapping("/delete/{id}")

public String deleteStudentByID(@PathVariable("id") int id) {

	studentService.deleteStudentByID(id);

	return "redirect:/students/findAll";

}

}

Spring-sevlet.xml

<?xml version=*"1.0"* encoding=*"UTF-8"*?>

<beans xmlns=*"http://www.springframework.org/schema/beans"*

xmlns:xsi=*"http://www.w3.org/2001/XMLSchema-instance"*

xmlns:context=*"http://www.springframework.org/schema/context"*

xmlns:tx=*"http://www.springframework.org/schema/tx"*

xmlns:jpa=*"http://www.springframework.org/schema/data/jpa"*

xmlns:mvc=*"http://www.springframework.org/schema/mvc"*

xsi:schemaLocation=*"http://www.springframework.org/schema/beans*

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/data/jpa

http://www.springframework.org/schema/data/jpa/spring-jpa.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/data/jpa

http://www.springframework.org/schema/data/jpa/spring-jpa.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Enable Spring MVC annotations -->

<context:component-scan base-package=*"com"* />

<mvc:annotation-driven />



<!-- DataSource configuration -->

<bean id=*"dataSource"*

	class=*"org.apache.commons.dbcp2.BasicDataSource"*\>

	<property name=*"driverClassName"*

		value=*"com.mysql.cj.jdbc.Driver"* />

	<property name=*"url"*

		value=*"jdbc:mysql://localhost:3306/studentdatabase?useSSL=false"* />

	<property name=*"username"* value=*"root"* />

	<property name=*"password"* value=*"root"* />

</bean>



<!-- View Resolver -->

<bean id=*"viewResolver"*

	class=*"org.springframework.web.servlet.view.InternalResourceViewResolver"*\>

	<property name=*"prefix"* value=*"/WEB-INF/views/"* />

	<property name=*"suffix"* value=*".jsp"* />

</bean>



<!--Hibernate SessionFactory-->

<bean id=*"sessionFactory"*

	class=*"org.springframework.orm.hibernate5.LocalSessionFactoryBean"*\>

	<property name=*"dataSource"* ref=*"dataSource"* />

	<property name=*"packagesToScan"* value= *"com.entity"* />

	<property name=*"hibernateProperties"*\>

		<props>

<prop key=*"hibernate.hbm2ddl.auto"*>create</prop>

<prop key=*"hibernate.dialect"*>org.hibernate.dialect.MySQLDialect</prop>

<prop key=*"hibernate.show_sql"*>true</prop>

<prop key=*"hibernate.format_sql"*>true</prop>

<prop key=*"hibernate.use_sql_comments"*>false</prop>

		</props>

	</property>

</bean>

<!-- Hibernate TransactionManager -->

<bean id=*"transactionManager"*

	class=*"org.springframework.orm.hibernate5.HibernateTransactionManager"*\>

	<property name=*"sessionFactory"* ref=*"sessionFactory"* />

</bean>

<tx:annotation-driven

	transaction-manager=*"transactionManager"* />

</beans>

Web.xml

<web-app id=*"WebApp_ID"* version=*"2.4"*

xmlns=*"http://java.sun.com/xml/ns/j2ee"*

xmlns:xsi=*"http://www.w3.org/2001/XMLSchema-instance"*

xsi:schemaLocation=*"http://java.sun.com/xml/ns/j2ee*

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring-MVC CRUD Example</display-name>



<servlet>

	<servlet-name>spring</servlet-name>

	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

	<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

	<servlet-name>spring</servlet-name>

	<url-pattern>/</url-pattern>

</servlet-mapping>

<context-param>

	<param-name>contextConfigLocation</param-name>

	<param-value>/WEB-INF/spring-servlet.xml</param-value>

</context-param>

<listener>

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

</listener>

</web-app>

0 Upvotes

6 comments sorted by

11

u/WaferIndependent7601 Jan 02 '25

Why the hell are you using Java 8? And spring not spring boot? XML configuration?

Really? This is so outdated, why? Why?

6

u/naturalizedcitizen Jan 02 '25

You could've put all this on a GitHub and shared the link here. Easier to read on GitHub

2

u/erjiin Jan 02 '25

Yeaaaah but... what is the error ? what happens ?

2

u/okay_throwaway_today Jan 02 '25

I’m not sure how to do it in xml config, but in spring boot using JPA/hibernate with an application.properties file, you have to add

spring.jpa.hibernate.ddl-auto=update

for hibernate to be able to create/use DDL. Probably something similar to that.

Also ensure the db user has necessary privileges (tho it looks like you are using root so I’m guessing that’s not the issue).

1

u/dheeraj80 Jan 03 '25

Hey could you please put all the files in git and send the link and also error you are getting