hibernate3.3.2学习笔记(二)

写一个annotation版的,不废话了,先加入三个jar包一个是hibernate-annotations.jar,ejb3-persistence.jar,hibernate-commons-annotations.jar,如图
hibernate3.3.2学习笔记(二)
一、建立一个teacher表,有id,name,title
二、建立一个Teacher类,代码如下
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Teacher {
	private int id;
	private String name;
	private String title;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	@Id
	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;
	}

}

三、在hibernate.cfg.xml中加入如下
<mapping class="com.fengpeng.model.Teacher"/>

四、建立一个teacher的测试类,代码如下
public class TeacherTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Teacher teacher = new Teacher();
		teacher.setId(3);
		teacher.setName("wang");
		teacher.setTitle("教授");
	
		Session session = new AnnotationConfiguration().configure().buildSessionFactory()
				.openSession();
		session.beginTransaction();
		session.save(teacher);
		session.beginTransaction().commit();
		session.close();
	}

}

运行测试类会出现Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)

你可能感兴趣的:(Hibernate,xml)