hibernate学习笔记1



@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;
}

}




@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 (?, ?)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值