tags:

views:

50

answers:

2

I am very new to Hibernate. I have MySQL database and mapped pojos. What should I do next? I know little bit LINQ to SQL from .NET, and it generates me List of mapped objects.

So basically, what are my next steps after creating POJOS if I want to have List of them and do CRUD operations upon them and data will be also saved in DB not only in java objects ?

kthx

+2  A: 

please see the hibernate document - Chapter 10. Working with objects http://docs.jboss.org/hibernate/stable/core/reference/en/html/objectstate.html#objectstate-querying-executing

You can createQuery() or createCriteria() to get a list of your pojos. for example:

List cats = session.createQuery("from Cat").list();

or

List cats = session.createCriteria(Cat.class).list();
qrtt1
A: 

To answer your question about the rest of CRUD, once you've got your list of objects, as described by qrtt1, then you can manipulate the objects in the session:

Session session = // obtain session
Transaction tx = session.beginTransaction();

List cats = session.createQuery("from Cat").list();

Cat firstCat = (Cat)cats.get(0);
firstCat.setName("Cooking Fat");
firstCat.setOwner("Richard O'Sullivan");

// etc for other cats in the collection

tx.commit();
session.close();

Any objects that you obtained via the query are "dirty checked" at the tx.commit(); this means that in this case an update statement will be issued for the first cat retrieved from the query.

Dick Chesterwood