您的位置:首页 > 数据库

hibernate整合遗留数据库之处理主键(复合外键引用非主键)

2010-09-29 06:48 507 查看
hibernate整合遗留数据库之处理主键(复合外键引用非主键)

有些遗留schema相当复杂:外键可能是一个复合键,并且设计为引用一个复合的自然的非主键。为了映射这个,你需要按相同的名称给几个属性分组,否则无法在property-ref中给复合命名。应用<properties>元素来给映射分组,如你所见,<properties>元素不仅可以用于给几个属性命名,而且可以定义了一个多列的unique约束,或者使几个属性变成不可变。注意,对于关联映射,列的顺序仍然很重要。

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" />
<mapping resource="pojo/Item.hbm.xml" />
</session-factory>
</hibernate-configuration>


pojo/User.java:
]package pojo;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class User implements Serializable {

private String id;
private String name;
private String password;
private int age;
private Set<Item> itemSet = new HashSet<Item>();

public String getId() {
return id;
}
@SuppressWarnings("unused")
private void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Set<Item> getItemSet() {
return itemSet;
}
public void setItemSet(Set<Item> itemSet) {
this.itemSet = itemSet;
}
//添加
public void addItem(Item item){
this.itemSet.add(item);
item.setUser(this);
}
}


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">
<id name="id" column="ID" type="string">
<generator class="uuid.hex"/>
</id>
<!-- 注意这里,用了unique="true",update="false" -->
<properties name="foreignKey" unique="true" update="false">
<property name="name" column="NAME" type="string"/>
<property name="password" column="PASSWORD" type="string"/>
</properties>
<property name="age" type="integer" column="AGE"/>
<set name="itemSet" inverse="true" cascade="save-update">
<key property-ref="foreignKey">
<column name="NAME"/>
<column name="PASSWORD"/>
</key>
<one-to-many class="Item"/>
</set>
</class>
</hibernate-mapping>


pojo/Item.java:
]package pojo;
import java.io.Serializable;
public class Item implements Serializable {
private String id;
private String name;
private String desc;
private int price;
private User user;

public String getId() {
return id;
}
@SuppressWarnings("unused")
private void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}


pojo/Item.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="Item" table="A_ITEM" dynamic-insert="true" dynamic-update="true">
<id name="id" column="ID" type="string">
<generator class="uuid.hex"/>
</id>
<property name="name" column="ITEMNAME" type="string"/>
<property name="desc" column="DESCRIPTION" type="string"/>
<property name="price" column="PRICE" type="integer"/>
<!-- 注意这里使用了property-ref="name" -->
<many-to-one name="user" class="User" property-ref="foreignKey">
<column name="NAME"/>
<column name="PASSWORD"/>
</many-to-one>
</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.Item;
import pojo.User;
public class Manager {
public static void main(String[] args) {

User user = new User();
user.setAge(18);
user.setName("fhd");
user.setPassword("kkk");

Item it1 = new Item();
it1.setDesc("aaaaaaa");
it1.setName("item1");
it1.setPrice(35);

Item it2 = new Item();
it2.setDesc("bbbbbbb");
it2.setName("item2");
it2.setPrice(12);

user.addItem(it1);
user.addItem(it2);

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();

session.save(user);

transaction.commit();
session.close();
}
}


表结构:
]A_USER表:
ID,
NAME,
AGE,
PASSWORD
A_ITEM表:
ID,
ITEMNAME,
DESCRIPTION
PRICE,
NAME,
PASSWORD
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐