调试train网站的bug汇总

来源:互联网 发布:自动套料软件 编辑:程序博客网 时间:2024/05/16 13:45

问题描述:查询【列车车次】的时候就报错了

  一:测试代码如下:

      @Testpublic void test() {Resource res =new FileSystemResource("WebRoot/WEB-INF/applicationContext.xml");BeanFactory factory = new XmlBeanFactory(res);TrainDAO trainDAO=(TrainDAO) factory.getBean("TrainDAO");Train train=(Train) trainDAO.findByProperty("number","4718").get(0);if(train!=null)System.out.println("data can be found");else    System.out.println("data can't be found");

二:在hibernatesql语句中:

 Hibernate:     select        train0_.id as id2_,        train0_.num as num2_,        train0_.startstation as startsta3_2_,        train0_.starttime as starttime2_,        train0_.entirejourney as entirejo5_2_,        train0_.entiretime as entiretime2_,        train0_.arrivestation as arrivest7_2_,        train0_.arrivetime as arrivetime2_,        train0_.hardprice as hardprice2_,        train0_.softabove as softabove2_,        train0_.softbelow as softbelow2_,        train0_.hardabove as hardabove2_,        train0_.hardmiddle as hardmiddle2_,        train0_.hardbelow as hardbelow2_     from        train train0_     where        train0_.num=?Hibernate:     select        tickets0_.trainid as trainid1_,        tickets0_.id as id1_,        tickets0_.id as id3_0_,        tickets0_.trainid as trainid3_0_,        tickets0_.hard as hard3_0_,        tickets0_.hardsleeper as hardslee4_3_0_,        tickets0_.soft as soft3_0_,        tickets0_.softsleeper as softslee6_3_0_,        tickets0_.noseat as noseat3_0_,        tickets0_.ticketdate as ticketdate3_0_     from        ticket tickets0_     where        tickets0_.trainid=?     order by        tickets0_."date"

三:对应的提示信息如下

 org.springframework.dao.InvalidDataAccessResourceUsageException: could not initialize a collection: [po.Train.tickets#1]; nested exception is org.hibernate.exception.SQLGrammarException: could not initialize a collection: [po.Train.tickets#1]at po.TrainDAO.findByProperty(TrainDAO.java:53)at test.ddd.test(ddd.java:26)Caused by: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [po.Train.tickets#1]Caused by: java.sql.SQLException: ORA-00904: "TICKETS0_"."date": 标识符无效

四:一些进行尝试的解决方案:

(1)Caused by: java.sql.SQLException: ORA-00904: "TICKETS0_"."date": 标识符无效

 太感谢了,和你犯了同样的错误,郁闷的来搜索了一把,看到你的提醒赶紧检查程序,果然,把表名字给写错了--------------怀疑表名写错了--------

我检查了一下我的hibernate输出语句发现了问题的关键,,,为什么会有  order by

        tickets0_."date",,,在我的数据库中,date字段变成了ticketdate,这样子的话,怎么可能读到数据呢???

(2)junit test 中使用debug的功能,其实在理论上面这里属于单元测试的内容!!

sql "select tickets0_.trainid as trainid1_, tickets0_.id as id1_, tickets0_.id as id3_0_, tickets0_.trainid as trainid3_0_, tickets0_.hard as hard3_0_, tickets0_.hardsleeper as hardslee4_3_0_, tickets0_.soft as soft3_0_, tickets0_.softsleeper as softslee6_3_0_, tickets0_.noseat as noseat3_0_, tickets0_.ticketdate as ticketdate3_0_ from ticket tickets0_ where tickets0_.trainid=? order by tickets0_."date"" (id=98)

count 401

hash 0

offset 0

value  (id=100)

1:通过实验,知道了一点:在向里面传参数的时候,其实传递的是:类的成员变量,而不是数据库中的字段名称。。。体会到了分层的好处!!!

2:所报的错误,在java语句中是不可以得到体现了,这个时候我需要去查找配置文件了。

<set name="passes" inverse="true" lazy="false" order-by="`order`">    <key column="trainid" not-null="true" />    <one-to-many class="po.Pass" /></set><set inverse="true" name="tickets" lazy="false" order-by="`date`">    <key column="trainid"/>    <one-to-many class="po.Ticket"/></set>

要看书了,自己对于one-to-many这个逻辑的确有些不懂的地方。

注意这里取名字还是有讲究的,name="tickets",这样子的话,

3:改变order-by 中的内容之后发现了 could not initialize a collection: [po.Train.tickets#1]

哈哈终于被我找到了错误。。。

/train/src/po/Train.hbm.xml<set inverse="true" name="tickets" lazy="false" order-by="`ticketdate`">    <key column="trainid"/>    <one-to-many class="po.Ticket"/></set>/train/src/po/Ticket.hbm.xml<id name="id" type="java.lang.Integer">            <column name="id" />            <generator class="native" />        </id>

找到这里突然有种发现新大陆的赶脚,觉得bug终于被找到的希望!!!

你妹的,还是报相同的错误

sql "select tickets0_.id as id1_, tickets0_.id as id3_0_, tickets0_.trainid as trainid3_0_, tickets0_.hard as hard3_0_, tickets0_.hardsleeper as hardslee4_3_0_, tickets0_.soft as soft3_0_, tickets0_.softsleeper as softslee6_3_0_, tickets0_.noseat as noseat3_0_, tickets0_.ticketdate as ticketdate3_0_ from ticket tickets0_ where tickets0_.id=? order by tickets0_."ticketdate"" (id=98)

count 370

hash 0

offset 0

value  (id=108)

出现“你妹的”最大原因就是对于hibernate中的外键不熟悉,

结论:对于理论知识要掌握了5分左右再去指导实践,要不然的话,就不知道怎么去指导实践了。。。。

 

 

根据提示信息,以及查看hibernate语句发现了问题的纠结地方了,在其中的select语句中怎么出现了两次的id。。。

 

(1)当你怀疑某个地方出现了错误之后

   Check the docs of hibernate

   Try setting the log level to debug ,to get some more information on what is happening  在调试的时候去设置日志模式

 

 

I figure it has something to do with your configuration. You get aIllegalArgumentException with the message argument type mismatch. You probably have something defined in your mapping file which is of another type in your class. Make sure the types match.

哇!!好牛呀!!!其实自己对于错误提示信息还不是很会看,加油了!!!----------编程就好像一门艺术!!!如果没有同样的感觉,怎么去编程呢!!!在这里我要大声的说I LOVE CODING

 

(3) 经过再仔细的观察数据发现了问题

tickets0_."ticketdate"

“”好奇特的赶脚!!!

 

 

1:终于问题被解决了,有了一种完成了某一个重大的工程的感觉,知道了在配置文件中是有一定的规律的。当在xml文件中,property name:一般说的是在一个类中的属性名称;;

                    Column name :这个一般说的是数据库中字段的名称;

2:单引号和双引号的作用是一样的。。

3:其中的hql语句的查询。他的字段对应的是属性名称。。

 

 

原创粉丝点击