0

我有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

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

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

工作簿数据.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;
}

工作簿数据.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()。所以我不明白为什么我会收到错误。任何人都可以看到这个问题吗?

4

1 回答 1

1

首先,用下划线(_) 分隔变量名确实是一种不好的做法

Java 命名约定建议使用驼峰式大小写代替 workbook_file_id 使用 workbookFileId 会更合适。

许多 API 都严格遵循这种驼峰式约定。至于 Hibernate,从未尝试过,但我很确定问题出在带下划线的变量名上。

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

再说一次,我没有测试它。

于 2014-09-01T21:41:23.660 回答