Suppose there is a bean Order which should have a 1:N relation to bean OrderItem. The configuration will be:
<beans> <bean class="jbg.tutorial.Order"> <property name="oneWayItems"> <sequence type="collection"> <bean-type type="jbg.tutorial.OrderItem" /> </sequence> </property> </bean> <bean class="jbg.tutorial.OrderItem"> <property name="order"> <bean-type type="jbg.tutorial.Order" /> </property> </bean> </beans>
This generates the following:
public class Order { private java.util.Collection<jbg.tutorial.OrderItem> oneWayItems; ... public final java.util.Collection<jbg.tutorial.OrderItem> getOneWayItems() { return oneWayItems; } public final void setOneWayItems(java.util.Collection<jbg.tutorial.OrderItem> oneWayItems) { this.oneWayItems = oneWayItems; } public final void addToOneWayItems(jbg.tutorial.OrderItem item) { this.oneWayItems.add(item); } ... } public class OrderItem { private jbg.tutorial.Order order; ... public final jbg.tutorial.Order getOrder() { return order; } public final void setOrder(jbg.tutorial.Order order) { this.order = order; } ... }
So far, we have created two distinct one-way relations. Now, let us tell JBG that it is in fact a single two-way relation. Add the reverse-property-name attribute:
<bean class="jbg.tutorial.Order"> <property name="twoWayItems"> <sequence type="collection"> <bean-type type="jbg.tutorial.OrderItem" reverse-property-name="order" /> </sequence> </property> </bean>
The generated source of Order changes as follows:
public class Order { private java.util.Collection<jbg.tutorial.OrderItem> twoWayItems; ... public final java.util.Collection<jbg.tutorial.OrderItem> getTwoWayItems() { return twoWayItems; } public final void setTwoWayItems(java.util.Collection<jbg.tutorial.OrderItem> twoWayItems) { this.twoWayItems = twoWayItems; } public final void setTwoWayItems_WithReverseSide(java.util.Collection<jbg.tutorial.OrderItem> twoWayItems) { setTwoWayItems(twoWayItems); // Set the reverse side of the relation if (null != this.twoWayItems) for (jbg.tutorial.OrderItem item : this.twoWayItems) item.setOrder(this); } public final void addToTwoWayItems(jbg.tutorial.OrderItem item) { this.twoWayItems.add(item); // Set the reverse side of the relation item.setOrder(this); } ... }
The addToTwoWayItems() method now sets the opposite side of the relation automatically, keeping it consistent. Similarly, a setTwoWayItems_WithReverseSide() method is generated that iterates all the items and sets the opposite side as well.
Suppose the bean Order has a 1:1 relation to bean Orderer.
<bean class="jbg.tutorial.Order"> ... <property name="oneWayOrderer"> <bean-type type="jbg.tutorial.Orderer" /> </property> </bean> <bean class="jbg.tutorial.Orderer"> <property name="oneWayOrder"> <bean-type type="jbg.tutorial.Order" /> </property> </bean>
This configuration generates:
public class Order { private jbg.tutorial.Orderer oneWayOrderer; ... public final jbg.tutorial.Orderer getOneWayOrderer() { return oneWayOrderer; } public final void setOneWayOrderer(jbg.tutorial.Orderer oneWayOrderer) { this.oneWayOrderer = oneWayOrderer; } ... public class Orderer { private jbg.tutorial.Order oneWayOrder; ... public final jbg.tutorial.Order getOneWayOrder() { return oneWayOrder; } public final void setOneWayOrder(jbg.tutorial.Order oneWayOrder) { this.oneWayOrder = oneWayOrder; } ... }
Let us now similarly set this relation up as two way:
<bean class="jbg.tutorial.Order"> ... <property name="twoWayOrderer"> <bean-type type="jbg.tutorial.Orderer" reverse-property-name="twoWayOrder" /> </property> </bean> <bean class="jbg.tutorial.Orderer"> <property name="twoWayOrder"> <bean-type type="jbg.tutorial.Order" reverse-property-name="twoWayOrderer" /> </property> </bean>
This configuration generates:
public class Order { private jbg.tutorial.Orderer twoWayOrderer; ... public final jbg.tutorial.Orderer getTwoWayOrderer() { return twoWayOrderer; } public final void setTwoWayOrderer(jbg.tutorial.Orderer twoWayOrderer) { this.twoWayOrderer = twoWayOrderer; } public final void setTwoWayOrderer_WithReverseSide(jbg.tutorial.Orderer twoWayOrderer) { setTwoWayOrderer(twoWayOrderer); // Set the reverse side of the relation if (null != twoWayOrderer && this != twoWayOrderer.getOrder()) twoWayOrderer.setOrder(this); } ... public class Orderer { private jbg.tutorial.Order twoWayOrder; ... public final jbg.tutorial.Order getTwoWayOrder() { return twoWayOrder; } public final void setTwoWayOrder(jbg.tutorial.Order twoWayOrder) { this.twoWayOrder = twoWayOrder; } public final void setTwoWayOrder_WithReverseSide(jbg.tutorial.Order twoWayOrder) { setTwoWayOrder(twoWayOrder); // Set the reverse side of the relation if (null != twoWayOrder && this != twoWayOrder.getTwoWayOrderer()) twoWayOrder.setTwoWayOrderer(this); } ... }
Next >> |