Reputation: 3975
My Hibernate.xml
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
My controller
@RequestMapping("/")
public String index() {
Category category = new Category();
category.setName("Hello");
categoryDao.saveCategory(category);
return "index";
}
My DAO
@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());
}
I skipped my BO currently for testing purpose. Otherwise am I going somewhere? My output should have been xxxxParent which is the name of category at id = 0 however its showing xxxxHello which is being passed from Controller. Otherwise insertion is working properly. Also is there any mistake in the way am trying to access DB via Hibernate. I mean using transactional annotation or opening session.
EDIT
When I deleted the codes for saving new category the correct output is shown. On checking the console previously only insert was working but now select is also working.
Upvotes: 0
Views: 142
Reputation: 5749
I feel the code
Category category = new Category();
category.setName("Hello");`
is creating an entity with id 0 (integer defaults to 0 if not declared) and is updating the existing entity in the DB. I don't see you explicitly setting the ID here. Please set the id and see.
Upvotes: 1