【问题标题】:many to one problem多对一问题
【发布时间】:2011-01-23 13:25:03
【问题描述】:

这个错误我已经有几个小时了,但我不再得到它了。这是一个带有 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-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)

解决这个问题的方法,现在我什至不知道这是否是声明客户和项目之间关系的正确方法。您在上面的代码中看到错误了吗?总之非常感谢。

【问题讨论】:

  • 该错误是非常基本的——没有用于描述客户端的吸气剂。问题是,在您的映射和代码中,描述在 Project 上。你在某处有多余的 hbm.xml 吗?您是否尝试过干净的重建?

标签: hibernate hibernate-mapping


【解决方案1】:

我发现问题是我的注意力,这段代码运行良好,但另一个 hbm.xml 中有错误,重复名称 Clients 而不是所需的名称。 我找错地方了,是我的错。 谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多