【问题标题】:Hibernate cannot create table when the entity annotation?实体注释时Hibernate无法创建表?
【发布时间】:2017-07-31 20:56:22
【问题描述】:

我的 web 项目,spring spring-mvc 和 hibernate,当 tomcat 启动时没有在 mysql db 中创建表。为什么?并且没有错误信息

会话工厂

 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
    <property name="dataSource" ref="dataSource" />  
    <property name="hibernateProperties">  
        <props>  
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>  
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>  
        </props>  
    </property>   
   <property name="packagesToScan">  
        <list>  
            <value>com.woodcoder.bean</value>  
        </list>  
    </property>   
</bean>  

属性

hibernate.hbm2ddl.auto=update
hibernate.show_sql=true  
hibernate.format_sql=false 

我试过hibernate.hbm2ddl.auto=create,还是一样。 为什么hibernate不创建表?

实体

   @Entity
    @Table(name="user_bean")

public class UserBean extends BaseBean {

    private String username;
    private String password;

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


}

基础实体

 @MappedSuperclass  
public class BaseBean implements Serializable{  
    /** 
     * ID 
     */
    @Id  
    @Column(name="id",length = 32, nullable = true)  
    @GeneratedValue(generator = "uuid")  
    @GenericGenerator(name = "uuid", strategy = "uuid")  
    private String id;  

    @Column(updatable = false)
    private Date createDate;  

    private Date modifyDate;  


    public String getId() {  
        return id;  
    }  

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

    public Date getCreateDate() {  
        return createDate;  
    }  

    public void setCreateDate(Date createDate) {  
        this.createDate = createDate;  
    }  

    public Date getModifyDate() {  
        return modifyDate;  
    }  

    public void setModifyDate(Date modifyDate) {  
        this.modifyDate = modifyDate;  
    }  

【问题讨论】:

  • 开启调试日志(如果你没有)。也许模式工具正在查找错误,但由于日志级别较低,您没有看到它们。其次,确保您的 XML 文件已正确替换为部署战中的值。这通常可能是某些事情没有发生的原因。
  • 根据您的建议,我将日志级别降低到 DEBUG,然后我获得更多信息,但没有创建表。我检查了tomcat中的部署目录,所有属性和xml文件都已正确部署。

标签: mysql spring hibernate tomcat7


【解决方案1】:

是的,我遇到了问题。 在休眠配置中,每行末尾都有一些空格,虽然我看不到它们。我删除了这些空格,创建了表。

    hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2020-06-17
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多