You didn't have an Id property in Address class for the One-To-Many relationship. And you still need some JPA annotation tag to present the relationship. You should refer to Hibernate annotation reference. Rick Guo 5/22/2007
-----原始邮件----- 发件人:"paulie" 发送时间:2007-05-16 21:36:49 收件人:[email protected] 抄送:(无) 主题:[appfuse-user] Basic One-To-Many 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#a10641915 Sent from the AppFuse - User mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] 劲爆150万同时在线,众人追捧梦幻西游
