有时候我的表中需要多个主键的时候,这个时候我们就需要使用组件来做为表的主键。
1.我们需要一份hibernate.cfg.xml的配置文件
2.我们需要一个用来持久化的类,我这里例子用的是 Member.java
3.需要一个组件类, 我这里的例子用的是 Name.java
4.需要为持久化类配置一个映射的规则文件。 Member.cfg.xml
5.用来将持久化类插入数据库的操纵类, 我这里的例子用的是 MemberHandler.java
1.hibernate.cfg.xml
代码如下 |
复制代码 |
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置数据库的驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置数据库用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 配置数据库的密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 配置数据库的url -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<!-- 配置数据池的最大容量 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 配置数据池的最小容量 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 配置数据链接的超时界限 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 在控制台显示后台是否打印执行的sql -->
<property name="hibernate.show_sql">true</property>
<!-- 是否以友好的格式显示打印的sql -->
<property name="hibernate.format_sql">true</property>
<!-- 打印一些辅助性的注释 -->
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<!-- 配置数据操作的方式 -->
<property name="hbm2ddl.auto">create</property>
<!-- 将我们上面 Member 的映射文件添加进来 -->
<mapping resource="org/Rudiment/hibernate/Member.hbm.xml" />
</session-factory>
</hibernate-configuration>
|
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
代码如下 |
复制代码 |
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-9-9 19:50:34 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="org.Rudiment.hibernate">
<class name="Member" table="TEST_MEMBER">
<composite-id name="name" class="Name">
<key-property name="first" type="string" />
<key-property name="last" type="string" />
</composite-id>
<property name="age" column="AGE"></property>
</class>
</hibernate-mapping>
|
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)
|
时间: 2024-10-26 20:33:16