开发者社区> 问答> 正文

SpringMVC 4 + Hibernate4的Entity自动扫描配置问题!

基础描述J2ee 7,IDE:IDEA13.1,SpringMVC 4.0.4.RELEASE Hibernate 4.3.5.Final

我想重新建立自动化框架,以前通过springmvc3+hibernate3 自动扫描@Controller自动扫描和@Entity自动建表

现在迁移到了Spring4 与hibernate4 但是Entity就是不创建

配置如下

web.xml

<web-app 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 Application</display-name>
 
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

mvc-despatcher-servlet.xml

<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:mvc="http://www.springframework.org/schema/mvc" 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
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 
    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="com.springapp.mvc.web"/>
    <!-- Configures the @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="/WEB-INF/resources/" />
 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- jsp page used the jstl-->
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
 
    <!-- hibernate beans  -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!--指定Hibernate核心配置文件 数据库配置信息在hibernate.cfg.xml内-->
 
        <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml"/>
        <property name="packagesToScan" value="com.springapp.mvc.entity.*" />
 
    </bean>
    <!--
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    -->
 
 
</beans>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://192.168.1.159:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
 
        <!-- 数据库方言 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
        <!-- DB schema will be updated if needed -->
        <property name="hbm2ddl.auto">update</property>
 
        <!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于差错,程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。项目部署后可以设置为false,提高运行效率-->
        <property name="hibernate.show_sql">true </property>
    </session-factory>
</hibernate-configuration>

运行

[2014-05-06 02:47:41,294] Artifact OutSource:war exploded: Artifact is being deployed, please wait...
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
06-May-2014 14:47:50.836 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory Z:\hava\build\tomcat\apache-tomcat-8.0.3\webapps\manager
06-May-2014 14:47:51.179 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.validator.internal.util.Version.<clinit> HV000001: Hibernate Validator 5.1.0.Final
06-May-2014 14:47:53.045 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.annotations.common.reflection.java.JavaReflectionManager.<clinit> HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
06-May-2014 14:47:53.078 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.Version.logVersion HHH000412: Hibernate Core {4.3.5.Final}
06-May-2014 14:47:53.087 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.cfg.Environment.<clinit> HHH000206: hibernate.properties not found
06-May-2014 14:47:53.095 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.cfg.Environment.buildBytecodeProvider HHH000021: Bytecode provider name : javassist
06-May-2014 14:47:53.245 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.cfg.Configuration.configure HHH000044: Configuring from URL: file:/Z:/hava/Workspace/J2ee/OutSource/target/OutSource/WEB-INF/hibernate.cfg.xml
06-May-2014 14:47:53.423 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.cfg.Configuration.doConfigure HHH000041: Configured SessionFactory: null
06-May-2014 14:47:54.054 WARN [RMI TCP Connection(3)-127.0.0.1] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure HHH000402: Using Hibernate built-in connection pool (not for production use!)
06-May-2014 14:47:54.065 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://192.168.1.159:3306/test]
06-May-2014 14:47:54.065 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator HHH000046: Connection properties: {user=root, password=****}
06-May-2014 14:47:54.065 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator HHH000006: Autocommit mode: false
06-May-2014 14:47:54.075 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure HHH000115: Hibernate connection pool size: 20 (min=1)
06-May-2014 14:47:54.516 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.dialect.Dialect.<init> HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
06-May-2014 14:47:54.849 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.engine.transaction.internal.TransactionFactoryInitiator.initiateService HHH000399: Using default transaction strategy (direct JDBC transactions)
06-May-2014 14:47:54.887 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory.<init> HHH000397: Using ASTQueryTranslatorFactory
06-May-2014 14:47:55.527 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.tool.hbm2ddl.SchemaUpdate.execute HHH000228: Running hbm2ddl schema update
06-May-2014 14:47:55.527 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.tool.hbm2ddl.SchemaUpdate.execute HHH000102: Fetching database metadata
06-May-2014 14:47:55.533 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.tool.hbm2ddl.SchemaUpdate.execute HHH000396: Updating schema
06-May-2014 14:47:55.536 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.tool.hbm2ddl.SchemaUpdate.execute HHH000232: Schema update complete
[2014-05-06 02:47:55,686] Artifact OutSource:war exploded: Artifact is deployed successfully
[2014-05-06 02:47:55,686] Artifact OutSource:war exploded: Deploy took 14,392 milliseconds

Entity扫描的类:

package com.springapp.mvc.entity;
 
import javax.persistence.Entity;
import javax.persistence.Table;
 
import org.hibernate.validator.constraints.NotBlank;
 
/**
 * Created by hava on 2014/5/5.
 */
@Entity
@Table(name="servicestatus")
public class ServiceStatus extends IdEntity {
 
    private String name;
    private Boolean status;
 
    // JSR303 BeanValidator的校验规则
    //@NotBlank
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    // JSR303 BeanValidator的校验规则
    //@NotBlank
    public Boolean getStatus() {
        return status;
    }
 
    public void setStatus(Boolean status) {
        this.status = status;
    }
}

IdEntity

package com.springapp.mvc.entity;
 
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
 
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
 
/**
 * 统一定义id的entity基类.
 * 
 * 基类统一定义id的属性名称、数据类型、列名映射及生成策略.
 * Oracle需要每个Entity独立定义id的SEQUCENCE时,不继承于本类而改为实现一个Idable的接口。
 * 
 * @author calvin
 */
// JPA 基类的标识
@MappedSuperclass
public abstract class IdEntity {
 
    protected Long id;
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
}

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>com.springapp</groupId>
    <artifactId>OutSource</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>OutSource</name>
 
    <properties>
        <spring.version>4.0.4.RELEASE</spring.version>
        <hibernate.version>4.3.5.Final</hibernate.version>
    </properties>
 
    <dependencies>
        <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>
 
        <!-- servlet3 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
 
 
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
 
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
 
        <!--定时任务调度使用-->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
 
        <!--postgresql for jdbc driver-->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.2-1002-jdbc4</version>
        </dependency>
 
        <!-- mysql driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.15</version>
        </dependency>
 
 
        <!-- jstl  -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>
 
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- titles by jstl-->
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-extras</artifactId>
            <version>3.0.3</version>
        </dependency>
 
        <!--XML reader-->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
 
        <!-- hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
 
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
 
 
        <!-- hibernate 验证数据安全 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
 
 
 
 
 
 
    </dependencies>
 
    <build>
        <finalName>OutSource</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

展开
收起
小旋风柴进 2016-03-05 15:29:43 4128 0
1 条回答
写回答
取消 提交回答
  • 需要把mvc.conf.xml里面的

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

    修改成为

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

    必须在扫描Entity的上一级

    2019-07-17 18:53:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载