1

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 Answers 2

3

please see the hibernate document - Chapter 10. Working with objects http://docs.jboss.org/hibernate/core/3.3/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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for report, I fixed it.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.