Hibernate双向多对一关系

来源:互联网 发布:手机ip更改软件 编辑:程序博客网 时间:2024/05/14 07:40
Hibernate双向多对一关系 
废话不多说,先说说这两个对象。 
帐号,就是用户了;相册,当然是用户的相册。用户与相册是一对多关系,反之,相册与用户是多对一关系。现在我们看两个对象的代码。 
Account 
Java代码  收藏代码
  1. import java.io.Serializable;  
  2. import java.util.Date;  
  3. import java.util.LinkedList;  
  4. import java.util.List;  
  5.   
  6. import javax.persistence.CascadeType;  
  7. import javax.persistence.Column;  
  8. import javax.persistence.Entity;  
  9. import javax.persistence.FetchType;  
  10. import javax.persistence.GeneratedValue;  
  11. import javax.persistence.Id;  
  12. import javax.persistence.JoinColumn;  
  13. import javax.persistence.OneToMany;  
  14. import javax.persistence.Table;  
  15.   
  16. import org.hibernate.annotations.CacheConcurrencyStrategy;  
  17. import org.hibernate.annotations.GenericGenerator;  
  18.   
  19. @Entity  
  20. @Table(name = "account")  
  21. public class Account implements Serializable {  
  22.   
  23.     @Id  
  24.     @GeneratedValue(generator = "hilo")  
  25.     @GenericGenerator(name = "hilo", strategy = "hilo")  
  26.     @Column(name = "account_id")  
  27.     private long id;// 主键  
  28.   
  29.     @Column(name = "name", nullable = false, unique = true)  
  30.     private String name; // 用户名  
  31.   
  32.     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Album.class)  
  33.     @JoinColumn(name = "account_id", updatable = true)  
  34.     private List<Album> albumList = new LinkedList<Album>(); // 相册  
  35. }  

要注意@OneToMany中的cascade,还有@JoinColumn中的updatable 
Album 
Java代码  收藏代码
  1. import java.io.Serializable;  
  2. import java.util.Date;  
  3.   
  4. import javax.persistence.CascadeType;  
  5. import javax.persistence.Column;  
  6. import javax.persistence.Entity;  
  7. import javax.persistence.FetchType;  
  8. import javax.persistence.GeneratedValue;  
  9. import javax.persistence.Id;  
  10. import javax.persistence.JoinColumn;  
  11. import javax.persistence.ManyToOne;  
  12. import javax.persistence.Table;  
  13.   
  14. import org.hibernate.annotations.Cache;  
  15. import org.hibernate.annotations.CacheConcurrencyStrategy;  
  16. import org.hibernate.annotations.GenericGenerator;  
  17.   
  18. @Entity  
  19. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)  
  20. @Table(name = "album")  
  21. public class Album implements Serializable {  
  22.     private static final long serialVersionUID = -159468065798255466L;  
  23.   
  24.     @Id  
  25.     @GeneratedValue(generator = "hilo")  
  26.     @GenericGenerator(name = "hilo", strategy = "hilo")  
  27.     @Column(name = "album_id")  
  28.     private long id;  
  29.   
  30.     @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Account.class)  
  31.     @JoinColumn(name = "account_id")  
  32.     private Account account;  
  33. }  

同样注意@ManyToOne中的cascade,与Account中保持一致。 
由此生成的sql语句: 
Sql代码  收藏代码
  1. alter table album drop foreign key FK5897E6F68D52691  
  2. drop table if exists account  
  3. drop table if exists album  
  4. drop table if exists hibernate_unique_key  
  5. 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 uniquepassword varchar(255) not null, point integer not null, sign varchar(255), sign_up_date datetime not nullprimary key (account_id))  
  6. create table album (album_id bigint not null, create_date datetime not null, url varchar(255) not null unique, account_id bigintprimary key (album_id))  
  7. alter table album add index FK5897E6F68D52691 (account_id), add constraint FK5897E6F68D52691 foreign key (account_id) references account (account_id)  
  8. CREATE TABLE  hibernate_unique_key (next_hi int(10) unsigned NOT NULL AUTO_INCREMENT,  PRIMARY KEY (next_hi))   
  9. INSERT INTO hibernate_unique_key(next_hi) VALUES(1)  

在执行新建用户的时候,同时新建相册,如果没有设定级联,会出save the transient instance before flushing异常 
Java代码  收藏代码
  1. Account a = new Account("wolf" + System.currentTimeMillis(), "snow");  
  2. a.setEmail("snowolf@wolf.org" + System.currentTimeMillis());  
  3. a.setGender(true);  
  4. // 执行新建用户  
  5. long id = accountService.signUp(a);  
  6. List<Album> list = new LinkedList<Album>();  
  7. list.add(new Album("" + System.currentTimeMillis()));  
  8. a.setAlbumList(list);  
  9.                 // 更新相册  
  10. accountService.update(a);  

生成的执行语句 
Sql代码  收藏代码
  1. Hibernate: insert into account (email, enabled, gender, sign_in_date, namepassword, point, sign, sign_up_date, account_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  
  2. Hibernate: insert into album (account_id, create_date, url, album_id) values (?, ?, ?, ?)  
  3. 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