-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: org.hibernate.PropertyValueException: not-null property refe
PostPosted: Thu Jul 05, 2007 2:52 pm 
Newbie

Joined: Mon May 28, 2007 6:24 am
Posts: 11
Location: Bangalore
Scenario

My program is a Simple Swing program which asks the user to input a ProductID and parts for it. This program demonstrates Hibernate Set Mapping.

Hibernate version: 3.2.4

Mapping documents

Product.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="hibernate.Product" table="products">
   <id name="productId" column="productId" >
       <generator class="assigned"/>
   </id>

  <set name="parts">
     <key column = "productId" not-null="true" />
     <one-to-many class = "hibernate.Parts" />
  </set>
</class>
</hibernate-mapping>



Parts.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="hibernate.Parts" table="parts">
    <id name="partId" column="partId" >
       <generator class="assigned"/>
     </id>
     <property name="partName" column="partName" />
  </class>
</hibernate-mapping>



Code



Code:

package hibernate;

import java.util.HashSet;
import java.util.Set;

import javax.swing.JOptionPane;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class ProductAppln {
   public static void main(String[] args) {
       Session session = null;
       Transaction tx = null;
       Product product = null;
       String choice = null;
       int count = 0;
       try{
          SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          session =sessionFactory.openSession();
          tx = session.beginTransaction();
          do{
             choice = JOptionPane.showInputDialog(null, "Do you want to Insert Data for Products (Y/N)");
             if (choice.equalsIgnoreCase("Y")){
                String productId = JOptionPane.showInputDialog(null, "Enter the Product ID (Number)");
                int prodId = Integer.parseInt(productId);
                String noOfParts = JOptionPane.showInputDialog(null, "Enter the number of parts for " +
                                                             "product id : "+productId);
                int no_parts = Integer.parseInt(noOfParts);
                Set parts_set = new HashSet();
                for(int i = 0; i< no_parts ; i++){
                   String partId = JOptionPane.showInputDialog(null, "Enter part id (Number)");
                   int pId = Integer.parseInt(partId);
                   String partName = JOptionPane.showInputDialog(null, "Enter part Name");
                   Parts parts = new Parts();
                   parts.setPartId(pId);
                   parts.setPartName(partName);
                   parts.setProductId(prodId);
                   parts_set.add(parts);
                   session.save(parts);
//                   session = sessionFactory.openSession();
                }
                
                product = new Product();
                product.setProductId(prodId);
                product.setParts(parts_set);
                session.save(product);
//                session =sessionFactory.openSession();
                tx.commit();
                count++;
          }else{
             return;
          }
          }while (!choice.equalsIgnoreCase("N"));
         
           System.out.println(count+ " Row(s) inserted into the Message Table");
          
       }
       catch(NumberFormatException e){
          e.printStackTrace();
       }catch(HibernateException e){
          e.printStackTrace();
       }
       catch(Exception e){
          e.printStackTrace();
       }finally{
          session.flush();
          session.close();
          System.exit(0);
       }
   }
}





Full stack trace of any exception that occurs:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.PropertyValueException: not-null property references a null or transient value: hibernate.Parts._partsBackref
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at hibernate.ProductAppln.main(ProductAppln.java:44)
Hibernate: update parts set partName=? where partId=?



Name and version of the database you are using:

Oracle 9i

Configuration File

Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
               "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
   
<hibernate-configuration>
      <session-factory>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:bob</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="hibernate.connection.password">tiger</property>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
      <property name="show_sql">true</property>
      <property name="hibernate.hbm2ddl.auto">update</property>

        <mapping resource="Parts.hbm.xml" />
        <mapping resource="Product.hbm.xml" />

      </session-factory>
</hibernate-configuration>
[/b]

_________________
http://www.hibernate-tutorial.com
http://www.spring-hibernate.com
http://www.spring-tutorial.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 3:48 pm 
Newbie

Joined: Thu Jan 04, 2007 1:36 pm
Posts: 16
Check your partsBackref column in your parts table for nullability. Its most probably a not null column.. and in that case, you'd have to set a value to it before trying to persist parts instance.
- Arun


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 9:47 pm 
Newbie

Joined: Mon May 28, 2007 6:24 am
Posts: 11
Location: Bangalore
Hi there. But I dont have any such column namely partsBackref for the parts table. I have only partName and partId. This program is basically a demonstration of collection mappng where one product can have many parts.

The following are my bean classes


Product.java

Code:
package hibernate;

import java.util.Set;

public class Product {
   private int productId;
   private Set parts;      //One product can contain many parts
   
   public Set getParts() {
      return parts;
   }
   public void setParts(Set parts) {
      this.parts = parts;
   }
   public int getProductId() {
      return productId;
   }
   public void setProductId(int productId) {
      this.productId = productId;
   }
}



and Parts.java
Code:
package hibernate;

import java.util.Set;

public class Parts {
   private int partId;
   private String partName;
   private int productId;
   
   public int getPartId() {
      return partId;
   }
   public void setPartId(int partId) {
      this.partId = partId;
   }
   public String getPartName() {
      return partName;
   }
   public void setPartName(String partName) {
      this.partName = partName;
   }
   public int getProductId() {
      return productId;
   }
   public void setProductId(int productId) {
      this.productId = productId;
   }
   
}



These are the Tables that are created once I run the Program but no data is populated

SQL> desc parts;
Name Null? Type
----------------------------------------- -------- --------------------------
PARTID NOT NULL NUMBER(10)
PARTNAME VARCHAR2(255 CHAR)
PRODUCTID NOT NULL NUMBER(10)

SQL> desc products
Name Null? Type
----------------------------------------- -------- --------------------------
PRODUCTID NOT NULL NUMBER(10)

SQL>

_________________
http://www.hibernate-tutorial.com
http://www.spring-hibernate.com
http://www.spring-tutorial.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 7:54 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 9:43 am
Posts: 30
Hi,

Are you getting this exception on
Code:
session.save(parts);

or
Code:
session.save(product);

?

may be I'm wrong, but I can't find somethig like
Code:
<many-to-one name="product" class="hibernate.Product" column="PRODUCT_ID"  not-null="false"/>

in your Parts.hbm.xml.
That could be a good reason to get

In Product.hbm.xml you have
Code:
<set name="parts">
     <key column = "productId" not-null="true" />
     <one-to-many class = "hibernate.Parts" />
  </set>


Why is not-null= "true"? You mean no Product can exist without Parts_Set?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 7:56 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 9:43 am
Posts: 30
Sorry, I whanted to say
That could be a good reason to get Exception


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 10:58 am 
Newbie

Joined: Thu Jan 04, 2007 1:36 pm
Posts: 16
No.. not-null="true" basically means that the product_id column in Parts table cannot be null.. which implies that the Parts CANNOT exist without the Product.
But you've pointed out to the right direction... looking at the hbm file for Parts, I dont see the product_id defined, though its defined in the java class.
Thats probably why you're getting this exception, coz you're using it in your Product.hbm.xml to map it to the Parts, but you havent defined that column in the Parts.hbm.xml. Including that should take care of the problem.

- Arun


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.