Web Service中数据库查询问题,急等!!
有两个表
CREATE TABLE `country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`home` int(11) DEFAULT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `home` (`home`),
CONSTRAINT `user_ibfk_2` FOREIGN KEY (`home`) REFERENCES `country` (`id`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
这两个表完极其简单,说白了就是user表中有一个home外键指向country表的主键。user表和country表各有一个name属性。
user表和country表是多对一的关系。
建立一个简单的UseService进行User和Country的查询操作。
public class UserService implements IUserService
{
private IUserDAO userDAO;
private ICountryDAO countryDAO;
public User findUserById(Integer id)
{
return userDAO.findById(id);
}
public Country findCountryById(Integer id)
{
return countryDAO.findById(id);
}
.....省略get(), set()
}
数据库采用Hibernate,通过Spring来配置。
建立一个简单测试程序,直接测试
public static void main(String[] args)
{
ApplicationContext ctx = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
IUserService userService = (IUserService) ctx.getBean("userServiceImpl");
User u= userService.findUserById(5);
System.out.println(u.getName());
Country c=userService.findCountryById(1);
System.out.println(c.getName());
}
测试没问题,可以通过。
使用xFile+Spring配置Web Service服务,没问题,可以查看到相应的wsdl。
现在问题来了:
通过Web Service调用findCountryById(Integer id)时能正确执行,并得到数据。
通过Web Service调用findUserById(Integer id)时出现如下错误:
- <soap:Body>
- <soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Fault: java.lang.NullPointerException</faultstring>
</soap:Fault>
</soap:Body>
在测试程序中User u= userService.findUserById(5);输出sql语句如下:
Hibernate: select user0_.id as id0_0_, user0_.home as home0_0_, user0_.name as name0_0_ from smservice.user user0_ where user0_.id=?
Hibernate: select country0_.id as id1_0_, country0_.name as name1_0_ from smservice.country country0_ where country0_.id=?
通过Web Service调用findUserById(5)时输出sql语句如下:
Hibernate: select user0_.id as id0_0_, user0_.home as home0_0_, user0_.name as name0_0_ from smservice.user user0_ where user0_.id=?
不知道为什么会发生这个错误!!
经过我的多次试验,只要是表中有外键,那么查询时就会出现这个Fault: java.lang.NullPointerException错误!!
恳请各位高人解答!!拜谢!!!!!!