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版块下的主题 多个主题对应一个版块 @Entity public 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 @GeneratedValue public 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的版块 @Entity public class Category { private int id; private String name; @Id @GeneratedValue public 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主题下面的回帖 多个回帖位于一个主题帖之下 @Entity public class Msg { private int id; private String cont; private Topic topic; @ManyToOne public Topic getTopic() { return topic; } public void setTopic(Topic topic) { this.topic = topic; } @Id @GeneratedValue public 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)
显示日期与时间
时间: 2024-10-24 20:25:54