MiH
MiH

Reputation: 421

How to config a SpringMVC + Hibernate?

I'm trying to create a springmvc project config with hibernate using web.xml

My prob is when I run, it's not auto create a User table.

This is my code:

File spring-config.xml

<!-- Config for soap -->

<bean id="UserWs" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition" lazy-init="true">
    <property name="schemaCollection">
        <bean class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
            <property name="inline" value="true" />
            <property name="xsds">
                <list>
                    <value>schema/schema1.xsd</value>
                </list>
            </property>
        </bean>
    </property>
    <property name="portTypeName" value="UserService"/>
    <property name="serviceName" value="UserService" />
    <property name="locationUri" value="/ws"/>
</bean>

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />

<context:component-scan base-package="com.higgsup.internship.soap" />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/springmvc"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.higgsup.internship.soap.model" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven />
<bean id="transactionManager"
      class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="userDao" class="com.higgsup.internship.soap.dao.UserDAOImpl"></bean>

File web.xml:

  <!--
    Main configuration file for this Spring web application.
-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/config/spring-config.xml
    </param-value>
</context-param>
<!--
    Loads the Spring web application context, using the files defined above.
-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--
    Define the Spring WS Servlet. The 'transformWsldLocations' param means
    that any WSDLs generated are context-aware and contain the correct
    path to their exposed port types. The 'contextConfigLocation' param
    with an empty value means that the Spring context won't try to load
    a file called webservices-servlet.xml
-->
<servlet>
    <servlet-name>services</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/config/spring-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>services</servlet-name>
    <url-pattern>/ws</url-pattern>
</servlet-mapping>

File UserDAO:

package com.higgsup.internship.soap.dao;

import com.higgsup.internship.soap.model.User;

public interface UserDAO {

public User getUser(Integer id);
}

File UserDaoImpl:

package com.higgsup.internship.soap.dao;

import com.higgsup.internship.soap.model.User;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class UserDAOImpl implements UserDAO {

@PersistenceContext
private EntityManager entityManager;

public User getUser(Integer id) {
    User user = entityManager.find(User.class, id);
    return user;
}
}

File User:

package com.higgsup.internship.soap.model;

public class User {

private int id;
private String username;
private String password;
private String email;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

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;
}

public String getEmail() {
    return email;
}

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

}

This is my log:

02-Jul-2016 23:46:57.342 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/9.0.0.M4 02-Jul-2016 23:46:57.343 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Mar 12 2016 11:39:59 UTC 02-Jul-2016 23:46:57.343 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 9.0.0.0 02-Jul-2016 23:46:57.343 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Windows 7 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 6.1 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: x86 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: C:\Program Files (x86)\Java\jdk1.8.0_60\jre 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_60-b27 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: C:\Users\hunghip.IntelliJIdea15\system\tomcat\Unnamed_spring-soap-hibernate-xml_2 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: D:\Setup\tomcat\apache-tomcat-9.0.0.M4 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote= 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false 02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false 02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1 02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\hunghip.IntelliJIdea15\system\tomcat\Unnamed_spring-soap-hibernate-xml_2\conf\logging.properties 02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\hunghip.IntelliJIdea15\system\tomcat\Unnamed_spring-soap-hibernate-xml_2 02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=D:\Setup\tomcat\apache-tomcat-9.0.0.M4 02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=D:\Setup\tomcat\apache-tomcat-9.0.0.M4\temp 02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.7 using APR version 1.5.2. 02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 02-Jul-2016 23:46:58.376 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.2h 3 May 2016) 02-Jul-2016 23:46:58.606 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"] 02-Jul-2016 23:46:58.625 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 02-Jul-2016 23:46:58.630 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"] 02-Jul-2016 23:46:58.631 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 02-Jul-2016 23:46:58.632 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 2099 ms 02-Jul-2016 23:46:58.674 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina 02-Jul-2016 23:46:58.674 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/9.0.0.M4 02-Jul-2016 23:46:58.682 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [http-nio-8080] 02-Jul-2016 23:46:58.690 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [ajp-nio-8009] 02-Jul-2016 23:46:58.692 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 59 ms Connected to server [2016-07-02 11:46:59,006] Artifact spring-soap-hibernate-xml:war: Artifact is being deployed, please wait... 02-Jul-2016 23:47:02.461 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 02-Jul-2016 23:47:02.520 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started 02-Jul-2016 23:47:02.625 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Sat Jul 02 23:47:02 ICT 2016]; root of context hierarchy 02-Jul-2016 23:47:02.673 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/config/spring-config.xml] 02-Jul-2016 23:47:03.109 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor. JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 02-Jul-2016 23:47:03.357 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory Building JPA container EntityManagerFactory for persistence unit 'default' 02-Jul-2016 23:47:03.385 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.jpa.internal.util.LogHelper.logPersistenceUnitInformation HHH000204: Processing PersistenceUnitInfo [ name: default ...] 02-Jul-2016 23:47:03.507 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.Version.logVersion HHH000412: Hibernate Core {5.2.1.Final} 02-Jul-2016 23:47:03.509 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.cfg.Environment. HHH000206: hibernate.properties not found 02-Jul-2016 23:47:03.511 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.cfg.Environment.buildBytecodeProvider HHH000021: Bytecode provider name : javassist 02-Jul-2016 23:47:03.583 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.annotations.common.reflection.java.JavaReflectionManager. HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 02-Jul-2016 23:47:04.038 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.dialect.Dialect. HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 02-Jul-2016 23:47:04.484 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.tool.schema.internal.SchemaCreatorImpl.applyImportSources HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@93c19f' 02-Jul-2016 23:47:04.495 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.buildNativeEntityManagerFactory Initialized JPA EntityManagerFactory for persistence unit 'default' 02-Jul-2016 23:47:04.781 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Sat Jul 02 23:47:02 ICT 2016]; root of context hierarchy 02-Jul-2016 23:47:04.831 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Sat Jul 02 23:47:02 ICT 2016]; root of context hierarchy 02-Jul-2016 23:47:04.906 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler Mapped URL path [/resources/] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0' 02-Jul-2016 23:47:05.023 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 2503 ms 02-Jul-2016 23:47:05.056 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'services': initialization started 02-Jul-2016 23:47:05.059 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'services-servlet': startup date [Sat Jul 02 23:47:05 ICT 2016]; parent: Root WebApplicationContext 02-Jul-2016 23:47:05.060 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/config/spring-config.xml] 02-Jul-2016 23:47:05.116 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor. JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 02-Jul-2016 23:47:05.144 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory Building JPA container EntityManagerFactory for persistence unit 'default' 02-Jul-2016 23:47:05.144 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.jpa.internal.util.LogHelper.logPersistenceUnitInformation HHH000204: Processing PersistenceUnitInfo [ name: default ...] 02-Jul-2016 23:47:05.161 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.dialect.Dialect. HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 02-Jul-2016 23:47:05.170 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.tool.schema.internal.SchemaCreatorImpl.applyImportSources HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@93c19f' 02-Jul-2016 23:47:05.174 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.buildNativeEntityManagerFactory Initialized JPA EntityManagerFactory for persistence unit 'default' 02-Jul-2016 23:47:05.226 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'services-servlet': startup date [Sat Jul 02 23:47:05 ICT 2016]; parent: Root WebApplicationContext 02-Jul-2016 23:47:05.243 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'services-servlet': startup date [Sat Jul 02 23:47:05 ICT 2016]; parent: Root WebApplicationContext 02-Jul-2016 23:47:05.289 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler Mapped URL path [/resources/] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0' 02-Jul-2016 23:47:05.380 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'services': initialization completed in 323 ms [2016-07-02 11:47:05,399] Artifact spring-soap-hibernate-xml:war: Artifact is deployed successfully [2016-07-02 11:47:05,400] Artifact spring-soap-hibernate-xml:war: Deploy took 6,394 milliseconds 02-Jul-2016 23:47:08.688 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory D:\Setup\tomcat\apache-tomcat-9.0.0.M4\webapps\manager 02-Jul-2016 23:47:08.872 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory D:\Setup\tomcat\apache-tomcat-9.0.0.M4\webapps\manager has finished in 184 ms

Upvotes: 0

Views: 158

Answers (1)

Chathuranga Tennakoon
Chathuranga Tennakoon

Reputation: 2189

it seems to be that you have not added jpa annotation to model the User class. can you try with following model class?

package com.higgsup.internship.soap.model;

@Entity
@Table(name = "tbl_user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "email")
    private String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }

    public String getEmail() {
        return email;
    }

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

Upvotes: 1

Related Questions