hibernate映射继承关系(2)

根类对应一个表
数据库:

-- Table "user" DDL

CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `userType` varchar(255) NOT NULL,
  `name` varchar(255) default NULL,
  `someProperty` varchar(255) default NULL,
  `otherProperty` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

	<class name="com.user.table_per_class_hierarchy.User"
		table="user">
		<id column="id" name="id" type="java.lang.Integer">
			<generator class="native" />
		</id>
		<discriminator column="userType" type="string"/>
		
		<property name="name" type="string" column="name" />
	
		<subclass name="com.user.table_per_class_hierarchy.DefaultUser" discriminator-value="Default">
			<property name="someProperty" column="someProperty" type="string"/>
		</subclass>
		<subclass name="com.user.table_per_class_hierarchy.PowerUser" discriminator-value="Power">
			<property name="otherProperty" column="otherProperty" type="string"/>
		</subclass>
				

	</class>
</hibernate-mapping>


每个类对应一个表:
父类与子类分别建立表,父类与子类通过 外键产生关联
数据库:

-- Table "user" DDL

CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Table "defaultuser" DDL

CREATE TABLE `defaultuser` (
  `defaultId` int(11) NOT NULL,
  `someProperty` varchar(255) default NULL,
  PRIMARY KEY  (`defaultId`),
  KEY `FKE603B98CB22A8A54` (`defaultId`),
  CONSTRAINT `FKE603B98CB22A8A54` FOREIGN KEY (`defaultId`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Table "poweruser" DDL

CREATE TABLE `poweruser` (
  `powerUserId` int(11) NOT NULL,
  `otherProperty` varchar(255) default NULL,
  PRIMARY KEY  (`powerUserId`),
  KEY `FK326F6A505FF156A3` (`powerUserId`),
  CONSTRAINT `FK326F6A505FF156A3` FOREIGN KEY (`powerUserId`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

User.hbm.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">
<hibernate-mapping>

	<class name="com.user.table_per_subclass.User"
		table="user">
		<id column="id" name="id" type="java.lang.Integer">
			<generator class="native" />
		</id>
		<property name="name" type="string" column="name" />
		
		<joined-subclass name="com.user.table_per_subclass.DefaultUser" table="defaultuser">
			<key column="defaultId"></key>
			<property name="someProperty"  column="someProperty" type="string"></property>
			
		</joined-subclass>
				
		<joined-subclass name="com.user.table_per_subclass.PowerUser" table="poweruser">
			<key column="powerUserId"></key>
			<property name="otherProperty" column="otherProperty" type="string"></property>
		</joined-subclass>
	</class>
</hibernate-mapping>


你可能感兴趣的:(java,Hibernate,xml,.net)