Why Inverse Must Be Setted in Hibernate Bidirectional Association Of One-to-Many and Many-to-Many

xiaoxiao2021-04-05  260

If you've used CMP 2.0 / 2.1, you're familiar with the concept of a managed association (or managed relationship) CMP associations are called container-managed relationships (CMRs) for a reason Associations in CMP are inherently bidirectional:.. A Change made to one side of an association is instantly reflected at the Other Side. for example, if we call bid.setitem (item), The Container Automatical Calls Item.getbids (). Add (item).

Transparent POJO-oriented persistence implementations such as Hibernate donot implement managed associations. Contrary to CMR, Hibernate associations areall inherently unidirectional. As far as Hibernate is concerned, the association from Bid to Item is a different association than the association from Item to Bid. Paper Item and BID are one-to-many relationship

This passage tells us that Hibernate does not have a relationship between the container management object as the CMP, the object in Hibernate is inniderational (unidirectional); Hibernate, there are two relationships: Unidirection, Bidirection, object The two-way relationship is indicated by inverse, the Inverse property tells the Hibernate "Inverse end" to maintain this relationship: One-to-Many's MANY or MANY-TO-MANY LINK TABLE

Making the association bidirectional

. So far so good But we also need to be able to easily fetch all the bids for a particular item We need a bidirectional association here, so we have to add scaffolding code to the Item class:. Public class Item {... private Set bids = new hashset (); public void setbids (set bids) {this.bids = bids;} public set getbids () {Return Bids;} public void addbid (bid) {bid.setItem (this); bids. Add (bid);} ...} You can think of the code in addbid () (a convenience method) as item in the Object Model.a Basic Mapping for this One-to-Many Association Would THIS: ... < / Class>

The column mapping defined by the element is a foreign key column of theassociated BID table. Notice that we specify the same foreign key column in this collection mapping that we specified in the mapping for the many-to-one association.The table structure for this association mapping is shown in figure 3.11.Now we have two different unidirectional associations mapped to the same foreign key, which poses a problem At runtime, there are two different in-memory representations of the same foreign key value:. the item property of Bid and an element of the bids collection held by an Item Suppose our application modifies the association by, for example, adding a bid to an item in this fragment of the addBid () method:. bid.setItem (item); / / Thus the BID ItemID field value, Item.id; otherwise, Hibernate is updated to nullbids.add (bid); this code is Fine, But in this situation, Hibernate Detects Two Different Changes to the in-memory Persistent Instances. From The point of view of the database, Just One Value Must Be Updated T o reflect these changes:. the ITEM_ID column of the BID table Hibernate does not transparently detect the fact that the two changes refer to the same database column, since at this point we've done nothing to indicate that this is a bidirectional association.

We need one more thing in our association mapping to tell Hibernate to treatthis as a bidirectional association: The inverse attribute tells Hibernate that the collection is a mirror image of the many-to-one association on the other side: ... In fact, in One-to-Many relationship, INVERS = "true" is not set, Hibernate also generates expected results, just executes several unwanted UPDATE statements (these Update looks a bit inexplicable), in general Will not affect the execution result;

about many-to-manyWhen you map a bidirectional many-to-many association, you must declare oneend of the association using inverse = "true" to define which side's state is used to update the link table.same as one-to-many

转载请注明原文地址:https://www.9cbs.com/read-132090.html

New Post(0)