class must be instantiated by Interceptor

在Hibernate工程中出现:

INFO: HHH000182: No default (no-argument) constructor for class: com.yangwan.entity.Person (class must be instantiated by Interceptor)

Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:697)
    at com.yangwan.test.MainTest.checkInformation(MainTest.java:73)

    at com.yangwan.test.MainTest.main(MainTest.java:89)


解决方法:

    Hibernate配置文件中,数据库和实体类之间属性映射通过实现类的getter&setter方法。如果实体类重载了带参数的构造函数,一定要添加不带参数的构造函数。当没有重载带参数的构造函数的时候,可以不写不带参数的构造函数,因为JVM在编译的时候会自动为实体类添加默认的不带参数的构造函数。但是为了避免这样错误的发生。建议在实现类的时候添加不带参数的构造函数。

package com.yangwan.entity;

public class Person {
	private int id;
	private String personName;
	private double score;
	//必须实现不带参数的默认构造函数,不然无法完成hibernate数据映射
	public Person() {

	}
	//重载了带参数的构造函数
	public Person(String personName, double score) {
		this.personName = personName;
		this.score = score;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getPersonName() {
		return personName;
	}

	public void setPersonName(String personName) {
		this.personName = personName;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值