[jboss-user] [EJB 3.0] - Re: Cannot join @OneToOne on non-PK fields?

sisepago do-not-reply at jboss.com
Mon Nov 13 19:40:38 EST 2006


# @JoinColumn(name="status", referencedColumnName="code")
-----
please read the jpa spec to understand how to use @JoinColumn: you can only use one of this attributes not both, because if you use both, name="status" is used as referenced key

# Lazy fetching has no problem, because I also use it:

@javax.persistence.Entity
  | public class Customer implements java.io.Serializable{
  |    /** The serialVersionUID */
  |    private static final long serialVersionUID = 1L;
  |    private long id;
  |    private String lastName;
  |    private String firstName;
  |    private Address address;
  |    
  |    #1 can be used
  |    @javax.persistence.OneToOne(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
  |    @javax.persistence.JoinColumn(name="ADDRESSID")
  | 
  |    #2 can also be used
  |    @javax.persistence.OneToOne(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
  |    @javax.persistence.JoinColumn(referencedColumnName="ZIP")
  |    public Address getAddress() {return this.address;}
  |    public void setAddress(Address address){this.address = address;}
  |    
  | }  
  | 
  | 

and in my client class LEFT JOIN FETCH syntax to fetch customer with lazy address object

   public List<Customer> getCustomersLazyUsing(long cId) {
  |       String query = "select c from Customer c " +
  |       "left join fetch c.address" + 
  |       " where c.id = :cId ";

and all work perfect

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3985657#3985657

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3985657



More information about the jboss-user mailing list