Hibernate双向多对一关系
来源:互联网 发布:手机ip更改软件 编辑:程序博客网 时间:2024/05/14 07:40
Hibernate双向多对一关系
废话不多说,先说说这两个对象。
帐号,就是用户了;相册,当然是用户的相册。用户与相册是一对多关系,反之,相册与用户是多对一关系。现在我们看两个对象的代码。
Account
要注意@OneToMany中的cascade,还有@JoinColumn中的updatable
Album
同样注意@ManyToOne中的cascade,与Account中保持一致。
由此生成的sql语句:
在执行新建用户的时候,同时新建相册,如果没有设定级联,会出save the transient instance before flushing异常
生成的执行语句
如果 Account中 @JoinColumn(name = "account_id", updatable = true)的updatable设置为false,则不会有 Hibernate: update album set account_id=? where album_id=? 执行,也就是说级联操作时不会做更新操作。
废话不多说,先说说这两个对象。
帐号,就是用户了;相册,当然是用户的相册。用户与相册是一对多关系,反之,相册与用户是多对一关系。现在我们看两个对象的代码。
Account
- import java.io.Serializable;
- import java.util.Date;
- import java.util.LinkedList;
- import java.util.List;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
- import org.hibernate.annotations.GenericGenerator;
- @Entity
- @Table(name = "account")
- public class Account implements Serializable {
- @Id
- @GeneratedValue(generator = "hilo")
- @GenericGenerator(name = "hilo", strategy = "hilo")
- @Column(name = "account_id")
- private long id;// 主键
- @Column(name = "name", nullable = false, unique = true)
- private String name; // 用户名
- @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Album.class)
- @JoinColumn(name = "account_id", updatable = true)
- private List<Album> albumList = new LinkedList<Album>(); // 相册
- }
要注意@OneToMany中的cascade,还有@JoinColumn中的updatable
Album
- import java.io.Serializable;
- import java.util.Date;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- import org.hibernate.annotations.Cache;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
- import org.hibernate.annotations.GenericGenerator;
- @Entity
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- @Table(name = "album")
- public class Album implements Serializable {
- private static final long serialVersionUID = -159468065798255466L;
- @Id
- @GeneratedValue(generator = "hilo")
- @GenericGenerator(name = "hilo", strategy = "hilo")
- @Column(name = "album_id")
- private long id;
- @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Account.class)
- @JoinColumn(name = "account_id")
- private Account account;
- }
同样注意@ManyToOne中的cascade,与Account中保持一致。
由此生成的sql语句:
- alter table album drop foreign key FK5897E6F68D52691
- drop table if exists account
- drop table if exists album
- drop table if exists hibernate_unique_key
- create table account (account_id bigint not null, email varchar(255) not null unique, enabled bit not null, gender bit not null, sign_in_date datetime, name varchar(255) not null unique, password varchar(255) not null, point integer not null, sign varchar(255), sign_up_date datetime not null, primary key (account_id))
- create table album (album_id bigint not null, create_date datetime not null, url varchar(255) not null unique, account_id bigint, primary key (album_id))
- alter table album add index FK5897E6F68D52691 (account_id), add constraint FK5897E6F68D52691 foreign key (account_id) references account (account_id)
- CREATE TABLE hibernate_unique_key (next_hi int(10) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (next_hi))
- INSERT INTO hibernate_unique_key(next_hi) VALUES(1)
在执行新建用户的时候,同时新建相册,如果没有设定级联,会出save the transient instance before flushing异常
- Account a = new Account("wolf" + System.currentTimeMillis(), "snow");
- a.setEmail("snowolf@wolf.org" + System.currentTimeMillis());
- a.setGender(true);
- // 执行新建用户
- long id = accountService.signUp(a);
- List<Album> list = new LinkedList<Album>();
- list.add(new Album("" + System.currentTimeMillis()));
- a.setAlbumList(list);
- // 更新相册
- accountService.update(a);
生成的执行语句
- Hibernate: insert into account (email, enabled, gender, sign_in_date, name, password, point, sign, sign_up_date, account_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
- Hibernate: insert into album (account_id, create_date, url, album_id) values (?, ?, ?, ?)
- Hibernate: update album set account_id=? where album_id=?
如果 Account中 @JoinColumn(name = "account_id", updatable = true)的updatable设置为false,则不会有 Hibernate: update album set account_id=? where album_id=? 执行,也就是说级联操作时不会做更新操作。
0 0
- Hibernate双向多对一关系
- hibernate双向多对一映射关系
- Hibernate双向多对一关系
- Hibernate HelloWorld-07 双向多对一的映射关系
- Hibernate多对一、双向关系、控制反转、级联配置
- 双向多对一映射关系
- hibernate映射关系 一对多、多对一单向关联 一对多双向关联
- Hibernate关联关系映射(单向一对多、单向多对一、双向一对多)
- Hibernate 单向多对一、单向一对多、双向一对多关联关系详解
- Hibernate关联关系映射-----双向一对多/多对一映射配置
- Hibernate关联关系配置-----基于连接表的双向一对多/多对一映射配置
- Hibernate关联关系映射-----双向一对多/多对一映射配置
- hibernate 关联映射:多对一(一对多)双向关联关系
- Hibernate 关系映射 —— 双向(一对多)多对一
- hibernate关系映射之单向多对一和双向一对多
- hibernate-关系映射一对多(多对一)双向关联)-10
- Hibernate(四)一对多映射 补充三:双向多对一关系(学生-->班级)
- hibernate实现非主键映射外键双向多对一关系
- Matlab:如何查找给定目录下的文件
- matlab获取文件夹中所有图像的方法
- Android 几种屏幕间跳转的跳转Intent Bundle
- 【opencv小应用】手势动态识别(简单实现)
- 国产InitPHP框架系列 - InitPHP框架搭建高可用WEB应用03:模板View使用
- Hibernate双向多对一关系
- 安卓锁屏的实现
- msp430端口操作
- Tomcat Server.xml配置文件i
- iOS开发中使用OCUnit进行单元测试
- ZOJ-3202
- 接口
- Improving SQL Server Performance
- centos_samba(这个好)