hibernate4,onetomany

项目搭建使用spring3 + hibernate4 + spring mvc 几个框架

来说说hibernate4onetomany

onetomany:应用场景如一个团队有很多玩家。
贴代码:

import java.util.Set;
import javax.persistence.*;
@Entity
@Table(name = "teams")
public class Team {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;    
    @OneToMany(mappedBy="team", cascade=CascadeType.ALL)
    private Set<Player> players;
    public Team(){}
    public Team(String name) {
        this.name = name;
    }
        //省掉多余的get、set方法
    public Set<Player> getPlayers() {
        return players;
    }

    public void setPlayers(Set<Player> players) {
        this.players = players;
    }
}
import javax.persistence.*;
@Entity
@Table(name = "players")
public class Player {
    @Id
    @GeneratedValue
    private Integer id;
    private String lastname;
    @ManyToOne
    @JoinColumn(name = "team_id")
    private Team team;
    public Player(){}
       //省掉get、set方法
    public Player(String lastname) {
        this.lastname = lastname;
    }
    public Team getTeam() {
        return team;
    }
    public void setTeam(Team team) {
        this.team = team;
    }
}

Test测试类,这里没有写单元测试,哪位哥们有时间,可以把单元测试贴出来:

测试类里面session是open的,取完级联数据以后,才关闭。在web调用后台的时候,session在dao层进行关闭,controller层再取级联数据就会报出session已关闭的错误,我百度,goolge后,网上都说配置spring的请求过滤器,在请求时候把session来打开,但我试了,没测试通过。如果哪位大神找到解决办法,请不吝赐教。在这里谢谢了。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

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

import com.ecoinfo.teaching.dao.FunTreeDao;
import com.ecoinfo.teaching.dao.UserDao;
import com.ecoinfo.teaching.vo.Article;
import com.ecoinfo.teaching.vo.FunTree;
import com.ecoinfo.teaching.vo.Player;
import com.ecoinfo.teaching.vo.Team;
import com.ecoinfo.teaching.vo.Users;
import com.sun.mail.handlers.text_xml;

/**
 * 测试保存一对多 多对多
 * @author 
 *
 */
@Repository
public class TestSaveObj {

    @Autowired
    private FunTreeDao funTreeDao;
    @Autowired
    private UserDao userDao;
    public void setFunTreeDao(FunTreeDao funTreeDao) {
        this.funTreeDao = funTreeDao;
    }
    public void setUsrDao(UserDao userDao) {
        this.userDao = userDao;
    }
    /**
     * 模拟数据
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
        SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        System.out.println("程序开始执行.......");
        //onetoMany
        //addByOneToMany(session);
        //getByOneToMany(session);
        //deleteByOneToMany(session);
        //updateByOneToMany(session);
        session.close();
        System.out.println("程序结束执行.......");
        
    }
    /**
     * 一对多演示 add
     */
    public static void addByOneToMany(Session session){
        Team team = new Team("Barcelona");
        Set<Player> players = new HashSet<Player>();
        Player p1 = new Player("Messi");
        Player p2 = new Player("Xavi");
        p1.setTeam(team);
        p2.setTeam(team);
        players.add(p1);
        players.add(p2);
        team.setPlayers(players);
        session.save(team);
        session.getTransaction().commit();
    }
    public static void getByOneToMany(Session session){
        Team team = (Team) session.get(Team.class, 7);
        //可获得查询对象的内容
        System.out.println("team:"+team.getName());
        Iterator<Player> it = team.getPlayers().iterator();
        while(it.hasNext()){
            Player player = (Player)it.next();
            System.out.println(player.getLastname());
        }
    }
    public static void deleteByOneToMany(Session session){
        Team team = (Team) session.get(Team.class, 7);
        System.out.println(team.getName()+"对应集合大小:"+team.getPlayers().size());
        session.delete(team);
        session.getTransaction().commit();
    }
    public static void updateByOneToMany(Session session){
        Team team = new Team();
        team.setId(7);
        team.setName("updateBarcelona");
        session.update(team);
        session.getTransaction().commit();
    }

以下是team和players表结构:

CREATE TABLE `teams` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
CREATE TABLE `players` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `lastname` varchar(45) NOT NULL,
  `team_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `FK_players_1` (`team_id`),
  CONSTRAINT `FK_players_1` FOREIGN KEY (`team_id`) REFERENCES `teams` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
数据库用的mysql

 

 

 

posted @ 2013-09-10 17:30  SongPingping  阅读(418)  评论(1编辑  收藏  举报