-->
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.  [ 4 posts ] 
Author Message
 Post subject: mapping problem
PostPosted: Thu May 06, 2004 9:08 am 
Beginner
Beginner

Joined: Wed Mar 24, 2004 8:43 am
Posts: 42
hi


here i have a 3 tables, Product, Orders and OrderItems, one to many relation between Product and OrderItems and Order and OrderItems have also a one to many relation, so PK of the orderItem table is consist of PK's of other two tables.
so here i am inserting data into a OrderItem table, data are inserted successfully. and i deleted the one master table(Product) record that is in the OrderItem table record is successfully deleted from the master table and child table,but the problem is related Order record to the Product record in the child table is also deleted from the Order master table

help me to identify the error


here i have attched my mapping hbm.xml files.


order.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="test.hibernate.data.Order" table="orders">
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)"
not-null="true"/>
<generator class="uuid.hex"/>
</id>

<property name="onam">
<column name="oname" sql-type="char(32)" not-null="true"/>
</property>

<property name="date">
<column name="order_date" sql-type="date" not-null="true"/>
</property>

<property name="priceTotal">
<column name="price_total" sql-type="double" not-null="true"/>
</property>


<set name="orderItems" table="items" inverse="true" cascade="all" lazy="false" sort="unsorted" batch-size="1" outer-join="auto">
<key column="order_id" />
<one-to-many class="test.hibernate.data.OrderItem" />
</set>



</class>

</hibernate-mapping>


product.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="test.hibernate.data.Product" table="products">

<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<property name="name">
<column name="name" sql-type="char(32)" not-null="true"/>
</property>

<property name="price">
<column name="price" sql-type="double" not-null="true"/>
</property>

<property name="amount">
<column name="amount" sql-type="integer" not-null="true"/>
</property>

<set name="productItems" table="items" inverse="true" cascade="all-delete-orphan" lazy="false" sort="unsorted" batch-size="1" outer-join="auto">
<key column="product_id" />
<one-to-many class="test.hibernate.data.OrderItem" />
</set>

</class>

</hibernate-mapping>


orderitem.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="test.hibernate.data.OrderItem" table="items">

<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<property name="orderId" insert="false" update="false">
<column name="order_id" sql-type="char(32)" not-null="true"/>
</property>

<property name="productId" insert="false" update="false">
<column name="product_id" sql-type="char(32)" not-null="true"/>
</property>


<property name="amount">
<column name="amount" sql-type="integer" not-null="true"/>
</property>


<property name="price">
<column name="price" sql-type="double" not-null="true"/>
</property>

<many-to-one name="order" class="test.hibernate.data.Order" cascade="all" column="order_id" not-null="true" />
<many-to-one name="product" class="test.hibernate.data.Product" cascade="all" column="product_id" not-null="true" />

</class>

</hibernate-mapping>


thx
gimhan


Top
 Profile  
 
 Post subject: Re: mapping problem
PostPosted: Mon May 10, 2004 1:13 am 
Beginner
Beginner

Joined: Wed Mar 24, 2004 8:43 am
Posts: 42
gimhan90 wrote:
hi


here i have a 3 tables, Product, Orders and OrderItems, one to many relation between Product and OrderItems and Order and OrderItems have also a one to many relation, so PK of the orderItem table is consist of PK's of other two tables.
so here i am inserting data into a OrderItem table, data are inserted successfully. and i deleted the one master table(Product) record that is in the OrderItem table record is successfully deleted from the master table and child table,but the problem is related Order record to the Product record in the child table is also deleted from the Order master table

help me to identify the error


here i have attched my mapping hbm.xml files.


order.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="test.hibernate.data.Order" table="orders">
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)"
not-null="true"/>
<generator class="uuid.hex"/>
</id>

<property name="onam">
<column name="oname" sql-type="char(32)" not-null="true"/>
</property>

<property name="date">
<column name="order_date" sql-type="date" not-null="true"/>
</property>

<property name="priceTotal">
<column name="price_total" sql-type="double" not-null="true"/>
</property>


<set name="orderItems" table="items" inverse="true" cascade="all" lazy="false" sort="unsorted" batch-size="1" outer-join="auto">
<key column="order_id" />
<one-to-many class="test.hibernate.data.OrderItem" />
</set>



</class>

</hibernate-mapping>


product.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="test.hibernate.data.Product" table="products">

<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<property name="name">
<column name="name" sql-type="char(32)" not-null="true"/>
</property>

<property name="price">
<column name="price" sql-type="double" not-null="true"/>
</property>

<property name="amount">
<column name="amount" sql-type="integer" not-null="true"/>
</property>

<set name="productItems" table="items" inverse="true" cascade="all-delete-orphan" lazy="false" sort="unsorted" batch-size="1" outer-join="auto">
<key column="product_id" />
<one-to-many class="test.hibernate.data.OrderItem" />
</set>

</class>

</hibernate-mapping>


orderitem.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="test.hibernate.data.OrderItem" table="items">

<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<property name="orderId" insert="false" update="false">
<column name="order_id" sql-type="char(32)" not-null="true"/>
</property>

<property name="productId" insert="false" update="false">
<column name="product_id" sql-type="char(32)" not-null="true"/>
</property>


<property name="amount">
<column name="amount" sql-type="integer" not-null="true"/>
</property>


<property name="price">
<column name="price" sql-type="double" not-null="true"/>
</property>

<many-to-one name="order" class="test.hibernate.data.Order" column="order_id" not-null="true"
/>
<many-to-one name="product" class="test.hibernate.data.Product" column="product_id" not-null="true" />
</class>

</hibernate-mapping>



just remove cascade="all " from orderitem.hbm.xml


thx
gimhan


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 3:39 pm 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
Hi,

I'm trying out the composite pattern example from the Hibernate site (http://hibernate.org/86.html).

Following errors occured:

1/
Configuring using the defaultCache settings.
java.lang.ClassCastException
at net.sf.hibernate.type.SetType.wrap(SetType.java:24)

2/
ERROR SessionImpl:2368 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
...
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
...


Here is the code I used to test the composite pattern:


Code:
public class TestKlasse {
   
    private SessionFactory sessions;
   
   public void configure() throws HibernateException {
       sessions = new Configuration()
       .addClass(Site.class)
       .setProperty(Environment.HBM2DDL_AUTO, "create")
       .buildSessionFactory();
   }   
   
   public static void main(String[] args) throws HibernateException {
      TestKlasse T = new TestKlasse();
      Collection l = new ArrayList();
      Site site = new Site(null,null,"test1","test2","test3");
      Site child1 = new Site(null, site,"test1","test1","test1");
      Site child2 = new Site(null, site,"test2","test2","test2");
      l.add(child1);
      l.add(child2);
      site.setChildrenSites((Collection)l);
   
   
      T.configure();  //
       Session session = T.sessions.openSession();
       Transaction tx = null;
       try {
           tx = session.beginTransaction();
           session.save(site);
           tx.commit();
       }
       catch (HibernateException he) {
           if (tx!=null) tx.rollback();
           throw he;
       }
       finally {
           session.close();
       }
   }
}


Persistent class:

Code:
public class Site {
   private Integer id;
   private long version;
   private Collection childrenSites;
   private Site parent;
   private String name;
   private String description;
   private String namespace;

   public Site(){
   }
   
   public Site(Collection childrenSites, Site parent, String name, String description, String namespace) {
      this.childrenSites=childrenSites;
      this.parent=parent;
      this.name=name;
      this.description=description;
      this.namespace=namespace;
   }
...


XML mapping as mentioned at the Hibernate site.

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

<hibernate-mapping>
<class name="Site" table="Site">
<id name="id" type="java.lang.Integer" unsaved-value="null">
<generator class="native"/>
</id>

<version name="version" type="long"/>
<set name="childrenSites" inverse="true" lazy="true" cascade="save-update">
<key column="parent"/>
<one-to-many class="Site"/>
</set>
<many-to-one name="parent" column="parent" cascade="save-update" class="Site"/>
<property name="description" type="java.lang.String"/>
<property name="namespace" type="java.lang.String"/>
<property name="name" type="java.lang.String"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 3:45 pm 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
Sorry for the last reply, I posted here by mistake.


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