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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值