【Java】Hibernate&Spring:使用jpa注解形式时applicationContext配置的方法

记录。

在applicationContext.xml直接配置hibernate信息。不使用*.hbm.xml和hibernate.cfg.xml。

具体方法说明附上原文链接:https://blog.csdn.net/myspacedemen/article/details/38397589

但注意hibernate版本是4以后会出现异常信息。

 

使用版本hibernate5+,applicationContext.xml中,部分简化配置如下:

	

		

		
			
				com.xxx.model
			
		

		
			
				org.hibernate.dialect.MySQLDialect
				true
				true
				update
			
		

	

 

  • 异常信息:

java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;

...

Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition

 

  • 解决方案

因为使用的版本是hibernate5+,4及以上不再有AnnotationSessionFactoryBean支持,所以

将:         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

改为:

        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

注意,这里的hibernatex需要根据你所使用的版本决定

    
	
	

		

		
			
				com.xxx.model
			
		

		
			
				org.hibernate.dialect.MySQLDialect
				true
				true
				update
			
		

	

 

实体类:

注意:这里的@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();

	}
}

 

你可能感兴趣的:(mysql,java)