views:

59

answers:

2

hi, I have simple spring mvc web application which can handle simple book store. I have a problem when the existing book is updating.

I want to up date title of a book. My updateBook.jsp is somethig like this.

<form method="post" action="">
    Previous book title : <input type="text" name="previousTitle" /> <br>
    New book title :<input type="text" name="newTitle"/><br>
    <input type="submit" name="update" value="update"/>
</form>

Problem: I have java class,"UpdateBookController" for handle the update of book. How can I handle the previous and new book titles in "UpdateBookController" class.

any idea..?

Thank in advance!

A: 

How about something like this:

Session sess = null;
try {
  SessionFactory fact = new Configuration().configure().buildSessionFactory();
  sess = fact.openSession();
  Transaction tr = sess.beginTransaction();
  Book book = (Book)sess.get(Book.class, new Long(<id of the book to update>));
  book.setTitle(<new title>);
  sess.update(book);
  tr.commit();
  sess.close();
  System.out.println("Update successfully!");
}
catch(Exception e){
  System.out.println(e.getMessage());
}

However, you may want to use the DAO pattern to handle this in a neat way.

Jeroen Rosenberg
The whole point of using Spring is that you can skip all this boilerplate code. See here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-hibernate-straight
seanizer
Yes I know that, but originally there wasn't much specified in the question so I just gave a very basic overview of the steps involved to do an update. Of course you can use a transaction manager and stuff, but that's not the point of the question I believe...
Jeroen Rosenberg
+1  A: 

Spring 3.0 way

@Controller
public class BookController {

    @Autowired
    private Repository<Book, Integer> bookRepository;

    @RequestMapping(method=RequestMethod.POST)
    public void updateTitle(@RequestParam("bookId") Integer bookId, 
                            @RequestParam("previousTitle") String previousTitle,
                            @RequestParam("newTitle") String newTitle) {

        Book book = bookRepository.findById(bookId);
        book.setPreviousTitle(previousTitle);
        book.setNewTitle(newTitle);

        bookRepository.merge(book);
    }

}

Where bookRepository can be written as

@Repository
public class BookRepository extends AbstractRepository<Book, Integer> {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void merge(Book book) {
        sessionFactory.getCurrentSession().update(book);
    }

}

If you want, you can create a custom UpdateTitleUseCase command class which encapsulates previousTitle and newTitle attributes. This way, your controller looks like

@RequestMapping(method=RequestMethod.POST)
public void updateTitle(UpdateTitleUseCase command) {
    Book book = bookRepository.findById(command.getBookId());
    book.setPreviousTitle(command.getPreviousTitle());
    book.setNewTitle(command.getNewTitle());

    bookRepository.merge(book);
}
Arthur Ronald F D Garcia
+1 for the Spring way
Pascal Thivent
@Pascal Thivent Thank you, Pascal. I am looking for your answers about unit-testing, testing and so on...
Arthur Ronald F D Garcia
@Arthur You'll find some answers but I didn't really post extensively on this topic (how went your talk by the way?). But my current toys include: [Arquillian](http://jboss.org/arquillian) (for integration or functional testing of Java EE 6), [Concordion](http://www.concordion.org/) for BDD.
Pascal Thivent
@Pascal Thivent Thank your for the links
Arthur Ronald F D Garcia