CHAPTER 12 Modifying objects efficiently This chapter shows you how to make data manipulations more efficient. We optimize and reduce the amount of code that is necessary to store objects and discuss the most efficient processing options. You should be familiar with the basic object states and the persistence interfaces; the previous chapters are required reading to understand this chapter. First we ll show you how transitive persistence can make your work with complex object networks easier. The cascading options you can enable in Hibernate and Java Persistence applications significantly reduce the amount of code that s otherwise needed to insert, update, or delete several objects at the same time. We then discuss how large datasets are best handled, with batch operations in your application or with bulk operations that execute directly in the database. Finally, we show you data filtering and interception, both of which offer transparent hooks into the loading and storing process inside Hibernate s engine. These features let you influence or participate in the lifecycle of your objects without writing complex application code and without binding your domain model to the persistence mechanism. Let s start with transitive persistence and store more than one object at a time. 12.1 Transitive persistence Real, nontrivial applications work not only with single objects, but rather with networks of objects. When the application manipulates a network of persistent objects, the result may be an object graph consisting of persistent, detached, and transient instances. Transitive persistence is a technique that allows you to propagate persistence to transient and detached subgraphs automatically. For example, if you add a newly instantiated Category to the already persistent hierarchy of categories, it should become automatically persistent without a call to save() or persist(). We gave a slightly different example in chapter 6, section 6. 4, Mapping a parent/children relationship, when you mapped a parent/child relationship between Bid and Item. In this case, bids were not only automatically made persistent when they were added to an item, but they were also automatically deleted when the owning item was deleted. You effectively made Bid an entity that was completely dependent on another entity, Item (the Bid entity isn t a value type, it still supports shared reference). There is more than one model for transitive persistence. The best known is persistence by reachability; we discuss it first. Although some basic principles are the same, Hibernate uses its own, more powerful model, as you ll see later. The same
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.