Hi Paulie,

The example you posted is quite jumbled, you have some annotations and also
some xdoclet tags in the same example.  The first thing that you need to do
it decide which you are going to use (I would suggest the annotations and
remove the xdoclet tags).

Hibernate can be quite confusing to get sorted.  The hibernate manual may
help
http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#entity-mapping-association-collections

I have had to implement a couple of @OneToMany relationships recently with
the OneToMany side being the owning side and it was a bit tricky.  I found
that I had use a JoinTable and 'manually' maintain the relationship between
the entities in that table.  I guess that a code example is what you are
after..

For a unidirectional relationsip with Person being the 'owning' side

in Person.java

@OneToMany
@JoinTable(name="person_address",
        joinColumns = @JoinColumn(name="person_fk"),
        inverseJoinColumns = @JoinColumns(name="address_fk")
    )
public List<Address> getAddresses() {
        return addresses;
    }

in Address.java

//nothing required

For a bidirectional relationship owned by the address side

in Person.java

@OneToMany(mappedBy="person")
public List<Address> getAddresses() {
        return addresses;
    }

in Address.java

    @ManyToOne
    @JoinColumn(name="person_fk")
    public Person getPerson() {
    return person;
}    

I have tried following code examples for having the Person side the owning
side but they say that there are  bad performance hits when doing so... so I
have avoided that.  

When you delete an address you will need to get the Person and remove the
Address from the List object.  When you add an address you will need to add
that Address to the Person entity, otherwise you will get errors to say that
associations still exist to the entity that you are trying to remove. I will
find some more detailed code and post a complete tomorrow.

Hope this helps,
Luke.



paulie wrote:
> 
> I have been looking through the forum and user guides trying to piece
> together the code for putting together a one-to-many relationship.  In my
> code, a Person can have many Addresses.  The error I am getting is
> MappingException: Could not determine type for: java.util.List, for
> columns: [org.hibernate.mapping.Column(address)].  Below is the code for
> Person and Address.  Any ideas?
> 
> package com.blob.app.model;
> import java.io.Serializable;
> import java.util.ArrayList;
> import java.util.List;
> import org.appfuse.model.BaseObject;
> import org.apache.commons.lang.builder.EqualsBuilder;
> import org.apache.commons.lang.builder.HashCodeBuilder;
> import org.apache.commons.lang.builder.ToStringBuilder;
> import javax.persistence.*;
> 
> /**
>  * @hibernate.class table="person"
>  */
> @Entity
> public class Person extends BaseObject implements Serializable  {
>       private static final long serialVersionUID = 141905530620673329L;
>       private Long personId;
>       private String firstName;
>       private String lastName;
> 
>       // @hibernate.id column="person_id" generator-class="increment"
> unsaved-values="null"
>       @Id @GeneratedValue(strategy=GenerationType.AUTO)
>       public Long getPersonId() {
>               return this.personId;
>       }
>       public void setPersonId(Long personId) {
>               this.personId = personId;
>       }
>       
>       @Column(name="first_name", length=50)
>       public String getFirstName() {
>               return firstName;
>       }
>       public void setFirstName(String firstName) {
>               this.firstName = firstName;
>       }
>       
>       @Column(name="last_name", length=50)
>       public String getLastName() {
>               return lastName;
>       }
>       public void setLastName(String lastName) {
>               this.lastName = lastName;
>       }
>       
>       private List<Address> addresses = new ArrayList<Address>();
> 
>     /**
>      * @return Returns the addresses.
>      * 
>      * @hibernate.bag table="addresses" lazy="false" cascade="all"
>      * @hibernate.collection-key column="person_id"
>      * @hibernate.collection-one-to-many class="org.appfuse.model.Address"
>      */
>     public List<Address> getAddresses() {
>         return addresses;
>     }
> 
>     public void setAddresses(List<Address> addresses) {
>         this.addresses = addresses;
>     }
> 
>     public void addAddress(Address address) {
>         getAddresses().add(address);
>     }
>       
>     public boolean equals(Object object) {
>       if (!(object instanceof Person)) {
>               return false;
>       }
>       Person person = (Person) object;
>       return new EqualsBuilder().append(this.firstName, person.firstName)
>                       .append(this.personId, 
> person.personId).append(this.lastName,
> person.lastName)
>                       .isEquals();
>     }
>     public int hashCode() {
>       return new HashCodeBuilder(1982570889, -560823371).append(
>                       
> this.firstName).append(this.personId).append(this.lastName)
>                       .toHashCode();
>     }
>     public String toString() {
>       return new ToStringBuilder(this).append("lastName", this.lastName)
>                       .append("personId", this.personId).append("firstName", 
> this.firstName)
>                       .toString();
>     }
> }
> 
> 
> package com.blob.app.model;
> import java.io.Serializable;
> import javax.persistence.Column;
> import org.apache.commons.lang.builder.ToStringBuilder;
> import org.apache.commons.lang.builder.ToStringStyle;
> import org.appfuse.model.BaseObject;
> 
> /**
>  * This class is used to represent an address.
>  */
> public class Address extends BaseObject implements Serializable {
>     private static final long serialVersionUID = 3617859655330969141L;
>     protected String address;
>     protected String city;
>     protected String province;
>     protected String country;
>     protected String postalCode;
> 
>     // @hibernate.property column="address" not-null="false" length="150"
>     @Column(name="address", length=150)
>     public String getAddress() {
>         return address;
>     }    
>     public void setAddress(String address) {
>         this.address = address;
>     }
> 
>     // @hibernate.property column="city" not-null="true" length="50"
>     @Column(name="city", nullable=false, length=50)
>     public String getCity() {
>         return city;
>     }    
>     public void setCity(String city) {
>         this.city = city;
>     }
> 
>     // @hibernate.property column="province" length="100"
>     @Column(name="province", length=100)
>     public String getProvince() {
>         return province;
>     }    
>     public void setProvince(String province) {
>         this.province = province;
>     }
> 
>     // @hibernate.property column="country" length="100"
>       @Column(name="country", length=100)
>     public String getCountry() {
>         return country;
>     }
>     public void setCountry(String country) {
>         this.country = country;
>     }
> 
>     // @hibernate.property column="postal_code" not-null="true"
> length="15"
>     @Column(name="postalCode", nullable=false, length=15)
>     public String getPostalCode() {
>         return postalCode;
>     }
>     public void setPostalCode(String postalCode) {
>         this.postalCode = postalCode;
>     }
>     
>     public boolean equals(Object o) {
>         if (this == o) return true;
>         if (!(o instanceof Address)) return false;
> 
>         final Address address1 = (Address) o;
> 
>         if (address != null ? !address.equals(address1.address) :
> address1.address != null) return false;
>         if (city != null ? !city.equals(address1.city) : address1.city !=
> null) return false;
>         if (country != null ? !country.equals(address1.country) :
> address1.country != null) return false;
>         if (postalCode != null ? !postalCode.equals(address1.postalCode) :
> address1.postalCode != null) return false;
>         if (province != null ? !province.equals(address1.province) :
> address1.province != null) return false;
> 
>         return true;
>     }
> 
>     public int hashCode() {
>         int result;
>         result = (address != null ? address.hashCode() : 0);
>         result = 29 * result + (city != null ? city.hashCode() : 0);
>         result = 29 * result + (province != null ? province.hashCode() :
> 0);
>         result = 29 * result + (country != null ? country.hashCode() : 0);
>         result = 29 * result + (postalCode != null ? postalCode.hashCode()
> : 0);
>         return result;
>     }
> 
>     /**
>      * Generated using Commonclipse (http://commonclipse.sf.net)
>      */
>     public String toString() {
>         return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
>                 .append("country", this.country)
>                 .append("address", this.address).append("province",
>                         this.province).append("postalCode",
> this.postalCode)
>                 .append("city", this.city).toString();
>     }
> }
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Basic-One-To-Many-tf3764612s2369.html#a10760243
Sent from the AppFuse - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to