003:hibernate:Annotation版本的Helloworld
来源:互联网 发布:淘宝售后客服的重要性 编辑:程序博客网 时间:2024/05/16 15:10
1、建 一个Teacher类:
public class Teacher {
private int id;
private String name;
private String title;
2、在数据库中建 teacher表
create table teacher(id int primary key,
name varcher(20),
title varchar(10));
3、引入与Annotation相关的jar包
windows---preferences-----java---User Libraries---hibernate----Add JARS---
(1)、hibernate-annotations-3.4.0.GA----hibernate-annotations.jar
(2)、hibernate-annotations-3.4.GA
lib--->-ejb3-persistence.jar
hibernate-commons-annotations.jar
共11个包
4、在Teacher.java下面的package com.bjsxt.hibernate.model;
@Entity
Entity是一个实体类,就意味着hibernate就可以帮着管起来,就 意味着跟数据库的某个表对应,如杲不写任何特殊的东西,就意味和它对应的数据库的那张表就叫teacher,如杲想知道数据库的表叫什么,可以加一个@table来指明
5、对主键进行注解@id[加在get方法上面]
@Id
public int getId(){
return id;
}
package com.bjsxt.hibernate.model;import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class Teacher { private int id; private String name; private String title; @Idpublic 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;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;} }
6、需要在hibernate.cfg.xml中告诉别人,有一个类加了映射是实体类。
<mapping class="com.bjsxt.hibernate.model.Teacher"/>
7、对TeacherTest.java:
package com.bjsxt.hibernate.model;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;public class TeacherTest { public static void main(String[]args){ Teacher t=new Teacher(); t.setId(1); t.setName("t"); t.setTitle("哦昂"); //开始拿到session //首先要拿到configuration Configuration cfg=new AnnotationConfiguration(); SessionFactory sf= cfg.configure().buildSessionFactory(); Session session=sf.openSession(); session.save(t); // session.getTransaction().commit(); session.beginTransaction().commit(); session.close(); sf.close(); } }
异常:
2 在myeclipse输入@时没有提示原因
这个需要配置一下工具的自动补全功能。一般默认的只有输入 . 的时候才能自动提示。配置方法:1. 打开MyEclipse,然后“window”→“Preferences”2. 选择“java”,展开,“Editor”,选择“Content Assist”。3. 选择“Content Assist”,然后看到右边,右边的“Auto-Activation”下面的“Auto Activation triggers for java”这个选项。其实就是指触发代码提示的就是“.”这个符号。4.“Auto Activation triggers for java”这个选项,在“.”后加@字母,然后“apply”,点击“OK”。
3 @Entityjavax.persistence.Entity报错Syntax error, annotations are only available if source level is 5.0
解决办法:
Project->Properties->Java Compiler->Compiler Compliance Level选择5.0或者以上的jdk版本
菜单条 ---"Windows" ----"preferneces"
在弹出对话框中 ,在左侧列表框中 选择 "JAVA" ---子菜单中 "compiler" ...
然后在右侧把 "Compiler compliance level" 设置成5.0以上 即可。
- 003:hibernate:Annotation版本的Helloworld
- Hibernate的Annotation版本的HelloWorld
- Hibernate 中创建Annotation版本的HelloWorld
- hibernate 建立annotation版本的helloworld
- Hibernate(annotation版本helloworld)-2
- annotation版本的helloworld
- 建立Annotation版本的HelloWorld
- Annotation版本的hibernate
- hibernate3.3.2学习笔记---Annotation版本的HelloWorld及org.hibernate.HibernateException错误改正
- 03hibernate Annotation版本的hibernate
- Hibernate的关系映射总结annotation版本
- 建立 Annotation版本的hibernate小例子
- Hibernate helloworld xml版本
- hibernate的Annotation配置
- Hibernate 简单的annotation
- Hibernate的Annotation注解
- Hibernate的Annotation注解
- Hibernate的Annotation注解
- <meta name="viewport" content="width=device-width, maximum-scale=1.0, initial-scale=1.0, user-sc
- UVA 10020 — Minimal coverage
- 论DSP281x_CodeStartBranch.asm的重要性
- Effective C# Item7:将值类型尽可能实现为具有常量性和原子性的类型
- H-4548-美素数
- 003:hibernate:Annotation版本的Helloworld
- LIDAR目标提取与识别的学科层次
- google code jam 2014 RB_C
- SQL高级查询
- Android 中 -Java中byte[]转String问题
- svn的安装
- 编译原理手记05-文法和语法分析中一些概念的解释
- C语言的本质(31)——C语言与汇编之函数调用的本质
- 大数加法