1.检查User类,发现注解的配置都没有错
//实体User映射到表T_USER @Entity @Table(name="T_USER") public class User { @Id //数据库中的主键生成器,值为uuid @GeneratedValue(generator = "system-uuid") @GenericGenerator(name = "system-uuid", strategy = "uuid") @Column(length=32) private String id;//id @Column(length=32) private String userName;//用户名 @Column(length=32) private String age;//年龄 public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; }2.检查sessionfactory,映射路径写错
<!--配置数据库会话工厂--> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 引用数据源 --> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <!-- hibernate属性配置 --> <props> <!-- 指定一种方言, 允许Hibernate针对特定的关系数据库生成优化的SQL --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!--hibernate.hbm2ddl.auto参数的作用主要用于:自动创建|更新|验证数据库表结构 --> <!-- 在SessionFactory创建时,自动检查数据库结构,或者将数据库schema的DDL导出到数据库. 使用 create-drop时,在显式关闭SessionFactory时,将drop掉数据库schema. 取值 validate | update | create | create-drop --> <!-- update:第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库), 以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。 要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。--> <prop key="hibernate.hbm2ddl.auto">update</prop> <!--输出所有SQL语句到控制台 --> <prop key="hibernate.show_sql">true</prop> <!--在log和console中打印出更漂亮的SQL。 取值 true | fals--> <prop key="hiberante.format_sql">true</prop> </props> </property> <property name="configLocations"> <list> <value> <!--配置映射的类--> classpath*:config/hibernateBean/hibernate.cfg.bean.xml </value> </list> </property> </bean>解决方法 将映射路径改正
