三、Hibernate基礎(chǔ)配置 1、如果在Annotation中,類名與真是的數(shù)據(jù)庫(kù)中的表名不同,可以使用增加@Table(name=數(shù)據(jù)庫(kù)中的表名)的方式使其對(duì)應(yīng)。此時(shí),如果數(shù)據(jù)庫(kù)中有這個(gè)表名則會(huì)對(duì)應(yīng)上,如果沒(méi)有這個(gè)表,則會(huì)生成這個(gè)表。同樣的,在XXX.hbm.xml中,也可以配置
三、Hibernate基礎(chǔ)配置package com.zgy.hibernate.model; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; @Entity @Table(name="_teacher") public class Teacher { private int id; private String name; private String title; private String address; private String wifeName; private Date birth; private ZhiCheng zhiCheng; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name="_name") public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getWifeName() { return wifeName; } public void setWifeName(String wifeName) { this.wifeName = wifeName; } @Temporal(TemporalType.DATE) public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Enumerated(EnumType.STRING) public ZhiCheng getZhiCheng() { return zhiCheng; } public void setZhiCheng(ZhiCheng zhiCheng) { this.zhiCheng = zhiCheng; } } 修改test下的TeacherTesting.java package com.zgy.hibernate.model; import static org.junit.Assert.*; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class TeacherTesting { public static SessionFactory sf = null; @BeforeClass public static void beforeClass(){ sf = new AnnotationConfiguration().configure().buildSessionFactory(); } @Test public void test() { Teacher t = new Teacher(); t.setId(5); t.setName("t5"); t.setTitle("高級(jí)"); t.setAddress("北京"); t.setBirth(new Date()); t.setZhiCheng(ZhiCheng.A); Session session = sf.openSession(); session.beginTransaction(); session.save(t); session.getTransaction().commit(); session.close(); } @AfterClass public static void afterClass(){ sf.close(); } }
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com