1. Trang chủ
  2. » Công Nghệ Thông Tin

Beginning Hibernate From Novice to Professional phần 3 pptx

35 320 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 35
Dung lượng 400,58 KB

Nội dung

i mport org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; i mport org.hibernate.cfg.Configuration; public class DAO { protected DAO() { } public static Session getSession() { Session session = (Session) DAO.session.get(); if (session == null) { session = sessionFactory.openSession(); DAO.session.set(session); } return session; } protected void begin() { getSession().beginTransaction(); } protected void commit() { getSession().getTransaction().commit(); } protected void rollback() { try { getSession().getTransaction().rollback(); } catch( HibernateException e ) { log.log(Level.WARNING,"Cannot rollback",e); } try { getSession().close(); } catch( HibernateException e ) { log.log(Level.WARNING,"Cannot close",e); } DAO.session.set(null); } public static void close() { getSession().close(); DAO.session.set(null); } CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION 49 6935ch03_final.qxd 8/2/06 9:36 PM Page 49 p rivate static final Logger log = Logger.getAnonymousLogger(); private static final ThreadLocal session = new ThreadLocal(); private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); } Using the Session The most common use cases for our POJOs will be to create them and delete them. In both cases, we want the change to be reflected in the database. For example, we want to be able to create a user, specifying the username and password, and have this information stored in the database when we are done. The logic to create a user (and reflect this in the database) is incredibly simple, as shown in Listing 3-16. Listing 3-16. Creating a User Object and Reflecting This in the Database try { begin(); User user = new User(username,password); getSession().save(user); commit(); return user; } catch( HibernateException e ) { rollback(); throw new AdException("Could not create user " + username,e); } We begin a transaction, create the new User object, ask the session to save the object, and then commit the transaction. If a problem is encountered (if, for example, a User entity with that username has already been created in the database), then a Hibernate exception will be thrown, and the entire transaction will be rolled back. T o retrieve the User object from the database, we will make our first excursion into HQL. HQL is somewhat similar to SQL, but you should bear in mind that it refers to the names used in the mapping files, rather than the table names and columns of the underlying database. The appropr iate HQL quer y to r etrieve the users having a given name field is as follows: from User where name= :username where User is the class name and :username is the HQL named parameter that our code will populate when we carr y out the query. This is remarkably similar to the SQL for a prepared statement to achieve the same end: select * from user where name = ? The complete code to retrieve a user for a specific username is shown in Listing 3-17. CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION50 6935ch03_final.qxd 8/2/06 9:36 PM Page 50 L isting 3-17. R etrieving a U ser O bject from the Database t ry { begin(); Query q = getSession().createQuery("from User where name = :username"); q .setString("username",username); User user = (User)q.uniqueResult(); commit(); return user; } catch( HibernateException e ) { rollback(); throw new AdException("Could not get user " + username,e); } We begin a transaction, create a Query object (similar in purpose to PreparedStatement in connected applications), populate the parameter of the query with the appropriate username, and then list the results of the query. We extract the user (if one has been retrieved success- fully) and commit the transaction. If there is a problem reading the data, the transaction will be rolled back. The key line used to obtain the User entity is: User user = (User)q.uniqueResult(); We use the uniqueResult()method because it is guaranteed to throw an exception if some- how our query identifies more than one User object for the given username. In principle, this could happen if the underlying database’s constraints don’t match our mapping constraint for a unique username field, and an exception is an appropriate way to handle the failure. The logic to delete a user from the database (Listing 3-18) is even more trivial than that required to create one. Listing 3-18. Deleting a User Object and Reflecting This in the Database try { begin(); getSession().delete(user); commit(); } catch( HibernateException e ) { rollback(); throw new AdException("Could not delete user " + user.getName(),e); } We simply instruct the session to delete the User object from the database, and commit the transaction. The transaction will roll back if there is a problem—for example, if the user has alr eady been deleted. You have now seen all the basic operations that we want to perform on our data, so we will now take a look at the architecture we are going to use to do this. CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION 51 6935ch03_final.qxd 8/2/06 9:36 PM Page 51 Building DAOs The DAO pattern is well known to most developers. The idea is to separate out the POJOs from the logic used to persist them into, and retrieve them from, the database. The specifics of the implementation vary—at one extreme, they can be provided as interfaces instantiated from a factory class, allowing a completely pluggable database layer. For our example, we have selected a compromise of concrete DAO classes. Each DAO class represents the operations that can be performed on a POJO type. We have already described the base class DAO in Listing 3-15, and the preceding examples made use of this. To help encapsulate the specifics of the database operations that are being carried out, we catch any HibernateException that is thrown and wrap it in a business AdException instance, as sho wn in Listing 3-19. Listing 3-19. The AdException Class for the Example package sample; public class AdException extends Exception { public AdException(String message) { super(message); } public AdException(String message, Throwable cause) { super(message,cause); } } The UserDAO provides all the methods required to retrieve an existing User object, delete an existing User object, or create a new User object (see Listing 3-20). Changes to the object in question will be persisted to the database at the end of the transaction. Listing 3-20. The UserDAO Class for the Example package sample.dao; import org.hibernate.HibernateException; import org.hibernate.Query; import sample.AdException; import sample.entity.User; public class UserDAO extends DAO { public UserDAO() { } CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION52 6935ch03_final.qxd 8/2/06 9:36 PM Page 52 p ublic User get(String username) throws AdException { t ry { begin(); Query q = getSession().createQuery("from User where name = :username"); q.setString("username",username); User user = (User)q.uniqueResult(); commit(); return user; } catch( HibernateException e ) { rollback(); throw new AdException("Could not get user " + username,e); } } public User create(String username,String password) throws AdException { try { begin(); User user = new User(username,password); getSession().save(user); commit(); return user; } catch( HibernateException e ) { rollback(); throw new AdException("Could not create user " + username,e); } } public void delete(User user) throws AdException { try { begin(); getSession().delete(user); commit(); } catch( HibernateException e ) { rollback(); throw new AdException("Could not delete user " + user.getName(),e); } } } CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION 53 6935ch03_final.qxd 8/2/06 9:36 PM Page 53 C ategoryDAO p rovides all the methods required to retrieve all of the C ategory o bjects, delete an existing Category object, or create a new Category object (see Listing 3-21). Changes to the object in question will be persisted to the database at the end of the transaction. Listing 3-21. The CategoryDAO Class for the Example package sample.dao; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import sample.AdException; import sample.entity.Category; public class CategoryDAO extends DAO { public Category get(String title) throws AdException { try { begin(); Query q = getSession().createQuery( "from Category where title = :title"); q.setString("title", title); Category category = (Category) q.uniqueResult(); commit(); return category; } catch (HibernateException e) { rollback(); throw new AdException("Could not obtain the named category " + title, e); } } public List list() throws AdException { try { begin(); Query q = getSession().createQuery("from Category"); List list = q.list(); commit(); return list; } catch (HibernateException e) { rollback(); throw new AdException("Could not list the categories", e); } } CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION54 6935ch03_final.qxd 8/2/06 9:36 PM Page 54 p ublic Category create(String title) throws AdException { try { begin(); C ategory cat = new Category(title); getSession().save(cat); commit(); return null; } catch (HibernateException e) { rollback(); throw new AdException("Could not create the category", e); } } public void save(Category category) throws AdException { try { begin(); getSession().update(category); commit(); } catch (HibernateException e) { rollback(); throw new AdException("Could not save the category", e); } } public void delete(Category category) throws AdException { try { begin(); getSession().delete(category); commit(); } catch (HibernateException e) { rollback(); throw new AdException("Could not delete the category", e); } } } AdvertDAO pr ovides all the methods required to delete an existing Advert object or cr eate a new Advert object (adverts are always retrieved by selecting them from a category, and are thus indirectly loaded by the CategoryDAO class). Changes to the object in question will be per- sisted to the database at the end of the tr ansaction (see Listing 3-22). Listing 3-22. The AdvertDAO Class for the E xample package sample.dao; import org.hibernate.HibernateException; CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION 55 6935ch03_final.qxd 8/2/06 9:36 PM Page 55 i mport sample.AdException; import sample.entity.Advert; import sample.entity.User; public class AdvertDAO extends DAO { public Advert create(String title, String message, User user) throws AdException { try { begin(); Advert advert = new Advert(title, message, user); getSession().save(advert); commit(); return advert; } catch (HibernateException e) { rollback(); throw new AdException("Could not create advert", e); } } public void delete(Advert advert) throws AdException { try { begin(); getSession().delete(advert); commit(); } catch (HibernateException e) { rollback(); throw new AdException("Could not delete advert", e); } } } If you compare the amount of code required to create our DAO classes here with the amount of code that would be required to implement them using the usual JDBC approach, you will see that Hibernate’s logic is admirably compact. The Example Client Listing 3-23 shows the example code tying this together . Of course, this isn’t a full application, but you now have all the DAOs necessary to manage the advertisement database. This exam- ple gives a flavor of how they can be used. The code should be r un with the tasks in the Ant script delivered in Listing 3-1. After run- ning the exportDDL task to create the empty database, you should run the createUsers and createCategories tasks to provide initial users and categories, and then the postAdverts task to place adv er tisements in the database . Finally, run the listAdverts task to display the sav ed data. The code invoking the DAOs to perform the tasks in question is shown in Listing 3-23. CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION56 6935ch03_final.qxd 8/2/06 9:36 PM Page 56 Listing 3-23. The Class to Create the Example Users p ackage sample; import sample.dao.DAO; i mport sample.dao.UserDAO; public class CreateUser { public static void main(String[] args) { if (args.length != 2) { System.out.println("params required: username, password"); return; } String username = args[0]; String password = args[1]; try { UserDAO userDao = new UserDAO(); System.out.println("Creating user " + username); userDao.create(username, password); System.out.println("Created user"); DAO.close(); } catch (AdException e) { System.out.println(e.getMessage()); } } } The CreateUser class uses the UserDAO class to create and persist an appropriate User object. The specifics of the (two) users created are drawn from the command-line parameters provided in the createUsers Ant task. In Listing 3-24, we create Category objects via the CategoryDAO class—and again we draw the specific details fr om the command line pr o vided by the Ant script. Listing 3-24. The Class to Create the E xample Categories package sample; import sample.dao.CategoryDAO; import sample.dao.DAO; public class CreateCategory { public static void main(String[] args) { CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION 57 6935ch03_final.qxd 8/2/06 9:36 PM Page 57 i f (args.length != 1) { System.out.println("param required: categoryTitle"); return; } CategoryDAO categories = new CategoryDAO(); String title = args[0]; try { System.out.println("Creating category " + title); categories.create(title); System.out.println("Created category"); DAO.close(); } catch (AdException e) { System.out.println(e.getMessage()); } } } The code in Listing 3-25 allows us to create an advert for a preexisting user in a pre- existing category. Note our use of UserDAO and CategoryDAO to obtain User and Category objects from the database. As with the user and category, the advert details are supplied by the Ant task. Listing 3-25. The Class to Create the Example Adverts package sample; import sample.dao.AdvertDAO; import sample.dao.CategoryDAO; import sample.dao.DAO; import sample.dao.UserDAO; import sample.entity.Advert; import sample.entity.Category; import sample.entity.User; public class PostAdvert { public static void main(String[] args) { if (args.length != 4) { System.out.println("params required: username, categoryTitle, title, message"); return; } CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION58 6935ch03_final.qxd 8/2/06 9:36 PM Page 58 [...]... throws HibernateException Updating Entities Hibernate automatically persists into the database changes made to persistent objects If a property changes on a persistent object, the associated Hibernate session will queue the change for persistence to the database using SQL From a developer’s perspective, you do not have to do any work to store these changes, unless you would like to force Hibernate to commit... SessionFactory—a heavyweight object from each invocation of the JVM from the Ant java task, and is not a problem in “real” applications 6 935 ch 03_ final.qxd 8/2/06 9 :36 PM Page 61 CHAPTER 3 s BUILDING A SIMPLE APPLICATION Summary In this chapter, we’ve shown how to acquire the Hibernate tools, how to create and run the example from Chapter 1, and how to create a slightly larger application from scratch, driving the... illustrated in Figure 4-1 Hibernate does not manage transient objects or persist changes to transient objects Figure 4-1 Transient objects are independent of Hibernate To persist the changes to a transient object, you would have to ask the session to save the transient object to the database, at which point Hibernate assigns the object an identifier 63 6 935 ch04_final.qxd 64 8/2/06 9 :35 PM Page 64 CHAPTER... user twice? • Are you allowed to save a user without a name? • Are you allowed to save a user without a password? When you start to think about classes that refer to other classes, there are additional questions to consider Have a look at the Customer and Email classes defined in Listing 5 -3 Listing 5 -3 Customer and Email Classes public class Customer { int customerId; int customerReference; String name;... OVERVIEW OF MAPPING If Hibernate could not uniquely identify an object with a primary key, then the following code could have several possible outcomes in the underlying table String customer = getCustomerFromHibernate("dcminter"); customer.setAge(10); saveCustomerToHibernate(customer); For example, let’s say the table originally contained the data shown in Table 5 -3 Table 5 -3 Updating an Ambiguous... dcminter 30 dcminter 42 Which of the following should be contained in the resulting table? • A single row for the user dcminter, with the age set to 10 • Two rows for the user, with both ages set to 10 • Two rows for the user, with one age set to 10 and the other to 42 • Two rows for the user, with one age set to 10 and the other to 30 • Three rows for the user, with one age set to 10 and the others to 30 ... want to persist The next chapter covers these in detail, and discusses why they are required and what they can contain 77 6 935 ch04_final.qxd 8/2/06 9 :35 PM Page 78 6 935 ch05_final.qxd 8/2/06 9:49 PM CHAPTER Page 79 5 sss An Overview of Mapping T he purpose of Hibernate is to allow you to treat your database as if it stores Java objects However, databases in practice do not store objects—they store data... database table generation from the hbm2ddl Ant task All of the files described in this chapter and the others can be downloaded from the Apress web site (www.apress.com) In the next chapter, we will look at the architecture of Hibernate and the lifecycle of a Hibernate- based application 61 6 935 ch 03_ final.qxd 8/2/06 9 :36 PM Page 62 6 935 ch04_final.qxd 8/2/06 9 :35 PM CHAPTER Page 63 4 sss The Persistence... After adding Hibernate to your application, you do not need to change your existing Java object model to add persistence marker interfaces or any other type of hint for Hibernate Instead, Hibernate works with normal Java objects that your application creates with the new operator, or that other objects create For Hibernate s purposes, these can be drawn up into two categories: objects for which Hibernate. .. executed • AUTO: Hibernate manages the query flushing to guarantee that the data returned by a query is up -to- date • COMMIT: Hibernate flushes the session on transaction commits • NEVER: Your application needs to manage the session flushing with the flush() method Hibernate never flushes the session itself By default, Hibernate uses the AUTO flush mode Generally, you should use transaction boundaries to ensure . applications. CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION60 6 935 ch 03_ final.qxd 8/2/06 9 :36 PM Page 60 Summary In this chapter, we’ve shown how to acquire the Hibernate tools, how to create and run the example from. task to display the sav ed data. The code invoking the DAOs to perform the tasks in question is shown in Listing 3- 23. CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION56 6 935 ch 03_ final.qxd 8/2/06 9 :36 . we want to perform on our data, so we will now take a look at the architecture we are going to use to do this. CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION 51 6 935 ch 03_ final.qxd 8/2/06 9 :36 PM Page

Ngày đăng: 09/08/2014, 14:20

TỪ KHÓA LIÊN QUAN