Search code examples
hibernatejava-7hibernate-mappingweblogic11g

net.sf.hibernate.PropertyValueException: not-null property references a null or transient value


I am trying to set 'Level' with some values but i get exception. Below is the version and code.

Hibernate version: 2.1

Server: Weblogic 11g

Mapping documents:

 <property name="Level" type="java.lang.Integer">
 <meta attribute="default-value">new java.lang.Integer(0)</meta>    
 <column name="Level" not-null="true" length="3" />    
 </property>

Code:

public Pol fillPol(ResultSet rs) throws SQLException {
      Pol p = new Pol();
      p.setLevel(new Integer(rs.getInt("setLevel")));
      if (rs.IsNull()) {
          p.setLevel(null);
      }
      return p;
  }

Exception i get

Caused by: net.sf.hibernate.PropertyValueException: not-null property references a null or transient value

Please help.


Solution

  • That's because you have a DB <-> Java datatype mismatch.

    To fix this check:

    • DB column. If it's allowed to have null values.
    • Java type. If it's primitive type or not.

    Try the following:

      public Pol fillPol(ResultSet rs) throws SQLException {
          Pol p = new Pol();
          p.setLevel(new Integer(rs.getInt("setLevel")));
          if (rs.IsNull()) {
              p.setLevel(Integer.valueOf(0)); //according to the mapping you're not setting it to null, but to zero.
          }
          return p;
      }