昨天在工程中新引入2个entity(数据库中这2张表没有id字段),
今天早上发布tomcat程序直接报错:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.baqgl.dao.base.BaseDAO.setSessionFactory(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [D:\apache-tomcat-7.0.52-x64\webapps\BAQGL\WEB-INF\classes\spring-common.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.baqgl.entity.HistoryCollectInfo
错误说的很明白新加的表没有设置id,然后百度得出一下结论:
Hibernate 中必须要有一个主键,如果没有主见,Hibernate会把你所有的字段,和起来做为一个主键,
因为在Hibernate中没有两个一样的对象,那个在你的表中就必须要有一个来区分,---主键---
你可以把name 设置为主键,设置主键是可以输入的。
<id name="c_name" type="java.lang.String" column="c_name">
<generator class="assign" />
</id>
//
generator 表示ID生成策略
native 表示按照数据库中来设置(sql200-2005 是identity mysql auto increment )
assign 表示可以输入的
uuid 表示随机生成18的一个数做为ID
------------------------------------------------------------------------------------
不过我工程是使用注解的方式
添加了@Id就解决了
ps:hibernate这点挺让人不爽的,之前mybatis就不存在此问题,这张硬件设备采集存储信息的采集表,我也不可能去改硬件程序,只能在时间字段上加个@Id对付下了