记录。
在applicationContext.xml直接配置hibernate信息。不使用*.hbm.xml和hibernate.cfg.xml。
具体方法说明附上原文链接:https://blog.csdn.net/myspacedemen/article/details/38397589
但注意hibernate版本是4以后会出现异常信息。
使用版本hibernate5+,applicationContext.xml中,部分简化配置如下:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan">
<list>
<value>com.xxx.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
- 异常信息:
java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
...
Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition
- 解决方案
因为使用的版本是hibernate5+,4及以上不再有AnnotationSessionFactoryBean支持,所以
将:<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
改为:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
注意,这里的hibernatex需要根据你所使用的版本决定
<!-- 将SessionFactory配置到spring容器中 -->
<!-- 加载配置的方案:
方案一:仍然使用外部的hibernate.cfg.xml配置信息;
方案二:在spring配置中放置hibernate配置信息。
-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan">
<list>
<value>com.xxx.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
实体类:
注意:这里的@Entity应该导入的包是:javax.persistence.*
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="t_user")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private String id;
private int age;
private String name;
private String password;
private double salary;
public User() {
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public double getSalary() {
return this.salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
测试类:
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.zqchow.model.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo {
@Resource
private SessionFactory sessionFactory;
@Test
public void hibernateTest() {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
User user = new User();
user.setName("小二");
user.setAge(24);
user.setPassword("333333");
user.setSalary(3.233D);
session.save(user);
transaction.commit();
session.close();
sessionFactory.close();
}
}