SQL Zone is brought to you in partnership with:

Peter Friese is a software engineer with 15+ years hands-on experience in software development, technical writing and public speaking. Peter works as a software engineering consultant at Zühlke Engineering. Having worked on a host of industry projects in diverse domains and being an active committer on a number of open source projects, he has in-depth knowledge in a broad range of technologies. His main areas of expertise are model-driven software development, cross-platform mobile development (iPhone, Android, Windows Phone 7, and mobile web) and Eclipse tooling. Peter blogs at http://www.peterfriese.de and tweets at @peterfriese. Peter is a DZone MVB and is not an employee of DZone and has posted 29 posts at DZone. View Full User Profile

Using Teneo, EMF and Hibernate to Update and Query Your Data

  • submit to reddit

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();
  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);
  4. Updating the page count is pretty obvious:

  5. Finally, don't forget to commit the transaction and close the session:

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();
  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");
  4. Finally, commit the transaction and close the session:

From http://www.peterfriese.de/

Published at DZone with permission of Peter Friese, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Douglas Ferguson replied on Fri, 2011/07/22 - 3:52pm

Hi, I'm curious if Teneo can be used with custom hibernate annotations? Currently we have EMF models for the front end and Hibernate pojos for backend. The pojos use on trivial annotations like mappedBy, JoinTable, Field/FeildBridge for hibernate search. Thanks...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.