扫码一下
查看教程更方便
对象/关系映射通常在 xml 文档中定义。 这个映射文件指示 hibernate——如何将定义的一个或多个类映射到数据库表?
尽管许多 hibernate 用户选择手动编写 xml,但存在许多工具来生成映射文档。 其中包括适用于高级 hibernate 用户的 xdoclet、middlegen 和 andromda。
让我们考虑我们之前定义的 pojo 类,其对象将持久保存在下一节定义的表中。
public class employee {
private int id;
private string firstname;
private string lastname;
private int salary;
public employee() {}
public employee(string fname, string lname, int salary) {
this.firstname = fname;
this.lastname = lname;
this.salary = salary;
}
public int getid() {
return id;
}
public void setid( int id ) {
this.id = id;
}
public string getfirstname() {
return firstname;
}
public void setfirstname( string first_name ) {
this.firstname = first_name;
}
public string getlastname() {
return lastname;
}
public void setlastname( string last_name ) {
this.lastname = last_name;
}
public int getsalary() {
return salary;
}
public void setsalary( int salary ) {
this.salary = salary;
}
}
我们愿意提供持久性的每个对象都会对应一个表。 考虑上述对象需要存储和检索到以下 rdbms 表中
create table employee (
id int not null auto_increment,
first_name varchar(20) default null,
last_name varchar(20) default null,
salary int default null,
primary key (id)
);
基于以上两个实体,我们可以定义如下映射文件,它指示 hibernate 如何将定义的一个或多个类映射到数据库表。
?xml version = "1.0" encoding = "utf-8"?>
this class contains the employee detail.
我们应该将映射文档保存在格式为
让我们看看了解一下关于映射文件中使用的映射元素的一些细节
还有其他可用的属性和元素,它们将在映射文档中使用,在讨论其他 hibernate 相关主题时,将尝试涵盖尽可能多的内容。