2015-10-20 46 views
1

我有对象。个人资料,爱好,兴趣,用户。休眠映射不能在我的代码中工作

这些对象中的关系是具有Profile 1的用户。 和Profile与Hobby和Interest有一对多关系。

下面的代码,并提供: -

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

} 
} 

它将数据保留在用户和配置文件中,但不包含在兴趣和兴趣中。

请帮忙。

在此先感谢.........

回答

3

对于没有级联操作执行默认*一对多的操作。所以,如果你想坚持或更新馆藏的条目,您必须指定注解的cascade属性:

@OneToMany(cascade = CascadeType.ALL) 

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

+0

感谢您的答复。它的工作原理。但它仍然不会在关系表中保留数据,如profile_hobby和profile_interest。请建议。 –