Hibernate 学习笔记( 三)-- 复合主键

亦凉 2022-07-10 19:25 264阅读 0赞

关于如何配置请参看:Hibernate 学习笔记(一)—用MyEclipse 6.5+MySQL 5.0的环境跑起来 准备:建表

用MySQL在名为STMS数据库中建表personx

090108_0843_Hibernate1.png

src/org.lxh.hibernate2.Personx.java

1 None.gif package org.lxh.hibernate2;
2 None.gif
3 ExpandedBlockStart.gif ContractedBlock.gif /** */ /**
4 InBlock.gif * @author ∪∩BUG E-mail: tidelgl@163.com
5 InBlock.gif * @version Aug 31, 2008 9:56:51 AM @ POJO类
6 ExpandedBlockEnd.gif */
7 None.gif
8 None.gif // 实现复合主键应满足的要 求:1.本类必须实现Serializable 接口
9 ExpandedBlockStart.gif ContractedBlock.gif public class Personx implements java.io.Serializable dot.gif {
10 InBlock.gif
11 InBlock.gif private PersonxId id;
12 InBlock.gif private int age;
13 InBlock.gif
14 ExpandedSubBlockStart.gif ContractedSubBlock.gif public Personx() dot.gif {
15 ExpandedSubBlockEnd.gif }
16 InBlock.gif
17 ExpandedSubBlockStart.gif ContractedSubBlock.gif public PersonxId getId() dot.gif {
18 InBlock.gif return this .id;
19 ExpandedSubBlockEnd.gif }
20 InBlock.gif
21 ExpandedSubBlockStart.gif ContractedSubBlock.gif public void setId(PersonxId id) dot.gif {
22 InBlock.gif this .id = id;
23 ExpandedSubBlockEnd.gif }
24 InBlock.gif
25 ExpandedSubBlockStart.gif ContractedSubBlock.gif public int getAge() dot.gif {
26 InBlock.gif return age;
27 ExpandedSubBlockEnd.gif }
28 InBlock.gif
29 ExpandedSubBlockStart.gif ContractedSubBlock.gif public void setAge( int age) dot.gif {
30 InBlock.gif this .age = age;
31 ExpandedSubBlockEnd.gif }
32 ExpandedBlockEnd.gif }

通过Hibernate反向工程建立personx表与Personx类的映射

首先调出DB Browser视图(Windows—view show—other—MyEclipse datebase—DB Browser)—展开MySQL_localhost至表person—右键表personx—Hibernate Reverse Engineering

090108_0843_Hibernate2.png
src/org.lxh.hibernate2.PersonxId.java

1 None.gif package org.lxh.hibernate2;
2 None.gif
3 None.gif import org.apache.commons.lang.builder.EqualsBuilder;
4 None.gif import org.apache.commons.lang.builder.HashCodeBuilder;
5 None.gif import org.lxh.hibernate1.Personx;
6 None.gif
7 ExpandedBlockStart.gif ContractedBlock.gif /** */ /**
8 InBlock.gif * @author ∪∩BUG E-mail: tidelgl@163.com
9 InBlock.gif * @version Aug 31, 2008 9:56:51 AM @ 复合主键类
10 ExpandedBlockEnd.gif */
11 None.gif // 实现复合主键应满足的要求:
12 None.gif // 1.本类必须实现 Serializable 接口
13 None.gif // 2.复写equals(比较 对象)和hashCode(取得Hash编码)方法(可用commons-lang-2.4.jar来复写,
14 None.gif // 下载: http://apache.mirror.phpchina.com/commons/lang/binaries/commons-lang-2.4-bin.zip )
15 ExpandedBlockStart.gif ContractedBlock.gif public class PersonxId implements java.io.Serializable dot.gif {
16 InBlock.gif
17 InBlock.gif // Fields
18 InBlock.gif
19 InBlock.gif private String name;
20 InBlock.gif private String phone;
21 InBlock.gif
22 InBlock.gif // Constructors
23 InBlock.gif
24 ExpandedSubBlockStart.gif ContractedSubBlock.gif /** */ /** default constructor */
25 ExpandedSubBlockStart.gif ContractedSubBlock.gif public PersonxId() dot.gif {
26 ExpandedSubBlockEnd.gif }
27 InBlock.gif
28 ExpandedSubBlockStart.gif ContractedSubBlock.gif /** */ /** full constructor */
29 ExpandedSubBlockStart.gif ContractedSubBlock.gif public PersonxId(String name, String phone) dot.gif {
30 InBlock.gif this .name = name;
31 InBlock.gif this .phone = phone;
32 ExpandedSubBlockEnd.gif }
33 InBlock.gif
34 InBlock.gif // Property accessors
35 InBlock.gif
36 ExpandedSubBlockStart.gif ContractedSubBlock.gif public String getName() dot.gif {
37 InBlock.gif return this .name;
38 ExpandedSubBlockEnd.gif }
39 InBlock.gif
40 ExpandedSubBlockStart.gif ContractedSubBlock.gif public void setName(String name) dot.gif {
41 InBlock.gif this .name = name;
42 ExpandedSubBlockEnd.gif }
43 InBlock.gif
44 ExpandedSubBlockStart.gif ContractedSubBlock.gif public String getPhone() dot.gif {
45 InBlock.gif return this .phone;
46 ExpandedSubBlockEnd.gif }
47 InBlock.gif
48 ExpandedSubBlockStart.gif ContractedSubBlock.gif public void setPhone(String phone) dot.gif {
49 InBlock.gif this .phone = phone;
50 ExpandedSubBlockEnd.gif }
51 InBlock.gif
52 InBlock.gif // 修改 复写equals的方法
53 ExpandedSubBlockStart.gif ContractedSubBlock.gif public boolean equals(Object obj) dot.gif {
54 ExpandedSubBlockStart.gif ContractedSubBlock.gif if ( this == obj) dot.gif {
55 InBlock.gif return true ;
56 ExpandedSubBlockEnd.gif }
57 InBlock.gif
58 InBlock.gif // 如 果不是Personx的实例
59 ExpandedSubBlockStart.gif ContractedSubBlock.gif if ( ! (obj instanceof Personx)) dot.gif {
60 InBlock.gif return false ;
61 ExpandedSubBlockEnd.gif }
62 InBlock.gif PersonxId p = (PersonxId) obj;
63 InBlock.gif return new EqualsBuilder().append( this .name, p.getName()).append(
64 InBlock.gif this .phone, p.getPhone()).isEquals();
65 ExpandedSubBlockEnd.gif }
66 InBlock.gif
67 InBlock.gif // 修改复 写hashCode的方法
68 ExpandedSubBlockStart.gif ContractedSubBlock.gif public int hashCode() dot.gif {
69 InBlock.gif return new HashCodeBuilder().append( this .name).append(
70 InBlock.gif this .phone).toHashCode();
71 ExpandedSubBlockEnd.gif }
72 InBlock.gif
73 ExpandedBlockEnd.gif }

src/org.lxh.hibernate2.PersonxOperate.java

1 None.gif package org.lxh.hibernate2;
2 None.gif
3 None.gif
4 None.gif import org.hibernate.Session;
5 None.gif import org.hibernate.cfg.Configuration;
6 None.gif
7 ExpandedBlockStart.gif ContractedBlock.gif /** */ /**
8 InBlock.gif * @author ∪∩BUG E-mail: tidelgl@163.com
9 InBlock.gif * @version Aug 31, 2008 9:56:51 AM
10 InBlock.gif * @ 具体操作Hibernate的类
11 ExpandedBlockEnd.gif */
12 ExpandedBlockStart.gif ContractedBlock.gif public class PersonxOperate dot.gif {
13 InBlock.gif // 在 Hibernate中所有的操作都是通过Session来完成
14 InBlock.gif private Session session;
15 InBlock.gif
16 InBlock.gif // Session 是 一个接口,必须实例化
17 InBlock.gif // 在构 造方法中实例实化Session对象
18 ExpandedSubBlockStart.gif ContractedSubBlock.gif public PersonxOperate() dot.gif {
19 InBlock.gif // 找 到Hibernae配置文件,从全局文件中取出SessionFactory,从sessionFactory中取出一个session
20 InBlock.gif this .session = new Configuration().configure().buildSessionFactory()
21 InBlock.gif .openSession();
22 ExpandedSubBlockEnd.gif }
23 InBlock.gif
24 InBlock.gif // 所有 的操作都是通过Session进行
25 InBlock.gif // (1) 增加操作
26 ExpandedSubBlockStart.gif ContractedSubBlock.gif public void insert(Personx p) dot.gif {
27 InBlock.gif // 将 数据存放到数据库中
28 InBlock.gif this .session.save(p);
29 InBlock.gif
30 InBlock.gif // 事 务提交
31 InBlock.gif this .session.beginTransaction().commit();
32 ExpandedSubBlockEnd.gif }
33 ExpandedBlockEnd.gif }
34 None.gif

src/org.lxh.hibernate2.Test.java

1 None.gif package org.lxh.hibernate2;
2 None.gif
3 None.gif
4 None.gif
5 ExpandedBlockStart.gif ContractedBlock.gif /** */ /**
6 InBlock.gif * @author ∪∩BUG E-mail: tidelgl@163.com
7 InBlock.gif * @version Aug 31, 2008 9:54:16 AM
8 InBlock.gif * @测试类
9 ExpandedBlockEnd.gif */
10 ExpandedBlockStart.gif ContractedBlock.gif public class Test dot.gif {
11 InBlock.gif
12 ExpandedSubBlockStart.gif ContractedSubBlock.gif /** */ /**
13 InBlock.gif * @param args
14 ExpandedSubBlockEnd.gif */
15 ExpandedSubBlockStart.gif ContractedSubBlock.gif public static void main(String[] args) dot.gif {
16 InBlock.gif Personx p = new Personx();
17 InBlock.gif PersonxId pk = new PersonxId();
18 InBlock.gif
19 InBlock.gif // name,phone 在表中是主键当插入值为空时将无法插入
20 InBlock.gif // p.setAge(4);
21 InBlock.gif // p.setName(“Hibernate”);
22 InBlock.gif // p.setPhone(“123456”);
23 InBlock.gif
24 InBlock.gif p.setAge( 2 );
25 InBlock.gif pk.setName( “ MySQL “ );
26 InBlock.gif pk.setPhone( “ 87654 “ );
27 InBlock.gif p.setId(pk);
28 InBlock.gif PersonxOperate po = new PersonxOperate();
29 InBlock.gif po.insert(p);
30 ExpandedSubBlockEnd.gif }
31 InBlock.gif
32 ExpandedBlockEnd.gif }
33 None.gif

src/org.lxh.hibernate2.Personx.hbm.xml

1 None.gif xml version=”1.0” encoding=”utf-8” ?>
2 None.gif DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
3 None.gif “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“ >
4 None.gif
7 None.gif < hibernate-mapping >
8 None.gif < class name =”org.lxh.hibernate2.Personx” table =”personx”
9 None.gif catalog =”stms” >
10 None.gif
14 None.gif < composite-id name =”id” class =”org.lxh.hibernate2.PersonxId” >
15 None.gif < key-property name =”name” type =”java.lang.String” >
16 None.gif < column name =”name” length =”100” />
17 None.gif key-property >
18 None.gif < key-property name =”phone” type =”java.lang.String” >
19 None.gif < column name =”phone” length =”50” />
20 None.gif key-property >
21 None.gif composite-id >
22 None.gif < property name =”age” type =”java.lang.Integer” >
23 None.gif < column name =”age” />
24 None.gif property >
25 None.gif class >
26 None.gif hibernate-mapping >
27 None.gif

src/hibernate.cfg.xml

1 None.gif xml version=’1.0’ encoding=’UTF-8’ ?>
2 None.gif DOCTYPE hibernate-configuration PUBLIC
3 None.gif “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
4 None.gif “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd“ >
5 None.gif
6 None.gif
7 None.gif < hibernate-configuration >
8 None.gif
9 None.gif < session-factory >
10 None.gif < property name =”connection.username” > root property >
11 None.gif < property name =”connection.url” >
12 None.gif jdbc:mysql://localhost:3306/STMS
13 None.gif property >
14 None.gif < property name =”dialect” >
15 None.gif org.hibernate.dialect.MySQLDialect
16 None.gif property >
17 None.gif < property name =”myeclipse.connection.profile” >
18 None.gif MySql_localhost
19 None.gif property >
20 None.gif < property name =”connection.password” > root property >
21 None.gif < property name =”connection.driver_class” >
22 None.gif com.mysql.jdbc.Driver
23 None.gif property >
24 None.gif < property name =”show_sql” > true property >
25 None.gif
26 None.gif
27 None.gif < mapping resource =”org/lxh/hibernate2/Personx.hbm.xml” />
28 None.gif
29 None.gif session-factory >
30 None.gif
31 None.gif hibernate-configuration >

例子结构:

090108_0843_Hibernate3.png

090108_0843_Hibernate4.png

090108_0843_Hibernate5.png

发表评论

表情:
评论列表 (有 0 条评论,264人围观)

还没有评论,来说两句吧...

相关阅读

    相关 数据库的复合

    主键是唯一的索引,那么为何一个表可以创建多个主键呢? 其实“主键是唯一的索引”这话有点歧义的。举个例子,我们在表中创建了一个ID字段,自动增长,并设为主键,这个是没有问题的,

    相关 联合复合

    联合主键和复合主键 联合主键 复合主键 联合主键 > 联合主键其实就是中间表。在多对多模型里,需要两个表中的主键组成联合主键,这样就可以