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:
The column mapping defined by the
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:
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