【Hibernate框架开发之二】第一个Hibernate-Annonation项目(采用@Entity、@Id直接映射实体类)


本站文章均为 李华明Himi 原创,转载务必在明显处注明:
转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/hibernate/789.html

          ☞ 点击订阅 ☜
 本博客最新动态!及时将最新博文通知您!

紧接上一篇,这里Himi直接copy上一篇创建的HelloHibernate项目,然后改名为:HelloAnnonation,Ok;

 OK,准备工作:

首先第一步,继续再我们自定义的user libraries 中添加Annotation所需的包,如下3个jar包(共11个包):

               /hibernate-annotations-3.4.0.GA/hibernate-annotations.jar     (核心包)

               /hibernate-annotations-3.4.0.GA/lib/ejb3-persistence.jar   (jpa)

               /hibernate-annotations-3.4.0.GA/lib/hibernate-commons-annotations.jar   (反射所需的包)

如下图 ;

【Hibernate框架开发之二】第一个Hibernate-Annonation项目(采用@Entity、@Id直接映射实体类)_第1张图片

然后我们新建一个People类,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.himi;
 
public class People {
 
     private int id;
     private String name;
     private int age;
     private String title;
     private String birthday;
 
     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;
     }
     public int getAge() {
         return age;
     }
     public void setAge( int age) {
         this .age = age;
     }
     public String getTitle() {
         return title;
     }
     public void setTitle(String title) {
         this .title = title;
     }
     public String getBirthday() {
         return birthday;
     }
     public void setBirthday(String birthday) {
         this .birthday = birthday;
     }
 
}

最后准备工作在我们数据库中新建一个对应映射people实体类的表:(不赘述,直接看操作过程)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mysql> use hibernate;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> create table people(id int primary key , name varchar (20),age int ,title varchar (20), birthday varchar (20) );
Query OK, 0 rows affected (0.06 sec)
 
mysql> describe people;
+ ----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+ ----------+-------------+------+-----+---------+-------+
| id       | int (11)     | NO   | PRI | NULL    |       |
| name     | varchar (20) | YES  |     | NULL    |       |
| age      | int (11)     | YES  |     | NULL    |       |
| title    | varchar (20) | YES  |     | NULL    |       |
| birthday | varchar (20) | YES  |     | NULL    |       |
+ ----------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

准备工作完成之后,那么如果通常我们会建立People.hbm.xml来对应数据库的组件和属性,然后将People在hibernate.cfg.xml配置文件中使用mapping resource声明我们有一个被加了映射,People是实体类;

但是本篇我们使用Annotation将不再创建对应的实体类对应数据库的xml,而是直接在People类中声明实体类就可以啦,修改People.java文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.himi;
 
import javax.persistence.Entity;
import javax.persistence.Id;
 
@Entity
public class People {
 
     private int id;
     private String name;
     private int age;
     private String title;
     private String birthday;
 
     @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;
     }
     public int getAge() {
         return age;
     }
     public void setAge( int age) {
         this .age = age;
     }
     public String getTitle() {
         return title;
     }
     public void setTitle(String title) {
         this .title = title;
     }
     public String getBirthday() {
         return

你可能感兴趣的:(Hibernate,框架,String,session,null,Class)