Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

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();
        }

From http://www.peterfriese.de/

It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}