annotion 时间类型

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();
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值