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 classPerson {@Id @GenericGenerator (name="Generator", strategy="Increment") @GeneratedValue (generator="Generator") @Column (name="person_id", unique=true, nullable=false) PrivateInteger personId; @Column (Name="Person_name") PrivateString PersonName; @OneToMany (targetentity=house.class) @JoinColumn (name="person_id", updatable =false) @Cascade (cascadetype.save_update)PrivateSetNewHashset(); PublicSetgethouses () {returnhouses; } Public voidSethouses (sethouses) { This. Houses =houses; } PublicInteger Getpersonid () {returnpersonId; } Public voidSetpersonid (Integer personId) { This. PersonId =personId; } PublicString Getpersonname () {returnPersonName; } Public voidsetpersonname (String personname) { This. PersonName =PersonName; } @Override PublicString 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 classHouse {@Id @GeneratedValue (generator="Generator") @GenericGenerator (name="Generator", strategy="Increment") @Column (name="house_id", unique=true, nullable=false) PrivateInteger Houserid; @Column (Name="house_address") PrivateString houseaddress; @ManyToOne (targetentity=person.class) @JoinColumn (name="person_id", updatable=false) @Cascade (cascadetype.save_update)Privateperson person ; PublicPerson Getperson () {returnPerson ; } Public voidSetperson (person person) { This. person =Person ; } PublicInteger Gethouserid () {returnHouserid; } Public voidSethouserid (Integer houserid) { This. Houserid =Houserid; } PublicString gethouseaddress () {returnhouseaddress; } Public voidsethouseaddress (String houseaddress) { This. houseaddress =houseaddress; } @Override PublicString 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 classPersondao extends hibernatedaosupport{@ResourcePrivate void Set(Sessionfactory sessionfactory) { This. Setsessionfactory (sessionfactory); } Public voidSaveperson () {person P=NewPerson (); P.setpersonname ("Person01"); This. Gethibernatetemplate (). Save (P); } Public voidSavepersonandhouse () {person P=NewPerson (); P.setpersonname ("Person01"); House h=NewHouse (); H.sethouseaddress ("Beijing"); Set<House> HS =NewHashset(); Hs.add (h); //Building Relationshipsp.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 classPersonservice {@ResourcePrivatePersondao Persondao; @Transactional Public voidSaveperson () { This. Persondao.saveperson (); } @Transactional Public voidSavepersonandhouse () { 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 classPersonController {@ResourcePrivatePersonservice Personservice; @RequestMapping ("Saveperson") PublicString Saveperson (httpservletrequest request) { This. Personservice.saveperson (); Request.setattribute ("Flag","Success"); return "H4/flag"; } @RequestMapping ("Savepersonhouse") PublicString Savepersonhouse (httpservletrequest request) { This. Personservice.savepersonandhouse (); Request.setattribute ("Flag","Success"); return "H4/flag"; } @RequestMapping ("test0") Public voidTest (HttpServletResponse response) throws ioexception{String STRs="[{title: ' Chinese good Voice '},{title: ' Chinese Drink '},{title: ' Chinese Good Dance '}]"; Response.getwriter (). write (STRs); }}
Access path
Http://localhost:8080/zgc/savePersonHouse
Springmvc4+hibernate4+spring4 annotations to a pair of multilevel saves