@Entity
public class Category {
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;
}
}
@Entity
public class Topic {
private int id;
private String title;
private Category category;
private Date createDate;
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@ManyToOne //@Column(name="cate_id") 未指定列名,则默认为 类名_id (eg:category_id)
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;
}
}
@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;
}
}
public static void testSave() {
Session session = sf.openSession();
session.beginTransaction();
for(int i=0; i<10; i++) {
Category c = new Category();
c.setName("c" + i);
session.save(c);
}
for(int i=0; i<10; i++) {
Category c = new Category();
c.setId(1);
Topic t = new Topic();
t.setCategory(c);
t.setTitle("t" + i);
t.setCreateDate(new Date());
session.save(t);
}
for(int i=0; i<10; i++) {
Topic t = new Topic();
t.setId(1);
Msg m = new Msg();
m.setCont("m" + i);
m.setTopic(t);
session.save(m);
}
session.getTransaction().commit();
session.close();
}
11:12:47,921 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - alter table Msg drop foreign key FK12F6179FADF6A
11:12:48,015 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - alter table Topic drop foreign key FK4D3DD0F8F317EEA
11:12:48,109 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - drop table if exists Category
11:12:48,125 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - drop table if exists Msg
11:12:48,140 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - drop table if exists Topic
11:12:48,140 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - create table Category (id integer not null auto_increment, name varchar(255), primary key (id))
11:12:48,203 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - create table Msg (id integer not null auto_increment, cont varchar(255), topic_id integer, primary key (id))
11:12:48,265 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - create table Topic (id integer not null auto_increment, createDate datetime, title varchar(255), category_id integer, primary key (id))
11:12:48,312 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - alter table Msg add index FK12F6179FADF6A (topic_id), add constraint FK12F6179FADF6A foreign key (topic_id) references Topic (id)
11:12:48,421 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - alter table Topic add index FK4D3DD0F8F317EEA (category_id), add constraint FK4D3DD0F8F317EEA foreign key (category_id) references Category (id)
11:12:48,500 INFO org.hibernate.tool.hbm2ddl.SchemaExport:196 - schema export complete
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Category (name) values (?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Topic (category_id, createDate, title) values (?, ?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)
Hibernate: insert into Msg (cont, topic_id) values (?, ?)