BIDIRECTIONAL @ONETOMANY / @MANYTOONE ASSOCIATION
来源:互联网 发布:linux配置ant 编辑:程序博客网 时间:2024/05/21 06:47
One of goals the in programming is representation of models from the real world. Very often an application needs to model some relationship between entities. In the last article about Hibernate associations I described the rules of setting up a “one to one” relationship. Today I’m going to show you how to setup a bidirectional “one to many” and “many to one” association. This example will be based on previous Hibernate tutorials.
At the start I need to say that my code example will be based on a simple situation. Let’s imagine a football league. Every league has teams, and in the team can play some players. So the summary is following: one team has many players, one player can play for one team. In this way we get obvious “one to many” and “many to one” relationships.
I use MySQL as a database in this example. Here are scripts for the table’s creation:
- CREATE TABLE `teams` (
- `id` int(6) NOT NULL AUTO_INCREMENT,
- `name` varchar(20) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
- CREATE TABLE `players` (
- `id` int(6) NOT NULL AUTO_INCREMENT,
- `lastname` varchar(20) NOT NULL,
- `team_id` int(6) NOT NULL,
- PRIMARY KEY (`id`),
- KEY `player's team` (`team_id`),
- CONSTRAINT `player's team` FOREIGN KEY (`team_id`) REFERENCES `teams` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
- ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
The next step is creation of POJOs:
- 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(String name) {
- this.name = name;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set<player> getPlayers() {
- return players;
- }
- public void setPlayers(Set<player> players) {
- this.players = players;
- }
- }
- </player></player></player>
I have used @OneToMany because one team can have many players. In the next POJO, the association will be @ManyToOne since many players can play for one team.
- 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(String lastname) {
- this.lastname = lastname;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getLastname() {
- return lastname;
- }
- public void setLastname(String lastname) {
- this.lastname = lastname;
- }
- public Team getTeam() {
- return team;
- }
- public void setTeam(Team team) {
- this.team = team;
- }
- }
Here I specify the column (team_id) which will be joined from the owning side (Teams). Notice that I don’t declare team_id field in the POJO. If I need to change a team for a player I just need to use setTeam(Team team) setter.
After POJOs were declared, I can demonstrate how to persist them:
- ...
- public static void main(String[] args) {
- SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- 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();
- session.close();
- }
- ...
- </player></player>
The result of the code execution is:
Hibernate: insert into teams (name) values (?)
Hibernate: insert into players (lastname, team_id) values (?, ?)
Hibernate: insert into players (lastname, team_id) values (?, ?)
That’s it, in this tutorial I have demonstrated how to setup “one to many” and “many to one” bidirectional association. I don’t see any sense in the same tutorial with an example of unidirectional association. Because Hibernate has its own best practices:
Unidirectional associations are more difficult to query. In a large application, almost all associations must be navigable in both directions in queries.
- Bidirectional @OneToMany / @ManyToOne association
- BIDIRECTIONAL @ONETOMANY / @MANYTOONE ASSOCIATION
- @ManyToOne @OneToMany
- Hibernet OneToMany ManyToOne
- hibernate注解onetomany manytoone
- Hibernate @OneToMany & @ManyToOne
- @ManyToOne注解 与 @OneToMany
- @OneToMany与@ManyToOne
- @ManyToOne和@OneToMany 注解
- OneToMany/ManyToOne关联
- @ManyToOne与@OneToMany
- Hibernate-----@OneToMany VS @ManyToOne
- @ManyToOne和@OneToMany 注解
- Jpa onetomany或者manytoone
- @ManyToOne @OneToMany @JoinColumn
- @ManyToOne和@OneToMany
- manytoone 和onetomany hibernate配置
- @OneToMany、@ManyToOne以及@ManyToMany讲解
- C#开发中IrisSkin2.dll如图添加到工具箱
- CSS中margin和padding的区别
- Jackson 框架,轻易转换JSON
- acm 3 1010 走台阶
- java 面试总结(都是亲身面试的经历)
- BIDIRECTIONAL @ONETOMANY / @MANYTOONE ASSOCIATION
- Android App图标尺寸
- kafka
- java-模拟单链表实现
- acm 3 1011 有只小蜜蜂
- HQL数据查询基础(二)
- 知道这20个正则表达式,能让你少写1,000行代码
- ACM probloms->1003
- C++中substr函数的用法