事先还没建表,在测试save方法时能让hibernate帮我自动生成数据库表吗?@Entity
public class Category {
private int id;
private String name;

@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}下面是save的测试方法,通过Service层的方法     @Test
public void testAdd() {
Category category = new Category();
category.setName("c1");
categoryService.add(category);
}连接数据库都是交给Spring容器来管理。在测试以上方法时不能自动生成数据表?
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 't_bbs.category' doesn't exist

解决方案 »

  1.   


    忘写<prop key="hibernate.hbm2ddl.auto">update</prop> 了。。
    现在又来异常了org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
      

  2.   

    不能,hibernate是O-R映射框架,不是数据库建模工具
      

  3.   

    把你的save()方法(在Service中找)放到session.beginTrasaction和commit之间了么?
      

  4.   

    能在sava之前就能生成一个表 但是没有数据
      

  5.   

    最好不要这样,应该先写配置文件(.hbm.xml)。然后生成表,最后再save.