2011-01-23 42 views
0

我有幾個小時現在這個錯誤,我不明白了。這是一個Spring 2.5和hibernate3的項目。我曾嘗試在客戶端對象中聲明一個項目對象。多對一的問題

我有2個表:客戶和項目

CREATE TABLE client (clientId INT NOT NULL, 
        PRIMARY KEY (clientId) 
) ENGINE=INNODB; 
CREATE TABLE project (projectId INT, clientId INT, 
        INDEX par_ind (clientId), 
        FOREIGN KEY (clientId) REFERENCES client(clientId) 
         ON DELETE CASCADE 
) ENGINE=INNODB; 

在Java代碼:

public class Project implements Serializable { 

private int id; 
private String name; 
private String description; 
private Date startDate; 
private Date endDate; 
private String manager; 
private Client client; 

public Project(String name, Date startDate, Date endDate) { 

    this.name = name; 
    this.startDate = startDate; 
    this.endDate = endDate; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public Date getStartDate() { 
    return startDate; 
} 

public void setStartDate(Date startDate) { 
    this.startDate = startDate; 
} 

public Date getEndDate() { 
    return endDate; 
} 

public void setEndDate(Date endDate) { 
    this.endDate = endDate; 
} 

public String getManager() { 
    return manager; 
} 

public void setManager(String manager) { 
    this.manager = manager; 
} 

public Client getClient() { 
    return client; 
} 

public void setClient(Client client) { 
    this.client = client; 
} 

public String toString() { 
    StringBuffer buffer = new StringBuffer(); 
    buffer.append("Project name: " + name); 
    buffer.append("Project description: " + description); 
    buffer.append("Project start date: " + startDate); 
    buffer.append("Project end date: " + endDate); 
    return buffer.toString(); 
} 

public boolean equals(Object obj) { 
    if ((obj instanceof Project) 
    && (((Project) obj).getName() == this.name)) { 
    return true; 
    } 
    return false; 
} 

} 



public class Client implements Serializable { 

/** 
    * 
    */ 
private static final long serialVersionUID = 1L; 
private int id; 
private String name; 
private String company; 
private String location; 
private String city; 
private String country; 

public Client() { 

} 

public Client(String name) { 
    this.name = name; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getCompany() { 
    return company; 
} 

public void setCompany(String company) { 
    this.company = company; 
} 

public String getLocation() { 
    return location; 
} 

public void setLocation(String location) { 
    this.location = location; 
} 

public String getCity() { 
    return city; 
} 

public void setCity(String city) { 
    this.city = city; 
} 

public String getCountry() { 
    return country; 
} 

public void setCountry(String country) { 
    this.country = country; 
} 

public String toString() { 
    StringBuffer buffer = new StringBuffer(); 
    buffer.append("Client name: " + name); 
    buffer.append("Client company: " + company); 
    buffer.append("Client location: " + location); 
    buffer.append("Client city: " + city); 
    buffer.append("Client country: " + country); 
    return buffer.toString(); 
} 

public boolean equals(Object obj) { 
    if ((obj instanceof Client) && (((Client) obj).getName() == this.name)) { 
    return true; 
    } 
    return false; 
} 

} 

Hibernate映射文件中,我有這樣的:

<hibernate-mapping package="test.domain"> 
<class name="Client" table="clients" dynamic-update="true"> 
    <id name="id" column="clientId" type="integer"> 
    <generator class="increment" /> 
    </id> 
    <property name="name" column="name" type="string" /> 
    <property name="company" column="company" type="string" /> 
    <property name="location" column="location" type="string" /> 
    <property name="city" column="city" type="string" /> 
    <property name="country" column="country" type="string" /> 

</class> 
</hibernate-mapping> 

<hibernate-mapping package="test.domain"> 
<class name="Project" table="project" dynamic-update="true"> 
    <id name="id" column="projectId" type="integer"> 
    <generator class="increment" /> 
    </id> 
    <property name="name" column="name" type="string" /> 
    <property name="description" column="description" type="string" /> 
    <property name="startDate" column="start_date" type="string" /> 
    <property name="endDate" column="end_date" type="string" /> 
    <property name="manager" column="manager" type="string" /> 

    <!-- a client has many projects --> 
    <many-to-one name="client" column="clientId" 
    class="test.domain.Client" cascade="save-update" lazy="false" /> 

</class> 
</hibernate-mapping> 

而且我收到此錯誤:

Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for description in class test.domain.Client 
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282) 
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:275) 

方式來包裝出了這個問題,現在我甚至不知道這是不是申報客戶和項目之間的關係的正確方法。你在上面的代碼中看到錯誤嗎?無論如何非常感謝你。

+0

該錯誤是非常基本的東西 - 沒有getter描述客戶端。事情是,在你的映射和代碼中,描述在Project上。你有沒有額外的hbm.xml文件?你嘗試了一個乾淨的重建? – hvgotcodes

回答

0

我發現問題是我的注意,這段代碼工作正常,但在另一個hbm.xml中出現錯誤,重名的客戶端而不是必需的名稱。 我在找錯地方,這是我的錯誤。 謝謝。