设置初始表,用于基本登录和“记住我”。
然而,hibernate 无法根据我带注释的类创建表......我不明白为什么。以下是位于 /WEB-INF/
下的 applicationContext.xml
的相关片段:
<context:annotation-config />
<context:component-scan base-package="com.mycompany" />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:property-placeholder location="file:${catalina.home}/conf/database.properties" ignore-unresolvable="true"/>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}"
p:user="${app.jdbc.username}"
p:password="${app.jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
和位于 /src/main/resources
下的 hibernate.cfg.xml
并由 maven 复制到 /WEB-INF/classe
s:
<hibernate-configuration>
<session-factory>
<!-- We're using MySQL database so the dialect needs to MySQL as well-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Enable this to see the SQL statements in the logs-->
<property name="show_sql">false</property>
<!-- <property name="hbm2ddl.auto">update</property> -->
<property name="hbm2ddl.auto">Create</property>
</session-factory>
</hibernate-configuration>
我的类(class)如下所示:
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.Index;
@Entity
@Table(name = "office")
public class Office extends Base {..
我的应用程序启动正常,我什至可以登录(因为我也有内存服务)。但是,没有创建数据库表(从后端访问),并且我也创建的持久登录不起作用,因为未创建相关表(当我尝试注销时实际上收到错误,指出*错误的sql没有表perpetitive_login存在*)。应用程序启动时没有出现错误。
我错过了什么?
最佳答案
您可以打开 org.hibernate.tool.hbm2ddl 的跟踪日志记录。这将向您显示 Hibernate 在创建架构时正在生成的 DDL。
来自 Hibernate 源代码:
String autoSchemaExport = properties.getProperty(Environment.HBM2DDL_AUTO);
if ( "validate".equals(autoSchemaExport) ) settings.setAutoValidateSchema(true);
if ( "update".equals(autoSchemaExport) ) settings.setAutoUpdateSchema(true);
if ( "create".equals(autoSchemaExport) ) settings.setAutoCreateSchema(true);
if ( "create-drop".equals(autoSchemaExport) ) {
settings.setAutoCreateSchema(true);
settings.setAutoDropSchema(true);
}
看起来你需要一个小的c来创建(正如@aishwaryaless所指出的)
关于java - 使用 Spring 和 Hibernate 创建新项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8256498/