【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中,部分简化配置如下:

	<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();

	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值