hibernate之多态关联(多态的多对一关联或者多态的一对一关联,利用joined-subclass)
2010-09-27 02:29
519 查看
hibernate之多态关联(多态的多对一关联或者多态的一对一关联,利用joined-subclass)
不必做任何特别的事情来启用hibernate中的多态关联,在关联映射中指定任何被映射的持久化类的名称,然后,如果该类声明了任何<union-subclass>,<subclass>或者<joined-subclass>元素,该关联就自然地成为多态。
下面以关联映射中映射的持久化类中声明了<joined-subclass>为例:
pom.xml:
resources/hibernate.cfg.xml:
pojo/User.java:
pojo/User.hbm.xml:
pojo/BillingDetails.java:
pojo/BankAccount.java:
pojo/CreditCard.java:
pojo/BillingDetails.hbm.xml:
util/HibernateUtil.java:
util/Manager.java:
表结构:
不必做任何特别的事情来启用hibernate中的多态关联,在关联映射中指定任何被映射的持久化类的名称,然后,如果该类声明了任何<union-subclass>,<subclass>或者<joined-subclass>元素,该关联就自然地成为多态。
下面以关联映射中映射的持久化类中声明了<joined-subclass>为例:
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/BillingDetails.hbm.xml" /> <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 String id; private String name; private int age; private String password; private BillingDetails billingDetails; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public BillingDetails getBillingDetails() { return billingDetails; } public void setBillingDetails(BillingDetails billingDetails) { this.billingDetails = billingDetails; } }
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> <property name="name" column="NAME" type="string"/> <property name="age" column="AGE" type="integer"/> <property name="password" column="PASSWORD" type="string"/> <!-- 这里加上unique="true"就成了一对一关联 --> <many-to-one name="billingDetails" class="BillingDetails" column="BILLINGDETAILS_ID" cascade="save-update"/> </class> </hibernate-mapping>
pojo/BillingDetails.java:
]package pojo; import java.io.Serializable; public abstract class BillingDetails implements Serializable{ private String id; private String owner; private User user; public String getId() { return id; } @SuppressWarnings("unused") private void setId(String id) { this.id = id; } public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
pojo/BankAccount.java:
]package pojo; public class BankAccount extends BillingDetails { private String account; private String bankname; private String swift; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getBankname() { return bankname; } public void setBankname(String bankname) { this.bankname = bankname; } public String getSwift() { return swift; } public void setSwift(String swift) { this.swift = swift; } }
pojo/CreditCard.java:
]package pojo; public class CreditCard extends BillingDetails { private String number; private String expMonth; private String expYear; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getExpMonth() { return expMonth; } public void setExpMonth(String expMonth) { this.expMonth = expMonth; } public String getExpYear() { return expYear; } public void setExpYear(String expYear) { this.expYear = expYear; } }
pojo/BillingDetails.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="BillingDetails" table="A_BILLINGDETAILS" dynamic-insert="true" dynamic-update="true"> <id name="id" column="ID" type="string"> <generator class="uuid.hex" /> </id> <property name="owner" column="OWNER" type="string"/> <one-to-one name="user" class="User" property-ref="billingDetails"/> <joined-subclass name="BankAccount" table="A_BANKACCOUNT"> <key column="ID"/> <property name="account" column="ACCOUNT" type="string"/> <property name="bankname" column="BANKNAME" type="string"/> <property name="swift" column="SWIFT" type="string"/> </joined-subclass> <joined-subclass name="CreditCard" table="A_CREDITCARD"> <key column="ID"/> <property name="number" column="NUM" type="string"/> <property name="expMonth" column="EXPMONTH" type="string"/> <property name="expYear" column="EXPYEAR" type="string"/> </joined-subclass> </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.BankAccount; import pojo.CreditCard; import pojo.User; public class Manager { public static void main(String[] args) { BankAccount ba = new BankAccount(); ba.setAccount("ba111"); ba.setBankname("ba222"); ba.setOwner("ba333"); ba.setSwift("ba444"); User user = new User(); user.setAge(30); user.setName("fhd"); user.setPassword("999999"); user.setBillingDetails(ba); CreditCard cc = new CreditCard(); cc.setExpMonth("5"); cc.setExpYear("2010"); cc.setOwner("ba555"); cc.setNumber("55"); User user1 = new User(); user1.setAge(30); user1.setName("fhd555"); user1.setPassword("555555"); user1.setBillingDetails(cc); Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); session.save(user); session.save(user1); transaction.commit(); session.close(); } }
表结构:
]A_USER表: ID, NAME, AGE, PASSWORD, BILLINGDETAILS_ID A_BILLINGDETAILS表: ID, OWNER A_BANKACCOUNT表: ID, ACCOUNT, BANKNAME, SWIFT A_CREDITCARD表: ID, NUM, EXPMONTH, EXPYEAR
相关文章推荐
- hibernate之多态关联(多态的多对一关联或者多态的一对一关联,利用subclass)
- hibernate之多态关联(多态的多对一关联或者多态的一对一关联,利用union-subclass)
- hibernate之多态关联(多态的一对多,利用joined-subclass)
- hibernate之多态关联(多态的一对多,利用union-subclass)
- hibernate之多态关联(多态的一对多,利用subclass)
- Hibernate深入学习(三):继承与多态查询,joined-subclass与union-subclass
- hibernate之多态关联(多态的多对一,利用any,使用每个具体类一张表的策略)
- 【Hibernate步步为营】--双向关联一对一映射详解(二)
- Hibernate实体关系映射:双向主键一对一关联
- hibernate一对一关联映射--
- Hibernate——一对一关联映射
- Hibernate之一对一外键关联
- hibernate 继承映射joined-subclass
- Hibernate 一对一外键双向关联
- Hibernate一对一双向关联映射
- Hibernate中一对一关联实例
- hibernate学习(6)——一对一单向外键关联
- Hibernate关系映射(4)_一对一双向主键关联
- 【SSH】Hibernate:O/R实体映射与一对一关联
- hibernate一对一双向外键关联_Annotation