Search code examples
hibernatehibernateexception

could not find a getter for in class hibernate


I have Hibernate / java code that I am trying to modify to add a reference to another class. I had most of this project written by contractors, so I am unfamiliar with how to write the Hibernate pieces. I am now getting an error that I see in some of the other stackflow questions, but they don't seem to apply to my particular case.

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

I have two objects, WorkbookData, and WorkbookConfig. A WorkbookData has one and only one WorkbookConfig.

The following snippets should cover the code in question:

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"/>

The getter for workbook_config_id in WorkbookData is getWorkbook_config_id(). So I don't understand why I'm getting the error. Can anyone see the issue?


Solution

  • First of all it is really a bad practice to name variables separated with underscore(_)

    Java naming convention suggests using camel case that is instead of workbook_file_id using workbookFileId would be more appropriate.

    Lots of APIs follow this camel case convention strictly. As for Hibernate, never tried it but i am quite sure the problem is about variable names with underscore.

    I believe (can not test it at the moment) Hibernate is maybe looking for a method with signature getWorkbook_Config_Id.

    Then again, I did not test it.