package ctl.annotion.teacher;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;//注意是javax.persistence
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name="TTeacher")//javax.persistence.Table;类Teacher映射表TTeacher在xml中用table="TTeacher"来表示
public class Teacher {
private int id;
private String name;
private String title;
private Date datet;//在orcale中不能用date做字段
//在xml中用type=""来生设置字段类型
//在mysql中可以实现从1自增但是在orcale中虽然实现了自增但是不是从一开始,而是从当前数据库中当前的id值上进行自增
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column
@Temporal(TemporalType.DATE)// 在mysql中2013-01-18在orcale中2013-01-18 00:00:00
// @Temporal(TemporalType.TIME)// 在mysql中12:40:54在orcale中 2013-01-18 13:05:00
//@Temporal(TemporalType.TIMESTAMP)// mysql中2013-01-18 12:42:24 在orcale中 2013-01-18 13:07:28:328000
public Date getDatet() {
return datet;
}
public void setDatet(Date datet) {
this.datet = datet;
}
}
package junit.test.teacher;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import ctl.annotion.teacher.Teacher;
public class TeacherTest {
private static SessionFactory sf=null;
@BeforeClass
public static void beforeClass(){
//sf=new AnnotationConfiguration().configure("Orcalehibernate.cfg.xml").buildSessionFactory();
sf=new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
}
@Test
public void testTeacherSave() {
Teacher t = new Teacher();
t.setName("zxp");
t.setTitle("Ich liebe dich");
t.setDate(new Date());
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
}
@Test
public void print(){
System.out.println(new Date());
}
@AfterClass
public static void afterClass(){
sf.close();
}
}