hibernate映射在我的代码中不起作用

时间:2015-10-20 13:59:40

标签: java spring hibernate

我有对象。资料,业余爱好,兴趣,用户

这些对象中的Relation是具有Profile one-one的用户。 与爱好和兴趣有一对多的关系。

以下代码: -

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;
    }


}

我正在编写一个测试类来测试服务层;

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);

}
}

它将数据保存在用户和个人资料中,但不会保留在Hobby and Interest中。

请帮助。

提前致谢.........

1 个答案:

答案 0 :(得分:3)

对于* ToMany操作,默认情况下不执行级联操作。因此,如果要保留或更新集合的条目,则必须指定注释的级联属性:

@OneToMany(cascade = CascadeType.ALL)

CascadeType.ALL将对集合执行所有操作(持久,更新,删除等),但您可以根据需要选择其他CascadeType。