hibernate映射继承关系(每个具体类对应一个表)

User:有属性id,name
DefaultUser,PowerUser 继承User
DefaultUser:有属性someProperty
PowerUser :有属性otherProperty
数据库:

-- Table "poweruser" DDL

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



-- Table "defaultuser" DDL

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


hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	
	<session-factory >
		<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
		<property name="hibernate.cache.use_query_cache">true</property>
		<property name="show_sql">true</property>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="connection.driver_class">
		org.gjt.mm.mysql.Driver
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/Sample
		</property>
		<property name="connection.username">
			root
		</property>
		<property name="connection.password">
			root
		</property>
		<mapping resource="com/user/table_per_concrete_class/DefaultUser.hbm.xml"/>
		<mapping resource="com/user/table_per_concrete_class/PowerUser.hbm.xml"/>
		
	</session-factory>
</hibernate-configuration>


Default.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_concrete_class.DefaultUser"
		table="defaultuser">
		<id column="id" name="id" type="java.lang.Integer">
			<generator class="native" />
		</id>
		<property name="name" type="string" column="name" />
		<property name="someProperty" type="string"
			column="someProperty">
		</property>

	</class>
</hibernate-mapping>


PowerUser.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_concrete_class.PowerUser"
		table="poweruser">
		<id column="id" name="id" type="java.lang.Integer">
			<generator class="native" />
		</id>
		<property name="name" type="string" column="name" />
		<property name="otherProperty" type="string"
			column="otherProperty">
		</property>

	</class>
</hibernate-mapping>


测试代码:
package com.user.table_per_concrete_class;

import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class Test extends TestCase {

	public void testCreate()
	{
		Configuration conf=new Configuration().configure("com/user/table_per_concrete_class/hibernate.cfg.xml");
		SchemaExport export=new SchemaExport(conf);
		export.create(true, true);
		
	}
	public void testAddUser()
	{
		Configuration conf=new Configuration().configure("com/user/table_per_concrete_class/hibernate.cfg.xml");
		SessionFactory factory=conf.buildSessionFactory();
		Session session =factory.openSession();
		Transaction tx=session.beginTransaction();
		DefaultUser defaultUser=new DefaultUser();
		defaultUser.setName("defaultname");
		defaultUser.setSomeProperty("defaultuserSomeProperty");
		
		PowerUser powerUser=new PowerUser();
		powerUser.setName("powername");
		powerUser.setOtherProperty("powerotherProperty");
		
		session.save(defaultUser);
		session.save(powerUser);
		
		tx.commit();
		session.close();
		factory.close();	
	}
	
	public void testQuery()
	{
		Configuration conf=new Configuration().configure("com/user/table_per_concrete_class/hibernate.cfg.xml");
		SessionFactory factory=conf.buildSessionFactory();
		Session session =factory.openSession();
		
		Query query=session.createQuery("from com.user.table_per_concrete_class.User");
		Iterator iterator=query.iterate();
		while(iterator.hasNext())
		{
			User user=(User)iterator.next();
			System.out.println(user.getName());
		}
		
	}
}

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