TAILIEUCHUNG - Beginning Hibernate From Novice to Professional phần 3

Các yêu cầu cấu hình Hibernate trong một môi trường EJB 3 hơi khác với mặc định yêu cầu chúng tôi thảo luận sâu hơn trong Phụ lục A. Nếu bạn đã sử dụng hoặc EJB trong quá khứ, bạn sẽ tìm thấy EJB 3 là một đơn giản hóa rất cần thiết. Hibernate 3 cho phép bạn tận dụng lợi thế của tính năng chú thích mới của Java 5. | CHAPTER 3 BUILDING A SIMPLE APPLICATION 49 import import import import public class DAO protected DAO public static Session getSession Session session Session if session null session session return session protected void begin getSession .beginTransaction protected void commit getSession .getTransaction .commit protected void rollback try getSession .getTransaction .rollback catch HibernateException e Cannot rollback e try getSession .close catch HibernateException e Cannot close e null public static void close getSession .close null 50 CHAPTER 3 BUILDING A SIMPLE APPLICATION private static final Logger log 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 .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.