无法在课堂休眠中找到一个getter

时间:2014-09-01 21:32:36

标签: hibernate hibernateexception

我正在尝试修改 Hibernate / java 代码以添加对另一个类的引用。我的大部分项目都是由承包商编写的,所以我不熟悉如何编写Hibernate的部分。我现在收到一些错误,我在其他一些堆栈流问题中看到,但它们似乎不适用于我的特定情况。

Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for workbook_config_id in class gov.nrel.nbc.spreadsheet.dto.WorkbookData

我有两个对象, WorkbookData WorkbookConfig 。 WorkbookData只有一个WorkbookConfig。

以下代码段应涵盖相关代码:

WorkbookData.java

package gov.nrel.nbc.spreadsheet.dto;

import java.io.Serializable;

...
public class WorkbookData  implements Serializable {
    private static final long serialVersionUID = -2765666207168226677L;
    private long workbook_id;
    private WorkbookFileData workbook_file_id;
    private WorkbookConfig workbook_config_id;
...
    /**
     * @return the workbook_config_id
     */
    public WorkbookConfig getWorkbook_config_id() {
        return workbook_config_id;
    }
    /**
     * @param workbook_config_id the workbook_config_id to set
     */
    public void setWorkbook_config_id(WorkbookConfig workbook_config_id) {
        this.workbook_config_id = workbook_config_id;
    }

WorkbookConfig.java

package gov.nrel.nbc.spreadsheet.dto;

import gov.nrel.nbc.spreadsheet.dto.WorkbookFileData;
import gov.nrel.nbc.spreadsheet.dto.SheetConfig;

...

public class WorkbookConfig implements Serializable {
    private static final long serialVersionUID = 8789432250514085496L;
    private String config_name;
    private String synonym;
    private long workbook_config_id;
...
/**
 * @param workbook_config_id the workbook_config_id to set
 */
public void setWorkbook_config_id(long workbook_config_id) {
    this.workbook_config_id = workbook_config_id;
}
/**
 * @return the workbook_config_id
 */
public long getWorkbook_config_id() {
    return workbook_config_id;
}

WorkbookData.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="gov.nrel.nbc.spreadsheet.dto.WorkbookData" table="workbook_data" lazy="false">

    <id name="workbook_id" 
        column="workbook_id">
        <generator class="increment"/>
    </id>

    <many-to-one name="workbook_file_id"
        class="gov.nrel.nbc.spreadsheet.dto.WorkbookFileData"
        column="workbook_file_id"
        lazy="false"
        not-null="false"/>

    <many-to-one name="workbook_config_id"
        class="gov.nrel.nbc.spreadsheet.dto.WorkbookConfig"
        column="workbook_config_id"
        not-null="false"/>
...

WorkbookConfig.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="gov.nrel.nbc.spreadsheet.dto.WorkbookConfig" table="workbook_config" lazy="false">

    <id
        name="workbook_config_id"
        column="workbook_config_id">
        <generator
            class="increment"/>
    </id>

    <property
        name="config_name"
        column="config_name"
        not-null="true"
         lazy="false"/>

    <property
        name="synonym"
        column="synonym"
        not-null="true"
         lazy="false"/>

WorkbookData中workbook_config_id的getter是getWorkbook_config_id()。所以我不明白为什么我得到错误。有谁能看到这个问题?

1 个答案:

答案 0 :(得分:1)

首先,命名用下划线(_)

分隔的变量实际上是一种不好的做法

Java命名约定建议使用camel case而不是workbook_file_id,使用workbookFileId会更合适。

许多API严格遵循此驼峰案例约定。至于Hibernate,从未尝试过,但我很确定问题是关于带下划线的变量名。

我相信(目前无法测试)Hibernate可能正在寻找带有签名getWorkbook_Config_Id的方法。

然后,我没有测试它。