以“Copa”实体类为例,Copa类中有id属性
一:在hibernate配置文件中要加入对应实体类
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.copa.model.Copa</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
二:hql写法注意
Query query = this.getSession().createQuery("FROM Copa WHERE id=:id");
hql是全orm映射,“from Copa”是要写成“from 实体类名”
“where id=:id” 里面的字段必须与实体类里的属性(字段名)完全一致
并不是原生SQL的写法,因为实体类已经用各种注解与数据表映射起来了~