標籤:設計 hibernate
hibernate中一對多的關係模式的設計
註解應該交由多的一方維護和管理。
例如:bbs的模組設計
三個表 版塊表(category) 、主題帖表(topic)和回帖表(msg)
他們之間的對應關係是:一個版塊對應多個主題帖,一個主題帖有多個回複貼。
以下是對應的建表語句。
CREATE TABLE `category` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;
CREATE TABLE `topic` ( `id` int(11) NOT NULL auto_increment, `createDate` datetime default NULL, `title` varchar(255) default NULL, `category_id` int(11) default NULL, PRIMARY KEY (`id`), KEY `FK_jqmw7lsw6i6lrdnw07c1id1nt` (`category_id`), CONSTRAINT `FK_jqmw7lsw6i6lrdnw07c1id1nt` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;
CREATE TABLE `msg` ( `id` int(11) NOT NULL auto_increment, `cont` varchar(255) default NULL, `topic_id` int(11) default NULL, PRIMARY KEY (`id`), KEY `FK_87qfcl8orshwjlllfik5deys5` (`topic_id`), CONSTRAINT `FK_87qfcl8orshwjlllfik5deys5` FOREIGN KEY (`topic_id`) REFERENCES `topic` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;
那麼在物件模型中是如何設計的呢?
實際過程中是通過hibernate的逆向工程產生類
import java.util.Date;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;//bbs版塊下的主題 多個主題對應一個版塊@Entitypublic class Topic {private int id;private String title;private Category category;//private Category category2;private Date createDate;public Date getCreateDate() {return createDate;}public void setCreateDate(Date createDate) {this.createDate = createDate;}@ManyToOne(fetch=FetchType.LAZY)public Category getCategory() {return category;}public void setCategory(Category category) {this.category = category;}@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}}
import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;//bbs的版塊 @Entitypublic class Category {private int id;private String name;@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;//bbs主題下面的回帖 多個回帖位於一個主題帖之下@Entitypublic class Msg {private int id;private String cont;private Topic topic;@ManyToOnepublic Topic getTopic() {return topic;}public void setTopic(Topic topic) {this.topic = topic;}@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getCont() {return cont;}public void setCont(String cont) {this.cont = cont;}}關於註解的幾個注意點:
1.當表名和類名不同,對錶名進行配置
@Entity
@Table(name="_teacher")
public class Teacher {
}
2.當欄位名和屬性名稱不同時,對屬性名稱進行配置
@Column(name="_name")
public String getName() {
return name;
}
3.manytoone註解交由多的哪一方維護和管理
4.映射日期和時間的類型,指定精度
@Temporal(參數) 參數有3種 只顯示時間,只顯示日期,時間日期都顯示
//@Temporal(TemporalType.DATE) 只顯示日期
//@Temporal(TemporalType.TIME) 只顯示時間
//@Temporal(TemporalType.TIMESTAMP) 顯示日期與時間
hibernate中一對多關聯性模式的設計