用了这么久的Hibernate了,今天却遇到一个从未遇到的问题,幸好我思维敏捷,善于联想,才得以在短时间内发现并解决了问题。以下是具体描述。
我在HibernateDAO这个类里增加了一个方法如下:
public Object getById(Class clazz, String id) throws HibernateException{
return session.find("from "+clazz.getName()+" o where o.id=?",id,Hibernate.STRING).get(0);
}
你知道,我的PO类的主键都是名为oid的。凑巧的是,有一些PO类除了具有oid属性外,还具有名为id的属性,用来表示业务编号,例如教师编号、文化程度的编号等等。这些类在使用这个方法时总报下面的异常:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
郁闷了半个小时,终于想到,会不会是查询语句中的o.id,Hibernate认为这里的id
表示的是主键oid
呢?在debug里把参数按oid的值一改,发现果然如此!
解决方法:暂时还不知道有什么方法能起到转义的作用,不过id这个属性确实有点容易产生歧义,还是改名为code
吧。
搬家前链接:https://www.cnblogs.com/bjzhanghao/archive/2004/07/28/28064.html
Your article was a pleasure to read. You have a talent for writing.
Your article is inspiring and has motivated me to take action. Thank you for advocating for positive change.
Thanks for the new and interesting perspective in this article.
Your use of data and statistics to support your arguments adds credibility and depth to your writing.
Your article is a great example of how storytelling can be used to convey important messages. Thank you for sharing your story with us.
I think this article will be very useful for those who are interested in the topic.
Your writing has a way of inspiring and motivating me. Thank you for sharing your knowledge and experience with us.
I appreciate the way you use data and statistics to support your arguments. It adds credibility and depth to your writing.