-->
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: Confusion on cascade=all
PostPosted: Tue May 28, 2013 4:20 pm 
Newbie

Joined: Tue May 28, 2013 4:13 pm
Posts: 1
I have confusion on concept of cascading in hibernate.Why isn't it saving the car automatically,While it is expected to be done on cascading?



Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="hibernateoobdemo.pojos.Car" table="Car">
        <id name="carno" column="carnum" type="integer">
            <generator class="native"></generator>
        </id>
        <property name="carname" type="string" column="carname"></property>
        <many-to-one name="person" class="hibernateoobdemo.pojos.Person" cascade="all" unique="true"></many-to-one>
    </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="hibernateoobdemo.pojos.Person" table="Person">
        <id name="code" type="integer">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="name"></property>
        <one-to-one name="car" class="hibernateoobdemo.pojos.Car" property-ref="person"></one-to-one>
    </class>
</hibernate-mapping>


Code:
package hibernateoobdemo;

import org.hibernate.*;
import org.hibernate.cfg.*;
import hibernateoobdemo.pojos.*;

public class Hibernateoobdemo
{

    public static void main(String[] args)
    {
        SessionFactory sf=new Configuration().configure().buildSessionFactory();
        Session session=sf.openSession();
        Transaction t=session.beginTransaction();
        t.begin();
        Car car=new Car();
        car.setCarname("AUDI");
       
        Person person=new Person();
        person.setName("Ravi");
        car.setPerson(person);
        person.setCar(car);
        session.save(person);
       
        t.commit();
        session.close();
    }
}


Code:

package hibernateoobdemo.pojos;

public class Car
{
    private int carno;
    private String carname;
    private Person person;

    public int getCarno() {
        return carno;
    }

    public String getCarname() {
        return carname;
    }

    public Person getPerson() {
        return person;
    }

    public void setCarno(int carno) {
        this.carno = carno;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
   
}


Code:

package hibernateoobdemo.pojos;

public class Person
{
    private int code;
    private String name;
    private Car car;

    public int getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public Car getCar() {
        return car;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCar(Car car) {
        this.car = car;
    }
   
   
   
}


Top
 Profile  
 
 Post subject: Re: Confusion on cascade=all
PostPosted: Wed May 29, 2013 5:41 pm 
Newbie

Joined: Tue May 28, 2013 9:25 am
Posts: 2
your cascade-all at the person of a car is not relevant for saving the car assigned to the Person in your example. it defines, that if you do sth on a car, that this action will be cascaded to the related Person. So if you you change your example to save the car instead of the person, the person will bei saved too.
To make your example work the way you want it to, add the cascading to the onetoone-Definition.


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.