2013-04-22 84 views
0

我Hibernate.xml初学者:与Hibernate 4泉怀疑

<bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<tx:annotation-driven transaction-manager="transactionManager" /> 

我控制器

@RequestMapping("/") 
public String index() { 
    Category category = new Category(); 
    category.setName("Hello"); 
    categoryDao.saveCategory(category); 
    return "index"; 
} 

吾道

@Transactional 
public void saveCategory(Category category) { 
    Session session = sessionFactory.getCurrentSession(); 
    session.save(category); 
    Category category2 = (Category) session.get(Category.class, 
      new Integer(0)); 

    System.out.println("xxxx" + category2.getName()); 
} 

我跳过目前用于测试目的我的BO。否则我去哪里?我的输出应该是xxxxParent,它是id = 0处的类别名称,但是它显示从Controller传递的xxxxHello。否则插入工作正常。在尝试通过Hibernate访问数据库的方式中也存在任何错误。我的意思是使用交易注释或开幕式。

编辑

当我删除代码保存正确的输出显示新的类别。在检查控制台以前只有插入工作,但现在选择也工作。

+0

检查此链接哪个有非常好的解释和Spring和Hibernate集成http://www.javabeat.net/2007/10/integrating-spring-framework-with-hibernate-orm-framework/ – Krishna 2013-04-23 11:16:48

回答

1

我觉得代码

Category category = new Category(); 
category.setName("Hello");` 

正在创建ID为0的实体(整数默认为0,如果不申报),并更新在数据库中现有的实体。我没有看到你明确地在这里设置ID。请设置ID并查看。

+0

这太棒了... – 2013-04-22 13:55:17