0

I am having trouble creating a database table in MySQL using Spring MVC and Tomcat 10.1.71. I have defined the table in my code and expected it to be automatically created when I run my application, but it is not being created. I have checked my database and the table does not exist.

project structure:

-->SpringMVC
---->src
------>main
-------->java
---------->com.example.SpringMVC
------------>controller
-------------->RegistrationController.java
------------>dao
-------------->UserDao.java
------------>model
-------------->User.java
------------>service
-------------->UserService.java
------------>HelloServlet.java
-------->resources
-------->webapp
----------->WEB-INF
------------->spring-servlet.xml
------------->web.xml
---->target
---->pom.xml

Here is my code for creating the table:

User.java

package com.example.registrationmanagementmvc.model;

import javax.persistence.*;

@Entity
@Table(name = "user_table")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String email;
    private String username;
    private String password;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "email='" + email + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
UserDao.java

package com.example.registrationmanagementmvc.dao;

import com.example.registrationmanagementmvc.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;

//    @Transactional
    public int createUser(User user) {
        Integer id = (Integer) this.hibernateTemplate.save(user);
        return id;
    }
}

UserService.java

package com.example.registrationmanagementmvc.service;

import com.example.registrationmanagementmvc.dao.UserDao;
import com.example.registrationmanagementmvc.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    @Transactional
    public int createUser(User user) {

        return this.userDao.createUser(user);
    }
}

RegistrationController.java

package com.example.registrationmanagementmvc.controller;

import com.example.registrationmanagementmvc.model.User;
import com.example.registrationmanagementmvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RegistrationController {
    @Autowired
    private UserService userService;
    @RequestMapping("/registration")
    public String showRegistrationForm() {
        return "registration";
    }

    @RequestMapping(value = "/processRegistrationForm", method = RequestMethod.POST)
    public String processRegistrationForm(@ModelAttribute User user) {
        System.out.println(user);
        this.userService.createUser(user);
        System.out.println("User is created");
        return "home";
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>RegistrationManagementMVC</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>RegistrationManagementMVC</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <junit.version>5.9.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.0.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>6.0.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.6.7.Final</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.32</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.0.2.Final</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
         version="6.0">
    
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       https://www.springframework.org/schema/tx/spring-tx.xsd
">
    <tx:annotation-driven/>

    <context:component-scan base-package="com.example.registrationmanagementmvc"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" name="viewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>


    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="ds">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/spring_jdbc"/>
        <property name="username" value="root"/>
        <property name="password" value="5v*RjZZWSVUXnVH#"/>
    </bean>

    <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory">
        <property name="dataSource" ref="ds"/>
<!--        <property name="packagesToScan" value="com.example.registrationmanagementmvc.model"/>-->

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>
                    com.example.registrationmanagementmvc.model.User
                </value>
            </list>
        </property>
    </bean>

    <bean class="org.springframework.orm.hibernate5.HibernateTemplate" name="hibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" name="transactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

</beans>

Error enter image description here

Any help would be greatly appreciated. Thank you in advance!

5
  • You should avoid posting screen shots of text in questions; instead, paste the text directly into the question (with appropriate formatting).
    – E-Riz
    Commented Mar 10, 2023 at 14:14
  • Hibernate does not automatically create or update the schema unless you specifically configure it to do that. Show us your app config properties and/or your SessionFactory initialization code.
    – E-Riz
    Commented Mar 10, 2023 at 14:15
  • spring-servlet.xml file contains all the configuration related hibernate and SessionFactory.
    – Osman Goni
    Commented Mar 10, 2023 at 14:59
  • The screen shot shows a Spring unsatisfied dependency error. What are the details of that?
    – E-Riz
    Commented Mar 10, 2023 at 15:23
  • RegistrationController need instance of UserService, UserService need UserDao instance, UserDao need HibernateTemplate instance but HibernateTemplate instance is not created.
    – Osman Goni
    Commented Mar 10, 2023 at 16:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.