Skip to content

March 14, 2009

1

Using Teneo, EMF and Hibernate to update and query your data

Last week I showed you how to use Teneo, EMF and Hibernate to store your data in a database.

This week, we're going to have a look at how to update the data, add more records and how to query the database for information using HQL, the Hibernate Query Language.

If you haven't done so, you might consider taking last week's tutorial in order to get your environment set up and understand the basic concepts. If you're lazy, you can as well download the code of the tutorial to get started more quickly. It is available here.

As it turns out, in order to change anything in the library database, we must first fetch the to-be-changed data, so we need to have a look at querying first.

Retrieving and updating data
Let's assume we'd want to the update the number of pages in a book, because the new edition has an additional chapter.

  1. So, first of all we need to open a session and begin a transaction:
        {
            Session session = sessionFactory.openSession();
            session.beginTransaction();
  2. Next, let's create an HQL query. We want to find any books that are written by an author by the name "A. K. Dewdney" that contain "Omnibus" in their title.
            Query query = session.createQuery(
                "SELECT book from " +
                "    Book book, " +
                "    Writer writer " +
                "WHERE " +
                "    book.title like '%Turing Omnibus%' " +
                "AND " +
                "    writer.name = 'A. K. Dewdney'");
  3. We can now execute the query and display the results:
            List books = query.list();
            Book book = books.get(0);
            System.out.println(book.getTitle());
  4. Updating the page count is pretty obvious:
            book.setPages(520);
  5. Finally, don't forget to commit the transaction and close the session:
            session.getTransaction().commit();
            session.close();
        }

Adding data
Let's now assume we want to add more books (and authors) to the library.

  1. By now, you should be pretty familiar with the pattern of opening a new session:
        {
            Session session = sessionFactory.openSession();
            session.beginTransaction();
  2. As we want to add new items to the library, we need to retrieve the library instance first of all:
            Query query = session.createQuery("from Library");
            List libraries = query.list();
            Library library = libraries.get(0);
  3. Creating a new book and its author is easy, as we just have to use the API EMF so kindly generated for us:
            Writer writer = LibraryFactory.eINSTANCE.createWriter();
            writer.setName("J.R.R. Tolkien");
            Book book = LibraryFactory.eINSTANCE.createBook();
            book.setTitle("The Hobbit");
            book.setPages(320);
            book.setAuthor(writer);
            book.setCategory(BookCategory.MYSTERY);
            library.getBooks().add(book);
            library.getWriters().add(writer);
  4. Finally, commit the transaction and close the session:
            session.getTransaction().commit();
            session.close();
        }

Thanks for reading this post. Follow me on twitter here to be notified about updates and other posts I write. Or, subscribe to my RSS feed here

Read more from EMF, Eclipse, Eclipse FAQ, MDSD
1 Comment Post a comment
  1. Matthias Treitler
    Mar 16 2009

    I think the great benefit of Teneo is the support of the EMF resource API through the HibernateResource implementation. With this feature you can store your models in database or XML/XMI or any other EMF-XyzResource with (nearly) no code changes. Thats really fantastic.

    Reply

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

Additional comments powered by BackType