Mirrana
Mirrana

Reputation: 1731

Why is a field in my Hibernate entity null?

I added a new integer column to a table in my MSSQL database, and then updated the .hbm.xml and .java files accordingly for the Hibernate entity of this table. I can see that I have assigned a '1' to the column for a given row that I'm working with, yet the entity I am getting back for this row has a null value in its field.

What seems even more strange is that, upon inspecting the usage of the getter and setter for this field by using breakpoints, they appear to be taking in and sending out a 1 when expected... though the boolean helper method I created (to return false if the value is 0 and true if 1), is seeing the field as null and is throwing an exception. This is happening even after the setter is called on the entity.

Any ideas of why this is happening? I'm only very mildly familiar with Hibernate, but I suspect that it's not a lazy loading issue since this is an integer type, not another entity altogether.

Edit

Here's the relevant parts of my Hibernate files. I can't include much due to confidentiality reasons, but this should be generic enough.

<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 8-Sep-2011 3:31:37 PM by Hibernate Tools 3.2.4.GA -->
    <hibernate-mapping>
            <class name="net.company.EntityName" table="TableName" schema="dbo" catalog="database">
                <id name="id" type="int">
                    <column name="TableNameId" />
                    <generator class="identity" />
                </id>
                    ...
                <property name="cancelled" type="int">
                    <column name="Cancelled" />
                </property>
                <property name="cancelledOn" type="timestamp">
                    <column name="CancelledOn" length="23" />
                </property>
                <property name="cancelledBy" type="string">
                    <column name="CancelledBy" length="50" />
                </property>
                    ...
            </class>
    </hibernate-mapping>


    public Integer getCancelled() {
        return cancelled;
    }

    public void setCancelled(Integer cancelled) {
        this.cancelled = cancelled;
    }

    public boolean isCancelled() {
        return cancelled != null && cancelled == 1;
    }

    public Date getCancelledOn() {
        return cancelledOn;
    }

    public void setCancelledOn(Date cancelledOn) {
        this.cancelledOn = cancelledOn;
    }

    public void setCancelledBy(String cancelledBy) {
        this.cancelledBy = cancelledBy;
    }

    public String getCancelledBy() {
        return cancelledBy;
    }

I thought that it may possibly be because of improper Dozer behavior, but the following is the only mapping in the dozer-mappings.xml file relevant to this entity:

<mapping>
    <class-a>net.company.EntityName</class-a>
    <class-b>net.company.EntityName</class-b>
</mapping>

Upvotes: 0

Views: 1607

Answers (1)

udalmik
udalmik

Reputation: 7998

Both

boolean isCancelled()

and

Integer getCancelled()

represents an accessor to the same property "cancelled" in terms of Java Beans spec, it can break Hibernate's properties discovery logic. Please try to rename boolean utility method to some other name, e.g:

boolean checkCancelled()

Upvotes: 2

Related Questions