Java编程:hibernate实体类属性第二个字母大写“Could not find a getter for rS” 的问题
来源:互联网 发布:seo能帮你赚到钱 编辑:程序博客网 时间:2024/06/04 19:40
项目中遇到的一个问题,本文描述问题并分析解决问题,如有不正之处,欢迎批评指正。
问题是这样的:
数据库字段定义为:r_s,通过工具生成对应的 Hibernate 实体类和配置文件。
实体类代码:
private Double rS;public void setRS(Double rs) { this.rs = rs;}public Double getRS() { return this.rs;}
<property name="rS" type="java.lang.Double"> <column name="r_s" length="10" /></property>
执行代码报错:Could not find a getter for “rS”。大约判定是属性 rS 与 getRS、setRS 不匹配的问题导致,于是把 getRS setRS 改为 getrS setrS,执行通过。问题是解决了,到底是什么原因导致的这个问题呢?
分析 Hibernate 代码,在 BasicPropertyAccessor.java 文件中存在如下代码:
String methodName = methods[i].getName(); // try "get" if( methodName.startsWith("get") ) { String testStdMethod = Introspector.decapitalize(methodName.substring(3)); String testOldMethod = methodName.substring(3); if( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) return methods[i]; }
有两种方法从 get 方法中截取属性名称:
- 直接截取 methodName.substring(3);
- 通过 Introspector.decapitalize(methodName.substring(3)) 截取
Sun 给出的 Introspector.decapitalize 的注释说明:
but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone.
大致意思:在特殊情况下,如果第一个、第二个都是大写字母,我们保持他的原态。这也就解释了上面的问题。getRS 中截取的属性为 RS,与实体类中的属性不一致,如果改为 getrS后,截取的属性为 rS,与实体类中的属相相匹配。
所以,在 Hibernate 的实体类中不要定义第一个、第二个都是大写字母的属性。
1 0
- Java编程:hibernate实体类属性第二个字母大写“Could not find a getter for rS” 的问题
- hibernate下两个相连大写字符导致Could not find a getter for问题
- org.hibernate.PropertyNotFoundException: Could not find a getter for XXX
- hibernate 报错:Could not find a getter for ~
- " org.hibernate.PropertyNotFoundException:Could not find a getter for ..
- org.hibernate.PropertyNotFoundException: Could not find a getter for XXX
- Hibernate: Could not find a getter for **** in class
- hibernate出现:org.hibernate.PropertyNotFoundException: Could not find a getter for id in class
- Hibernate错误 Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for pnsame i
- 在spring+hibernate框架的java项目开发过程中,经常会遇到这样的错误: could not find a getter for ... in class ...
- org.hibernate.PropertyNotFoundException: Could not find a getter for titlse in class com.po.Intro
- org.hibernate.PropertyNotFoundException: Could not find a getter for titlse in class com.po.Intro异常
- org.hibernate.PropertyNotFoundException: Could not find a getter for empDeptId in class
- org.hibernate.PropertyNotFoundException Could not find a getter for id in class
- 错误提示:Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for ID
- org.hibernate.PropertyNotFoundException: Could not find a getter for id in class com.pb.dao.UserDao
- org.hibernate.PropertyNotFoundException: Could not find a getter for sMarathon in class com.honsto.
- org.hibernate.PropertyNotFoundException: Could not find a getter
- 迭代器模式
- CSS实现三级下拉菜单分析
- Java后台书单
- Laplacian函数用法示例
- 测量各种数据类型的长度(包括指针),typedf关键字对数据类型的作用!
- Java编程:hibernate实体类属性第二个字母大写“Could not find a getter for rS” 的问题
- SSH远程连接至Linux主机
- Zynq通过TFTP进行启动
- window实现sublime的C++代码跳转
- Scharr函数用法示例
- vim+python+OpenCV学习八 : 调用摄像头
- 查找整数
- GMII,RGMII,SGMII,TBI,RTBI接口信号及时序介绍
- java命令执行打好的jar包,maven的jar包形式