springmvc4+hibernate4+spring4注解一对多级联保存

package com.h3c.zgc.user.entity;



import java.util.HashSet;

import java.util.Set;



import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.OneToMany;

import javax.persistence.Table;



import org.hibernate.annotations.Cascade;

import org.hibernate.annotations.CascadeType;

import org.hibernate.annotations.GenericGenerator;



@Entity

@Table(name="t_person",catalog="itac")

public class Person {

    @Id

    @GenericGenerator(name="generator",strategy="increment")

    @GeneratedValue(generator="generator")

    @Column(name="person_id",unique=true,nullable=false)

    private Integer personId;

    @Column(name="person_name")

    private String personName;

    

    @OneToMany(targetEntity=House.class)

    @JoinColumn(name="person_id",updatable =false)

    @Cascade(CascadeType.SAVE_UPDATE)

    private Set<House> houses = new HashSet<House>();

    

    public Set<House> getHouses() {

        return houses;

    }

    public void setHouses(Set<House> houses) {

        this.houses = houses;

    }

    public Integer getPersonId() {

        return personId;

    }

    public void setPersonId(Integer personId) {

        this.personId = personId;

    }

    public String getPersonName() {

        return personName;

    }

    public void setPersonName(String personName) {

        this.personName = personName;

    }

    @Override

    public String toString() {

        return "Person [personId=" + personId + ", personName=" + personName

                + ", houses=" + houses + "]";

    }

    

}

 

package com.h3c.zgc.user.entity;



import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.ManyToOne;

import javax.persistence.Table;



import org.hibernate.annotations.Cascade;

import org.hibernate.annotations.CascadeType;

import org.hibernate.annotations.GenericGenerator;





@Entity

@Table(name="t_house",catalog="itac")

public class House {

    @Id

    @GeneratedValue(generator="generator")

    @GenericGenerator(name="generator",strategy="increment")

    @Column(name="house_id",unique=true,nullable=false)

    private Integer houserId;

    @Column(name="house_address")

    private String houseAddress;

    

    @ManyToOne(targetEntity=Person.class)

    @JoinColumn(name="person_id",updatable=false)

    @Cascade(CascadeType.SAVE_UPDATE)

    private Person person;

    

    public Person getPerson() {

        return person;

    }

    public void setPerson(Person person) {

        this.person = person;

    }

    public Integer getHouserId() {

        return houserId;

    }

    public void setHouserId(Integer houserId) {

        this.houserId = houserId;

    }

    public String getHouseAddress() {

        return houseAddress;

    }

    public void setHouseAddress(String houseAddress) {

        this.houseAddress = houseAddress;

    }

    @Override

    public String toString() {

        return "House [houserId=" + houserId + ", houseAddress=" + houseAddress

                + "]";

    }  

}

dao

package com.h3c.zgc.user.dao;



import java.util.HashSet;

import java.util.Set;



import javax.annotation.Resource;



import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import org.springframework.stereotype.Repository;



import com.h3c.zgc.user.entity.House;

import com.h3c.zgc.user.entity.Person;



@Repository

public class PersonDao extends HibernateDaoSupport{

    @Resource

    private void set(SessionFactory sessionFactory){

        this.setSessionFactory(sessionFactory);

    }

    public void savePerson(){

        Person p = new Person();

        p.setPersonName("person01");

        this.getHibernateTemplate().save(p);

    }

    public void savePersonAndHouse(){

        Person p = new Person();

        p.setPersonName("person01");

        

        House h = new House();

        h.setHouseAddress("beijing");

        

        Set<House> hs = new HashSet<House>();

        hs.add(h);

        

        //建立关系

        p.setHouses(hs);

        h.setPerson(p);

        this.getHibernateTemplate().save(p);

        System.out.println(p);

    }

}

service

package com.h3c.zgc.user.service;



import javax.annotation.Resource;



import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;



import com.h3c.zgc.user.dao.PersonDao;

@Service

public class PersonService {

    @Resource

    private PersonDao personDao;

    @Transactional

    public void savePerson() {

        this.personDao.savePerson();

    }

    @Transactional

    public void savePersonAndHouse() {

        this.personDao.savePersonAndHouse();

    }

}

controller

package com.h3c.zgc.user.controller;



import java.io.IOException;



import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;



import com.h3c.zgc.user.service.PersonService;

@Controller

public class PersonController {

    @Resource

    private PersonService personService;

    @RequestMapping("savePerson")

    public String savePerson(HttpServletRequest request){

        this.personService.savePerson();

        request.setAttribute("flag", "success");

        return "h4/flag";

    }

    @RequestMapping("savePersonHouse")

    public String savePersonHouse(HttpServletRequest request){

        this.personService.savePersonAndHouse();

        request.setAttribute("flag", "success");

        return "h4/flag";

    }

    

    

    

    @RequestMapping("test0")

    public void test(HttpServletResponse response) throws IOException{

        String strs = "[{ title: '中国好声音'},{ title: '中国饮料'},{ title: '中国好舞蹈'}]";

        response.getWriter().write(strs);

    }

}

 

访问路径

http://localhost:8080/zgc/savePersonHouse

你可能感兴趣的:(Hibernate4)