有两个问题:
[b][color=red][size=large]1、spring和hibernate中的一对多问题(包括添加、删除和更行)~我现在这么写代码规范吗?
2、总感觉项目里加了spring之后,处理事务的速度变得慢了很多~比只是加入hibernate的时候慢~这是神马原因呢?[/size][/color][/b]
数据库有两个表:“user”和“order”;一个“user”对应多个“order”;user表的外键关联在“order”表中
具体的Dao借口都继承了泛型的Dao(泛型Dao里面有spring的数据操作)
现在我想在“order”里面添加数据~测试结果现在添加失败~但没有Exception;以下是我的代码,问题应该就在[color=red][size=large]“OrderServiceImpl”[/size][/color]中;
(Spring:ApplicationContext.xml)
[code="java"] 1. <!-- -->
-
-
- <!-- -->
- <!-- -->
-
-
-
-
-
- <!-- -->
- <!-- -->
-
-
-
-
-
-
-
-
-
-
- <!-- -->
- <!-- -->
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <!-- --> [/code]
-
User.hbm.xml
[code="java"] 1.
-
-
-
-
-
-
-
-
-
- [/code]
Order.hbm.xml
[code="java"] 1.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [/code]
OrderDaoImpl(向数据库底层添加数据)
[code="java"]
public class OrderDaoImpl extends GenericHibernateDaoImpl implements OrderDao {
boolean success =false;
public boolean addOrder(Order order) {
success=saveObj(order);
System.out.println("save order");
return success;
}
[/code]
[b][color=red]OrderServiceImpl(这层和用户沟通~问题出现在这代码当中:第16~19行)[/color][/b]
[code="java"] 1. public boolean addOrder(String orderName, Integer orderStatue,
- String userName,Integer version) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- Order order = (Order)ctx.getBean("Order");
- userService = (UserService) ctx.getBean("UserService");
- orderDao=(OrderDao)ctx.getBean("OrderDao");
- boolean success = false;
-
- Integer userId = userService.findUserId(userName);//get a UserID by userName
- User user = userService.findUser(userId);//get an User by UserID
- order.setOrderName(orderName);
- order.setOrderStatue(orderStatue);
- order.setVersion(version);
- order.setUser(user);//set an "user" into the order;
- try {
- success=orderDao.addOrder(order);//保存“order”结果提示返回的“success”值为“false”,应该怎么办好呢?
- } catch (Exception e) {
- success = false;
- e.printStackTrace();
- }
- return success;
- } [/code]
- Integer userId = userService.findUserId(userName);//get a UserID by userName
如果单单用sql语句是可以插入的~但是我想用hibernate的特性把参数值set进去~thanks