码迷,mamicode.com
首页 > Web开发 > 详细

hibernate关于多对多注解配置

时间:2017-02-16 14:10:31      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:ted   ping   tga   layer   需要   div   any   关系   cas   

Game实体类配置关系

@Entity
@Table(name = "game")
public class Game {
    @Id
    @GeneratedValue
    private Integer gid;
    private String gname;
    @ManyToMany(mappedBy = "games",cascade = CascadeType.ALL)
    private Set<Player> players=new HashSet<Player>();

    public Integer getGid() {
        return gid;
    }

    public void setGid(Integer gid) {
        this.gid = gid;
    }

    public String getGname() {
        return gname;
    }

    public void setGname(String gname) {
        this.gname = gname;
    }

    public Set<Player> getPlayers() {
        return players;
    }

    public void setPlayers(Set<Player> players) {
        this.players = players;
    }
}

player实体类配置关系

@Entity
@Table(name = "player")
public class Player {
    @Id
    @GeneratedValue
    private Integer pid;
    private String pname;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "pg",joinColumns = @JoinColumn(name = "pid"),inverseJoinColumns = @JoinColumn(name = "gid"))
    private Set<Game> games=new HashSet<Game>();

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public Set<Game> getGames() {
        return games;
    }

    public void setGames(Set<Game> games) {
        this.games = games;
    }
}

hibernate.cfg.xml需要配置关联实体类路径

<mapping class="cn.wyd.a.Game"/>
		<mapping class="cn.wyd.a.Player"/>

测试类测试数据

 @Test
    public void test1() {
        Game game1 = new Game();
        Game game2 = new Game();
        game1.setGname("g1");
        game2.setGname("g2");
        Player player = new Player();
        player.setPname("123");
        player.getGames().add(game1);
        player.getGames().add(game2);
        game1.getPlayers().add(player);
        game2.getPlayers().add(player);
        session.save(player);
    }

  

hibernate关于多对多注解配置

标签:ted   ping   tga   layer   需要   div   any   关系   cas   

原文地址:http://www.cnblogs.com/wyd12138/p/6405099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!