1

I am having objects. Profile,Hobby,Interest,User.

The Relation in these objects are user with Profile one-one. and Profile having relation one to many with Hobby and Interest.

the code giving below:--

package com.dineshonjava.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;


    @Entity
    @Table(name="User")
    public class User {
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name = "userId")
        private int iId;
        @Column(name = "NAME")
        private String sName;
        @Column(name = "PASS")
        private String sPass;
        @OneToOne(cascade = CascadeType.ALL,fetch=FetchType.LAZY)
        @JoinColumn(name="ProfileId")
        private Profile oSingleProfile;


        /**
         * @return the oSingleProfile
         */
        public Profile getoSingleProfile() {
            return oSingleProfile;
        }

        /**
         * @param oSingleProfile the oSingleProfile to set
         */
        public void setoSingleProfile(Profile oSingleProfile) {
            this.oSingleProfile = oSingleProfile;
        }

        public User(String sName, String sPass) {
            super();
            this.sName = sName;
            this.sPass = sPass;
        }

        public User() {
            super();
            // TODO Auto-generated constructor stub
        }

        /**
         * @return the sName
         */
        public String getsName() {
            return sName;
        }
        /**
         * @param sName the sName to set
         */
        public void setsName(String sName) {
            this.sName = sName;
        }
        /**
         * @return the sPass
         */
        public String getsPass() {
            return sPass;
        }
        /**
         * @param sPass the sPass to set
         */
        public void setsPass(String sPass) {
            this.sPass = sPass;
        }


    }
package com.dineshonjava.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="Profile")
public class Profile {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "Id")
    private int iId;
    @Column(name = "Address")
    private String Address;
    @Column(name = "Zip")
    private int iZip;   
    @OneToMany
    private Collection<Hobby> lstHobby=new ArrayList<Hobby>();
    @OneToMany  
    private Collection<Interest> lstInterest=new ArrayList<Interest>();


    public Profile() {
        super();        
    }


    public Profile(String address, int iZip, Collection<Hobby> lstHobby,
            Collection<Interest> lstInterest) {
        super();
        Address = address;
        this.iZip = iZip;
        this.lstHobby = lstHobby;
        this.lstInterest = lstInterest;
    }


    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

    public int getiZip() {
        return iZip;
    }

    public void setiZip(int iZip) {
        this.iZip = iZip;
    }


    /**
     * @return the lstHobby
     */
    public Collection<Hobby> getLstHobby() {
        return lstHobby;
    }


    /**
     * @param lstHobby the lstHobby to set
     */
    public void setLstHobby(Collection<Hobby> lstHobby) {
        this.lstHobby = lstHobby;
    }


    /**
     * @return the lstInterest
     */
    public Collection<Interest> getLstInterest() {
        return lstInterest;
    }


    /**
     * @param lstInterest the lstInterest to set
     */
    public void setLstInterest(Collection<Interest> lstInterest) {
        this.lstInterest = lstInterest;
    }

}





  package com.dineshonjava.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="Hobby")
public class Hobby {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id")
    private int iId;
    @Column(name = "HobbyName")
    private String sHobbyName;  


    public Hobby(){
        super();
        // TODO Auto-generated constructor stub
    }

    public String getsHobbyName() {
        return sHobbyName;
    }

    public Hobby(String sHobbyName) {
        super();
        this.sHobbyName = sHobbyName;
    }
    /**
     * @param sHobbyName the sHobbyName to set
     */
    public void setsHobbyName(String sHobbyName) {
        this.sHobbyName = sHobbyName;
    }


}

and i am Writing a test class to test the service layer;

package com.dineshonjava.tester;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dineshonjava.model.Hobby;
import com.dineshonjava.model.Interest;
import com.dineshonjava.model.Profile;
import com.dineshonjava.model.User;
import com.dineshonjava.service.UserService;



public class ServiceTest {  

    private static SessionFactory sessionFactory;

    public static void main(String[] args) {        
        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
                new String[] {"config/sdnext-servlet.xml"});        
        UserService userservice=(UserService)appContext.getBean("UserServiceImpl"); 

        List<Hobby> lsthobby=new ArrayList<Hobby>();
        List<Interest> lstinterest=new ArrayList<Interest>();   



        Profile p=new Profile();
        p.setAddress("fdsf");
        p.setiZip(11121);


        Hobby h1=new Hobby();
        h1.setsHobbyName("hobby1");
        h1.setsHobbyName("hobby2");

        Hobby h2=new Hobby();
        h2.setsHobbyName("hobby3");
        h2.setsHobbyName("hobby4");

        Interest int1=new Interest();
        int1.setsInterestName("interest1");
        int1.setsInterestName("interest2");

        lsthobby.add(h1);
        lsthobby.add(h2);
        lstinterest.add(int1);




        p.getLstHobby().add(h2);
        p.getLstHobby().add(h1);
        p.getLstInterest().add(int1);

        p.getLstHobby().add(h1);
        p.getLstHobby().add(h2);


        User user2=new User();
        user2.setsName("Prabhat2");
        user2.setsPass("prabhat4");
        user2.setoSingleProfile(p);


        Boolean ck=userservice.addUser(user2);

}
}

it is persisting the data in User and Profile but not in Hobby and Interest.

Please Help.

Thanks in advance.........

1 Answer 1

3

For *ToMany operations by default no cascade operations are executed. So if you want to persist or update the entries of the collections, you have to specify the cascade attribute of the annotation:

@OneToMany(cascade = CascadeType.ALL)

CascadeType.ALL will execute all operations on the collection (persist, update, delete etc.), but you can choose other CascadeTypes according to your requirements.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply.It works.However still it is not persisting data in relational tables like profile_hobby and profile_interest. Please suggest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.