-->
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.  [ 2 posts ] 
Author Message
 Post subject: Not inserting in a table | Collection mapping using Sets
PostPosted: Fri Jul 06, 2007 10:38 am 
Newbie

Joined: Mon May 28, 2007 6:24 am
Posts: 11
Location: Bangalore
My program is developed using Hibernate to demonstrate Collection mapping using sets. Basically A product can contain many parts (set).

The basic probelm is when i run this program the products table is getting inserted but the parts table is empty.

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

  <set name="parts">
     <key column = "productId" />
     <one-to-many class = "hibernate.Parts" />
  </set>
<property name = "productName" type = "string" />

</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 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;
       Parts parts = null;
       try{
          SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          session =sessionFactory.openSession();
          tx = session.beginTransaction();
          Set parts_set = new HashSet();
         parts = new Parts();
         parts.setPartId(1);
         parts.setPartName("screw");
         parts.setPartId(1);
         parts_set.add(parts);
         parts = new Parts();
         parts.setPartId(2);
         parts.setPartName("nuts");
         parts_set.add(parts);

         product = new Product();
         product.setProductId(1);
         product.setParts(parts_set);
         product.setProductName("Laptop");
         session.save(product);
         tx.commit();
       }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:

No error but this is what got displayed in console

llog4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into products (productName, productId) values (?, ?)
Hibernate: update parts set productId=? where partId=?
Hibernate: update parts set productId=? 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>




The Bean Code

Code:

package hibernate;

import java.util.Set;

public class Product {
   private int productId;
   private String productName;
   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;
   }
   public String getProductName() {
      return productName;
   }
   public void setProductName(String productName) {
      this.productName = productName;
   }
}

package hibernate;


public class Parts {
   private int partId;
   private String partName;
   
   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;
   }
}





Results in the backend

SQL> select * from products;

PRODUCTID
----------
PRODUCTNAME
---------------------------------------------
1
Laptop


SQL> select * from parts;

no rows selected

NO DATA WAS BEING INSERTED in the PARTS TABLE

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 11:03 am 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
You have here an example of a Disctrict, that in Portugal is the divisory of the country.

Country has many Districts
Districts has many Concelhos

The Entity Distrito
Code:
@Entity
@Table(name="DISTRITOS")
public class Distrito implements Serializable {
    // <editor-fold defaultstate="collapsed" desc=" PrimaryKey:   String id ">
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_distrito", nullable=false)
    private Long id;
   
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    //</editor-fold>
   
    // <editor-fold defaultstate="collapsed" desc=" Property:   String nome ">
    @Column(name="nome_distrito", nullable=false, unique=true)
    private String nome;
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    // </editor-fold>
   
    // <editor-fold defaultstate="collapsed" desc=" N-1  Relation to Pais pais ">
    @ManyToOne
    @JoinColumn(name="PAIS_FK", nullable=false)
    private Pais pais;
   
    public Pais getPais() {
        return this.pais;
    }
   
    public void setPais(Pais pais) {
        this.pais = pais;
    }
    // </editor-fold>
   
    // <editor-fold defaultstate="collapsed" desc=" 1-N  Relation to Collection /*Concelho*/ concelhos ">
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy="distrito")
    private List<Concelho> concelhos = new ArrayList<Concelho>();
   
    public List<Concelho> getConcelhos() {
        return this.concelhos;
    }
   
    public void setConcelhos(List<Concelho> concelhos) {
        this.concelhos = concelhos;
    }
   
    public void addConcelho(Concelho concelho){
        concelho.setDistrito(this);
        concelhos.add(concelho);
    }
    // </editor-fold>

    public String toString() {
        return getNome();
    }
}


dont forget to rate if it was usefull

Read the chapter 6 of the book Java Persistence With Hibernate

--ms


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.