hibernate helloworld (annotation版本)

1、 创建teacher表,create table(id int primary key,name varchar(20),title varchar(10));
2、 创建Teacher类
3、 在Hibernate lib中加入annotation的jar包
a) hibernate-annotations.jar
b) ejb3-persistence.jar
c) hibernate-commons-annotations.jar
d) 注意文档中没有提到hibernate-commom-annotations.jar文件
4、 参考Annotation文档建立对应的注解
import javax.persistence.Entity;
import javax.persistence.Id;
在bean中加注解
@ Entity,@Id(加在getId()上面)
@没有提示(1)alt+/ (2)window –preference-java-Editor-Content Assist-Auto ..for java设置为.@;Auto...for javadoc设置为#;
5、 在hibernate.cfg.xml 中建立映射<mapping class=””/>
6、 参考文档进行测试(注意文档中缺少configure()的小bug)
7、 测试类
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import com.eagle.hibernate.model.Teacher;
public class TeacherTest {
public static void main(String args[]){
Teacher t = new Teacher();
t.setId(1);
t.setName("teaher1");
t.setTitle("讲师");
Configuration cfg = new AnnotationConfiguration();//注意与xml配置的差别
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
注:抛异常java.lang.ClassNotFoundException: javax.persistence.Cacheable
要引入hibernate-jpa-2.0-api-1.0.0.Final.jar在hibernate-lib-jpa中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值