Hi friends
There are some peculiarities with bidirectional relationships in JPA. With all bidirectional relationship types, including one-to-one, there is always the concept of an owning side of the relationship. see this sample :
@Entity
public class CreditCard implements java.io.Serializable
{
@OneToOne(mappedBy="creditCard")
private Customer customer;
public Customer getCustomer( )
{
return this.customer;
}
public void setCustomer(Customer customer)
{
this.customer = customer;
}
}
@Entity
public class Customer implements java.io.Serializable
{
@OneToOne
(cascade={CascadeType.ALL})
@JoinColumn(name="CREDIT_CARD_ID")
private CreditCard creditCard;
public CreditCard getCreditCard( )
{
return creditCard;
}
public void setCreditCard(CreditCard card)
{
this.creditCard = card;
}
}
Although a setCustomer( ) method is available in the CreditCard bean class, it will not cause a
change in the persistent relationship if we set it. When we marked the @OneToOne relationship in the CreditCard bean class with the mappedBy( ) attribute, this designated the CreditCard entity as the inverse side of the relationship. This means that the Customer entity is the owning
side of the relationship.
If you wanted to associate a CreditCard instance with a different Customer , you would have to call setCreditCard( ) on the old Customer, passing in null, and then call setCreditCard( ) on the new Customer:
Customer newCust = em.find(Customer.class, newCustId);
CreditCard card = oldCustomer.getCreditCard( );
oldCustomer.setCreditCard(null);
newCust.setCreditCard(card);
bye.
There are some peculiarities with bidirectional relationships in JPA. With all bidirectional relationship types, including one-to-one, there is always the concept of an owning side of the relationship. see this sample :
@Entity
public class CreditCard implements java.io.Serializable
{
@OneToOne(mappedBy="creditCard")
private Customer customer;
public Customer getCustomer( )
{
return this.customer;
}
public void setCustomer(Customer customer)
{
this.customer = customer;
}
}
@Entity
public class Customer implements java.io.Serializable
{
@OneToOne
(cascade={CascadeType.ALL})
@JoinColumn(name="CREDIT_CARD_ID")
private CreditCard creditCard;
public CreditCard getCreditCard( )
{
return creditCard;
}
public void setCreditCard(CreditCard card)
{
this.creditCard = card;
}
}
Although a setCustomer( ) method is available in the CreditCard bean class, it will not cause a
change in the persistent relationship if we set it. When we marked the @OneToOne relationship in the CreditCard bean class with the mappedBy( ) attribute, this designated the CreditCard entity as the inverse side of the relationship. This means that the Customer entity is the owning
side of the relationship.
If you wanted to associate a CreditCard instance with a different Customer , you would have to call setCreditCard( ) on the old Customer, passing in null, and then call setCreditCard( ) on the new Customer:
Customer newCust = em.find(Customer.class, newCustId);
CreditCard card = oldCustomer.getCreditCard( );
oldCustomer.setCreditCard(null);
newCust.setCreditCard(card);
bye.