可以使用注释创建hibernate应用程序。 有许多注释可用于创建hibernate应用程序,如@entity,@id,@table等。
hibernate注释基于jpa 2规范,并支持所有功能。
所有jpa注释都在javax.persistence.*
包中定义。 hibernate entitymanager实现由jpa规范定义的接口和生命周期。
使用hibernate注释的核心优点是我们不需要创建映射(*.hbm.xml
)文件直接创建对象关联。 在这里,hibernate注释用于提供元数据。
完整的项目结构图如下 -
创建具有注释的hibernate应用程序有4
个步骤。分别如下 -
对于mysql,您需要添加mysql-connector-java-5.1.40-bin.jar
文件,以及 hibernate5的核心类库,如下所示 -
在这里,我们创建一个持久化类: employee
。 并使用注释完成与数据库表:tb_employee
的映射关联。
@entity
注释将此类标记为实体。@table
注释指定要保留此实体的数据的表名。 如果不使用@table
注释,hibernate将使用类名作为表名称bydefault
。@id
注释标记此实体的标识符。@column
注释指定此属性或字段的列的详细信息。如果未指定@column
注释,则属性名称将用作列名称bydefault
。
employee.java 文件的代码如下 -
package com.h3;
import javax.persistence.entity;
import javax.persistence.id;
import javax.persistence.table;
@entity
@table(name = "tb_employee")
public class employee {
@id
private int id;
private string firstname, lastname;
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getfirstname() {
return firstname;
}
public void setfirstname(string firstname) {
this.firstname = firstname;
}
public string getlastname() {
return lastname;
}
public void setlastname(string lastname) {
this.lastname = lastname;
}
}
打开hibernate.cfg.xml
文件,并添加如下的映射资源条目:
<mapping class="com.h3.employee"/>
现在完整的配置文件: hibernate.cfg.xml
,将如下所示:
<?xml version='1.0' encoding='utf-8'?>
<!doctype hibernate-configuration public
"-//hibernate/hibernate configuration dtd 3.0//en"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- generated by myeclipse hibernate tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.mysqldialect</property>
<property name="show_sql">true</property>
<mapping class="com.h3.employee"/>
</session-factory>
</hibernate-configuration>
在这个类中,我们只是将employee
类的对象存储到数据库中。 在这里,我们使用standardserviceregistrybuilder
类和metadatasources
类从持久化类获取映射的信息。
test.java
测试类代码如下 -
package com.h3;
import org.hibernate.*;
import org.hibernate.boot.metadatasources;
import org.hibernate.boot.registry.standardserviceregistry;
import org.hibernate.boot.registry.standardserviceregistrybuilder;
import org.hibernate.cfg.*;
public class test {
public static void main(string[] args) {
//session session = new annotationconfiguration().configure().buildsessionfactory().opensession();
final standardserviceregistry registry = new standardserviceregistrybuilder().configure("hibernate.cfg.xml")
.build();
// 2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
sessionfactory sessionfactory = new metadatasources(registry).buildmetadata().buildsessionfactory();
/**** 上面是配置准备,下面开始我们的数据库操作 ******/
session session = sessionfactory.opensession();// 从会话工厂获取一个session
transaction t = session.begintransaction();
employee e1 = new employee();
e1.setid(1001);
e1.setfirstname("yii");
e1.setlastname("bai");
employee e2 = new employee();
e2.setid(1002);
e2.setfirstname("min");
e2.setlastname("su");
session.persist(e1);
session.persist(e2);
t.commit();
session.close();
system.out.println("successfully saved");
}
}
运行上面(test.java)示例,得到以下结果 -
... ...
info: hhh000400: using dialect: org.hibernate.dialect.mysqldialect
hibernate: insert into tb_employee (firstname, lastname, id) values (?, ?, ?)
hibernate: insert into tb_employee (firstname, lastname, id) values (?, ?, ?)
successfully saved
查看数据库表 tb_employee
中的数据,应该会看到以下结果 -