Design of Multi-link mode in hibernate
A one-to-many Relationship Pattern Design in hibernate
Annotations should be maintained and managed by multiple parties.
For example, bbs Module Design
Three table Forum tables (category), topic table (topic), and reply table (msg)
The relationship between them is: one forum corresponds to multiple topic posts, and one topic post has multiple reply posts.
The following table creation statements are used.
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;
How is it designed in the object model?
In the actual process, a class is generated through reverse engineering of 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; // multiple topics under the bbs Forum correspond to one forum @ 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 forum @ 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; // Multiple replies under the bbs Topic are located under one topic post @ Entitypublic class Msg {private int id; private String cont; private 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 ;}}Notes:
1. When the table name and class name are different, configure the table name
@ Entity
@ Table (name = "_ teacher ")
Public classTeacher {
}
2. When the field name and attribute name are different, configure the attribute name
@ Column (name = "_ name ")
PublicString getName (){
ReturnName;
}
3. The manytoone annotation is maintained and managed by multiple parties.
4. ing the date and time types, specifying the precision
@ Temporal (parameter) There are three types of parameters: show only time, show only date, show all time and date
// @ Temporal (TemporalType. DATE) only displays the DATE
// @ Temporal (TemporalType. TIME) only displays the TIME
// @ Temporal (TemporalType. TIMESTAMP) display Date and Time