如何让Hibernate为我创建数据库结构?以下是我的applicationContext.xml文件的片段:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.application.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
虽然我已经将hbm2ddl.auto属性指定为create,但Hibernate并没有为我创建架构。当我使用session.save()调用将对象保存到数据库时,我得到如下错误:
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'Person'.
这是我对session.save()
的调用public void createNewPerson(final String personName, final int personAge)
{
final Person person = new Person();
person.setName(personName);
person.setAge(personAge);
final Session session = this.getSessionFactory().openSession();
session.beginTransaction();
session.save(person);
session.getTransaction().commit();
session.close();
}
模型类
@Entity
public class Person
{
@Id
@GeneratedValue
private int id;
private String name;
private int age;
//getters and setters excluded
}
答案 0 :(得分:0)
使用
让它工作hibernate.hbm2ddl.auto
而不是
hbm2ddl.auto