Hibernate对应关系

一对一关联主要有3种方式 
1.两主键关联 
2.外键关联 
3.关联表关联 
这三种方式hibernate annitation都提供了支持 
这里重点讲第二种配置 

配置步骤 
一数据库表配置 
表结构见附录 
sns_topic表示一个论坛主题 
sns_topic_views`表示帖子浏览量 
topic.views_id持有topic_views.id,为多对一关联。但我们需要的是一对一关联 
所以需要在views_id上增加唯一性约束 

Java代码   收藏代码
  1. UNIQUE KEY `views_id` (`views_id`),  


ok 数据库端搞定 

二 entity class 配置 
两个entity class : Topic  TopicViews,代码见附录 
Topic.java 

Java代码   收藏代码
  1. @OneToOne(optional = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY)  
  2.    @JoinColumn(name = "views_id")   
  3. public TopicViews getTopicViews() {  
  4.     return topicViews;  
  5. }  



TopicViews.java 

Java代码   收藏代码
  1. @OneToOne(mappedBy = "topicViews")  
  2. public Topic getTopic() {  
  3.     return topic;  
  4. }    
  5.   
  6. public void setTopic(Topic topic) {  
  7.     this.topic = topic;  
  8. }  



这就配置完了 

三 总结 
这个例子中的帖子和帖子点击量,前者属于不经常变化的实体类,而后者更新非常频繁 
配置成一对一的目的是为了更有效的利用缓存 
但事实上效果不好 没能达到预期的效果 
每次更新topicviews都需要先外连接()这两张表,虽然关联的效率很高,但相比主键关联还是差些 
主键一对一关联可独立的访问每个表 而不需要任何的关联查询 


附录1 

Java代码   收藏代码
  1. CREATE TABLE `sns_topic` (  
  2.   `id` INTEGER(11) NOT NULL AUTO_INCREMENT,  
  3.   `author_id` INTEGER(11) NOT NULL,  
  4.   `title` VARCHAR(256) COLLATE utf8_general_ci DEFAULT NULL,  
  5.   `content` TEXT COLLATE utf8_general_ci,  
  6.   `create_date` DATETIME DEFAULT NULL,  
  7.   `quan_id` INTEGER(11) DEFAULT NULL,  
  8.   `category_id` INTEGER(11) DEFAULT NULL,  
  9.   `stick` TINYINT(4) DEFAULT NULL,  
  10.   `digest` TINYINT(4) DEFAULT NULL,  
  11.   `locked` TINYINT(4) DEFAULT NULL,  
  12.   `hide` TINYINT(4) DEFAULT NULL,  
  13.   `views_id` INTEGER(11) DEFAULT NULL,  
  14.   `posts` INTEGER(11) DEFAULT NULL,  
  15.   `last_post` DATETIME DEFAULT NULL,  
  16.   `last_poster` VARCHAR(128) COLLATE utf8_general_ci DEFAULT NULL,  
  17.   `highlight` TINYINT(4) DEFAULT NULL,  
  18.   PRIMARY KEY (`id`),  
  19.   UNIQUE KEY `views_id` (`views_id`),  
  20.   KEY `author_id` (`author_id`),  
  21.   KEY `quan_id` (`quan_id`),  
  22.   CONSTRAINT `sns_topic_fk2` FOREIGN KEY (`views_id`) REFERENCES `sns_topic_views` (`id`),  
  23.   CONSTRAINT `sns_topic_fk` FOREIGN KEY (`author_id`) REFERENCES `sns_user` (`id`),  
  24.   CONSTRAINT `sns_topic_fk1` FOREIGN KEY (`quan_id`) REFERENCES `sns_quan` (`id`)  
  25. )ENGINE=InnoDB  
  26. AUTO_INCREMENT=14 CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'  
  27. COMMENT='InnoDB free: 11264 kB; (`author_id`) REFER `sns/sns_user`(`i; InnoDB free: 11264';  
  28.    
  29. //`sns_topic_views`表示帖子浏览量  
  30. CREATE TABLE `sns_topic_views` (  
  31.   `id` INTEGER(11) NOT NULL AUTO_INCREMENT,  
  32.   `views` INTEGER(11) DEFAULT NULL,  
  33.   PRIMARY KEY (`id`)  
  34. )ENGINE=InnoDB  
  35. AUTO_INCREMENT=3 CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'  
  36. COMMENT='InnoDB free: 11264 kB';  



附录2 

Java代码   收藏代码
  1. package com.justel.sns.domain;  
  2.   
  3. import static javax.persistence.GenerationType.IDENTITY;  
  4.   
  5. import java.util.Date;  
  6. import java.util.HashSet;  
  7. import java.util.Set;  
  8.   
  9. import javax.persistence.CascadeType;  
  10. import javax.persistence.Column;  
  11. import javax.persistence.Entity;  
  12. import javax.persistence.FetchType;  
  13. import javax.persistence.GeneratedValue;  
  14. import javax.persistence.Id;  
  15. import javax.persistence.JoinColumn;  
  16. import javax.persistence.ManyToOne;  
  17. import javax.persistence.OneToMany;  
  18. import javax.persistence.OneToOne;  
  19. import javax.persistence.PrimaryKeyJoinColumn;  
  20. import javax.persistence.Table;  
  21. import javax.persistence.Temporal;  
  22. import javax.persistence.TemporalType;  
  23.   
  24. import org.hibernate.annotations.Cache;  
  25. import org.hibernate.annotations.CacheConcurrencyStrategy;  
  26.   
  27. /** 
  28.  * 主贴 entity. 
  29.  *  
  30.  * @author xuliangyong 
  31.  */  
  32. @Entity  
  33. @Table(name = "sns_topic", catalog = "sns")  
  34. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)  
  35. public class Topic implements java.io.Serializable {  
  36.     private static final long serialVersionUID = -5503914485019914680L;  
  37.     private Integer id;  
  38.     private User snsUser; //作者  
  39.     private Quan snsQuan; //所属圈子  
  40.     private String title;  
  41.     private String content;  
  42.     private Date createDate;  
  43.     private Integer categoryId; //帖子分类  
  44.     private boolean stick;  //置顶  
  45.     private boolean digest; //精华  
  46.     private boolean lock;       //锁定  
  47.     private boolean hide;       //隐藏  
  48.     private Integer posts;  //回复次数  
  49.     private Date lastPost;  //最后回复时间  
  50.     private String lastPoster;  //最后回复用户  
  51.     private boolean highlight;  //高亮显示  
  52.     private Set<Post> snsPosts = new HashSet<Post>(0);  
  53.       
  54.     private TopicViews topicViews; //访问次数  
  55.     // Constructors  
  56.   
  57.     /** default constructor */  
  58.     public Topic() {  
  59.     }  
  60.       
  61.     public Topic(Integer id){  
  62.         this.id = id;  
  63.     }  
  64.       
  65.     /** minimal constructor */  
  66.     public Topic(User snsUser) {  
  67.         this.snsUser = snsUser;  
  68.     }  
  69.   
  70.     /** full constructor */  
  71.     public Topic(User snsUser, Quan snsQuan, String title,  
  72.             String content, Date createDate, Integer categoryId, boolean stick,  
  73.             boolean digest, boolean lock, boolean hide, TopicViews topicViews, Integer posts,  
  74.             Date lastPost, String lastPoster, boolean highlight,  
  75.             Set<Post> snsPosts) {  
  76.         this.snsUser = snsUser;  
  77.         this.snsQuan = snsQuan;  
  78.         this.title = title;  
  79.         this.content = content;  
  80.         this.createDate = createDate;  
  81.         this.categoryId = categoryId;  
  82.         this.stick = stick;  
  83.         this.digest = digest;  
  84.         this.lock = lock;  
  85.         this.hide = hide;  
  86.         this.topicViews = topicViews;  
  87.         this.posts = posts;  
  88.         this.lastPost = lastPost;  
  89.         this.lastPoster = lastPoster;  
  90.         this.highlight = highlight;  
  91.         this.snsPosts = snsPosts;  
  92.     }  
  93.   
  94.     // Property accessors  
  95.     @Id  
  96.     @GeneratedValue(strategy = IDENTITY)  
  97.     @Column(name = "id", unique = true, nullable = false)  
  98.     public Integer getId() {  
  99.         return this.id;  
  100.     }  
  101.   
  102.     public void setId(Integer id) {  
  103.         this.id = id;  
  104.     }  
  105.   
  106.     @ManyToOne(fetch = FetchType.LAZY)  
  107.     @JoinColumn(name = "author_id", nullable = false)  
  108.     public User getSnsUser() {  
  109.         return this.snsUser;  
  110.     }  
  111.   
  112.     public void setSnsUser(User snsUser) {  
  113.         this.snsUser = snsUser;  
  114.     }  
  115.   
  116.     @ManyToOne(fetch = FetchType.LAZY)  
  117.     @JoinColumn(name = "quan_id")  
  118.     public Quan getSnsQuan() {  
  119.         return this.snsQuan;  
  120.     }  
  121.   
  122.     public void setSnsQuan(Quan snsQuan) {  
  123.         this.snsQuan = snsQuan;  
  124.     }  
  125.   
  126.     @Column(name = "title", length = 256)  
  127.     public String getTitle() {  
  128.         return this.title;  
  129.     }  
  130.   
  131.     public void setTitle(String title) {  
  132.         this.title = title;  
  133.     }  
  134.   
  135.     @Column(name = "content", length = 65535)  
  136.     public String getContent() {  
  137.         return this.content;  
  138.     }  
  139.   
  140.     public void setContent(String content) {  
  141.         this.content = content;  
  142.     }  
  143.   
  144.     @Column(name = "create_date", length = 19)  
  145.     public Date getCreateDate() {  
  146.         return this.createDate;  
  147.     }  
  148.   
  149.     public void setCreateDate(Date createDate) {  
  150.         this.createDate = createDate;  
  151.     }  
  152.   
  153.     @Column(name = "category_id")  
  154.     public Integer getCategoryId() {  
  155.         return this.categoryId;  
  156.     }  
  157.   
  158.     public void setCategoryId(Integer categoryId) {  
  159.         this.categoryId = categoryId;  
  160.     }  
  161.   
  162.     @Column(name = "stick")  
  163.     public boolean getStick() {  
  164.         return this.stick;  
  165.     }  
  166.   
  167.     public void setStick(boolean stick) {  
  168.         this.stick = stick;  
  169.     }  
  170.   
  171.     @Column(name = "digest")  
  172.     public boolean getDigest() {  
  173.         return this.digest;  
  174.     }  
  175.   
  176.     public void setDigest(boolean digest) {  
  177.         this.digest = digest;  
  178.     }  
  179.   
  180.     @Column(name = "locked")  
  181.     public boolean getLock() {  
  182.         return this.lock;  
  183.     }  
  184.   
  185.     public void setLock(boolean lock) {  
  186.         this.lock = lock;  
  187.     }  
  188.   
  189.     @Column(name = "hide")  
  190.     public boolean getHide() {  
  191.         return this.hide;  
  192.     }  
  193.   
  194.     public void setHide(boolean hide) {  
  195.         this.hide = hide;  
  196.     }  
  197.   
  198.     @Column(name = "posts")  
  199.     public Integer getPosts() {  
  200.         return this.posts;  
  201.     }  
  202.   
  203.     public void setPosts(Integer posts) {  
  204.         this.posts = posts;  
  205.     }  
  206.   
  207.     @Temporal(TemporalType.DATE)  
  208.     @Column(name = "last_post", length = 19)  
  209.     public Date getLastPost() {  
  210.         return this.lastPost;  
  211.     }  
  212.   
  213.     public void setLastPost(Date lastPost) {  
  214.         this.lastPost = lastPost;  
  215.     }  
  216.   
  217.     @Column(name = "last_poster", length = 128)  
  218.     public String getLastPoster() {  
  219.         return this.lastPoster;  
  220.     }  
  221.   
  222.     public void setLastPoster(String lastPoster) {  
  223.         this.lastPoster = lastPoster;  
  224.     }  
  225.   
  226.     @Column(name = "highlight")  
  227.     public boolean getHighlight() {  
  228.         return this.highlight;  
  229.     }  
  230.   
  231.     public void setHighlight(boolean highlight) {  
  232.         this.highlight = highlight;  
  233.     }  
  234.   
  235.     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "snsTopic")  
  236.     public Set<Post> getSnsPosts() {  
  237.         return this.snsPosts;  
  238.     }  
  239.   
  240.     public void setSnsPosts(Set<Post> snsPosts) {  
  241.         this.snsPosts = snsPosts;  
  242.     }  
  243.   
  244.     @OneToOne(optional = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY)  
  245.     @JoinColumn(name = "views_id")  
  246.     public TopicViews getTopicViews() {  
  247.         return topicViews;  
  248.     }  
  249.   
  250.     public void setTopicViews(TopicViews topicViews) {  
  251.         this.topicViews = topicViews;  
  252.     }  
  253.   
  254. }  
  255.   
  256.   
  257. package com.justel.sns.domain;  
  258.   
  259. import static javax.persistence.GenerationType.IDENTITY;  
  260.   
  261. import java.io.Serializable;  
  262.   
  263. import javax.persistence.Column;  
  264. import javax.persistence.Entity;  
  265. import javax.persistence.GeneratedValue;  
  266. import javax.persistence.Id;  
  267. import javax.persistence.OneToOne;  
  268. import javax.persistence.PrimaryKeyJoinColumn;  
  269. import javax.persistence.Table;  
  270.   
  271. @Entity  
  272. @Table(name = "sns_topic_views", catalog = "sns")  
  273. public class TopicViews implements Serializable {  
  274.     private Integer id;  
  275.     private Topic topic;  
  276.     private Integer views;  
  277.       
  278.     //http://blog.csdn.net/EJB_JPA/archive/2008/05/11/2433301.aspx  
  279.     @OneToOne(mappedBy = "topicViews")  
  280.     public Topic getTopic() {  
  281.         return topic;  
  282.     }    
  283.       
  284.     public void setTopic(Topic topic) {  
  285.         this.topic = topic;  
  286.     }  
  287.       
  288.     public Integer getViews() {  
  289.         return views;  
  290.     }  
  291.       
  292.     public void setViews(Integer views) {  
  293.         this.views = views;  
  294.     }  
  295.   
  296.     @Id    
  297.     @GeneratedValue(strategy = IDENTITY)  
  298.     public Integer getId() {  
  299.         return id;  
  300.     }  
  301.   
  302.     public void setId(Integer topicId) {  
  303.         this.id = topicId;  
  304.     }  
  305.       
  306.       

http://xuliangyong.iteye.com/blog/353096

猜你喜欢

转载自dyrlgh.iteye.com/blog/1949469