Hibernate无法使用受保护的默认构造函数 [英] Hibernate failed on using protected default constructor

查看:55
本文介绍了Hibernate无法使用受保护的默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期望Hibernate能够使用受保护的无参数构造函数实例化类,但是,我们得到: org.hibernate.HibernateException:无法实例化resultclass:com.xxx.Installation ./p>

从以下位置更改后:

  @VisibleForHibernate受保护的分期付款(){} 

收件人:

  @VisibleForHibernate公共分期付款(){} 

该问题已解决.我们使用的Hibernate版本:3.6.10.Final.

关于无参数构造函数何时必须为 public 而不是受 protected 保护的Hibernate,是否有任何规定?

解决方案

从参考文献

https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/persistent-classes.html#persistent-classes-pojo-constructor

所有持久类都必须具有默认构造函数(可以是非公共的),以便Hibernate可以使用Constructor.newInstance()实例化它们.建议您使用默认构造函数,该构造函数至少具有程序包可见性,以便在Hibernate中生成运行时代理.

自从Hibernate用户反思以来,Hibernate要求构造函数应该是公共的,或者应该具有程序包级别的可见性.

I have expected Hibernate to be able to instantiate class using protected no-args constructor, however, we got: org.hibernate.HibernateException: Could not instantiate resultclass: com.xxx.Installment.

After changing from:

@VisibleForHibernate
protected Installment() {
}

to:

@VisibleForHibernate
public Installment() {
}

the problem has been fixed. Hibernate version we use: 3.6.10.Final.

Are there any rules as to when the no-args constructor must be public as opposed to protected in order for Hibernate to work?

解决方案

From the reference

https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/persistent-classes.html#persistent-classes-pojo-constructor

All persistent classes must have a default constructor (which can be non-public) so that Hibernate can instantiate them using Constructor.newInstance(). It is recommended that you have a default constructor with at least package visibility for runtime proxy generation in Hibernate.

Since Hibernate user reflection, Hibernate requires that constructor should be public or it should have package level visibility.

这篇关于Hibernate无法使用受保护的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆