有时候我的表中需要多个主键的时候,这个时候我们就需要使用组件来做为表的主键。
1.我们需要一份hibernate.cfg.xml的配置文件
2.我们需要一个用来持久化的类,我这里例子用的是 Member.java
3.需要一个组件类, 我这里的例子用的是 Name.java
4.需要为持久化类配置一个映射的规则文件。 Member.cfg.xml
5.用来将持久化类插入数据库的操纵类, 我这里的例子用的是 MemberHandler.java
1.hibernate.cfg.xml
代码如下 |
复制代码 |
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
root
word">root
jdbc:mysql://localhost:3306/hibernate
20
1
5000
true
true
true
100
3000
2
true
create
|
2.持久化类 Member.java
代码如下 |
复制代码 |
package org.Rudiment.hibernate;
import java.util.HashMap;
import java.util.Map;
public class Member {
private Integer id;
private Name name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
|
3.持久化类的组件类 Name.java,这个类需要实现 Serializable 这个接口,保证能序列化,还有重写equals和hashCode这两个方法
代码如下 |
复制代码 |
package org.Rudiment.hibernate;
import java.io.Serializable;
public class Name implements Serializable
{
private String first;
private String last;
public Name()
{}
public Name(String first, String last)
{
this.first = first;
this.last = last;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
@Override
public boolean equals(Object obj)
{
if(this == obj)
{
return true;
}
else if(obj != null && obj.getClass() == Name.class)
{
Name target = (Name)obj;
if(target.getFirst().equals(this.getFirst()) && target.getLast().equals(this.getLast()))
{
return true;
}
}
return false;
}
@Override
public int hashCode()
{
return this.getFirst().hashCode() * 7 + this.getLast().hashCode();
}
}
|
4.持久化类的映射规则文件 Member.cfg.xml
代码如下 |
复制代码 |
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
5.持久化类的操纵类 MemberHandler.java
代码如下 |
复制代码 |
package org.Rudiment.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class MemberHandler {
public static void insert() {
Configuration conf = new Configuration();
conf.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
SessionFactory sf = conf.buildSessionFactory(sr);
Session session = sf.openSession();
session.beginTransaction();
Member m = new Member();
m.setAge(24);
Name n = new Name("IT","kezhan");
m.setName(n);
session.save(m);
session.getTransaction().commit();
session.close();
}
public static void main(String[] args) {
insert();
}
}
|
注:
当运行MemberHandler的时候将发现,后台数据库多了一个 test_member 的数据表,表的内容如下:
代码如下 |
复制代码 |
mysql> select * from test_member;
+-------+--------+------+
| first | last | AGE |
+-------+--------+------+
| IT | kezhan | 24 |
+-------+--------+------+
1 row in set (0.00 sec)
mysql> desc test_member;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| first | varchar(255) | NO | PRI | NULL | |
| last | varchar(255) | NO | PRI | NULL | |
| AGE | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
|