hibernate整合遗留数据库之处理主键(映射自然键之复合自然键)
2010-09-29 03:04
573 查看
hibernate整合遗留数据库之处理主键(映射自然键之复合自然键)
hibernate支持自然键的使用。它支持复合的和非复合的这两种自然主键。
以复合的自然主键为例:
假设USER表的主键由NAME和PASSWORD组成。
pom.xml:
resources/hibernate.cfg.xml:
pojo/User.java:
pojo/User.hbm.xml:
util/HibernateUtil.java:
util/Manager.java:
表结构:
如例:
session.load(User.class,user)代码片段中,user表现得就像它自己的标识符类。不过,如上例中,定义一个只声明键属性的单独复合标识符类则更为优雅,不过必须实现equals()和hashCode()方法, 同时还需要实现Serializable接口。
hibernate支持自然键的使用。它支持复合的和非复合的这两种自然主键。
以复合的自然主键为例:
假设USER表的主键由NAME和PASSWORD组成。
pom.xml:
]<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>hibernateTest</groupId> <artifactId>hibernateTest</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>hibernateTest</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.1.GA</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.4.GA</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.3.0</version> <scope>runtime</scope> </dependency> </dependencies> <build> <finalName>hibernateTest</finalName> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
resources/hibernate.cfg.xml:
]<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name="sessionFactory"> <!-- 指定连接数据库所用的驱动 --> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- 指定连接数据库的url,hibernate连接的数据库名 --> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> <property name="hibernate.connection.useUnicode">true</property> <property name="hibernate.connection.characterEncoding">gbk</property> <!-- 指定连接数据库的用户名 --> <property name="hibernate.connection.username">system</property> <!-- 指定连接数据库的密码 --> <property name="hibernate.connection.password">password</property> <!-- 指定连接池里最大连接数 --> <property name="hibernate.c3p0.max_size">20</property> <!-- 指定连接池里最小连接数 --> <property name="hibernate.c3p0.min_size">5</property> <!-- 指定连接池里连接的超时时长,以秒为单位 --> <property name="hibernate.c3p0.timeout">120</property> <!-- 指定连接池里最大缓存多少个Statement对象 --> <property name="hibernate.c3p0.max_statements">100</property> <!-- 每隔XX秒检查连接池里的空闲连接 ,单位是秒 --> <property name="hibernate.c3p0.idle_test_period">120</property> <!-- 当连接池里面的连接用完的时候,C3P0一次获取的新的连接数 --> <property name="hibernate.c3p0.acquire_increment">2</property> <!-- 指定数据库方言 --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- 显示Hibernate持久化操作所生成的SQL --> <property name="show_sql">true</property> <!-- 将SQL脚本进行格式化后再输出 --> <property name="hibernate.format_sql">true</property> <!-- 罗列所有的映射文件 --> <mapping resource="pojo/User.hbm.xml" /> </session-factory> </hibernate-configuration>
pojo/User.java:
]package pojo; import java.io.Serializable; public class User implements Serializable { private static class Id implements Serializable{ private String name; private String password; public Id(String name,String password){ this.name = name; this.password = password; } //重写hashCode() @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); return result; } //重写equals() @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Id other = (Id) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; return true; } } private Id id; private int age; public User(){} public User(String name,String password,int age){ this.age = age; id = new Id(name,password); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Id getId() { return id; } public void setId(Id id) { this.id = id; } }
pojo/User.hbm.xml:
]<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="pojo"> <class name="User" table="A_USER" dynamic-insert="true" dynamic-update="true"> <!-- 注意这里 --> <composite-id name="id" class="User$Id"> <key-property name="name" column="NAME" type="string" access="field"/> <key-property name="password" column="PASSWORD" type="string" access="field"/> </composite-id> <property name="age" type="integer" column="AGE"/> </class> </hibernate-mapping>
util/HibernateUtil.java:
]package util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; static{ try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable e) { throw new ExceptionInInitializerError(e); } } public static SessionFactory getSessionFactory(){ return sessionFactory; } public static void shutdown(){ getSessionFactory().close(); } }
util/Manager.java:
]package util; import org.hibernate.Session; import org.hibernate.Transaction; import pojo.User; public class Manager { public static void main(String[] args) { User user = new User("fuhaidong","000990",28); Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); session.save(user); transaction.commit(); session.close(); } }
表结构:
]A_USER表: NAME, PASSWORD, AGE
如例:
session.load(User.class,user)代码片段中,user表现得就像它自己的标识符类。不过,如上例中,定义一个只声明键属性的单独复合标识符类则更为优雅,不过必须实现equals()和hashCode()方法, 同时还需要实现Serializable接口。
相关文章推荐
- hibernate整合遗留数据库之处理主键(映射自然键之非复合自然键)
- hibernate整合遗留数据库之处理主键(复合主键的外键)
- hibernate整合遗留数据库之处理主键(复合外键引用非主键)
- hibernate整合遗留数据库之处理主键(复合主键中的外键)
- hibernate整合遗留数据库之处理主键(外键引用非主键)
- hibernate整合遗留数据库之处理主键(外键引用非主键) .
- hibernate 映射无主键和复合主键的数据库表
- hibernate 映射无主键的数据库表
- SSH:Hibernate框架(Hibernate复合主键映射)
- Java程序员从笨鸟到菜鸟之(六十)细谈Hibernate(十一)hibernate复合主键映射
- 闲来无事,复习复习以前的东西---hibernate中的关联映射图解 ---- 复合主键
- Hibernate复合主键映射
- Hibernate的复合主键映射
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之七:Hibernate Entity的多字段组成主键处理
- Hibernate映射详解(七)--复合主键映射与Component映射
- Hibernate处理无主键的表的映射问题
- hibernate中表的复合主键映射表
- Hibernate的关联映射---6.1.9: 基于复合主键的关联关系
- hibernate复合主键映射
- Hibernate征途(七)之复合主键映射和集合映射