分享Java快乐

我有一份快乐,分给你一些;我还是有一份快乐,你也有一份快乐。

mxj ID:javamxj
510557次访问,排名74好友0人,关注者2
javamxj的文章
原创 82 篇
翻译 0 篇
转载 0 篇
评论 717 篇
javamxj的公告

    版权声明:在此发表的有关文章均属本人javamxj原创,转摘或引用请注明出处。



联系方式:
Email:
javamxj@gmail.com

友情Blog

最近评论
xkpkhu:wow power leveling
xkpkhu:wow power leveling
xkpkhu:wow power leveling
zhouxz1026:写得真是太好了,水平真的很高,佩服啊!赞一个!学习了!
蜂胶
蜂蜜
hitprince:谢谢啦,知道了
文章分类
收藏
    相册
    有空逛逛
    java开源大全(RSS)
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 Eclipse快速上手Hibernate--4. 继承映射(1)收藏

    新一篇: Lomboz中配置JBoss 4.0.1

       前面的《Eclipse快速上手Hibernate--1. 入门实例 》等三篇文章已经谈了Hibernate的入门以及利用工具创建的方法。这篇文章主要说说在Hibernate中的继承映射。相关配置请参考前三篇文章
     
       如果程序中的对象含有继承的关系,在Hibernate中有以下三种策略将这种关系映射到数据表上:
    · 每个类层次结构一个表(table per class hierarchy)
    · 每个子类一个表(table per subclass)
    · 每个具体类一个表(table per concrete class)(有一些限制)
     
       每个类层次结构一个表的方式是将所有继承同一父类别的对象储存在同一个表格中,为了做到这一点,需要在表格中使用识别字段来表示某一列(row)是属于某个子类别或父类别,在这个主题中我们将先说明这个方法。
     
    1. 创建项目
     
    ·  新建一个Java项目:InheritanceMapping,注意选中“创建单独的源文件夹和输出文件夹”,同时添加“用户库”:hibernate。 
     
     
    2. 编写类文件
     
    ·  新建一个类,包名:javamxj.inheritance.one,类名:Animal。然后在生成的代码中添加变量,再利用“生成 Getter 和 Setter”,具体方式同《Eclipse快速上手Hibernate--1. 入门实例 》文章中的编辑User.java的方式一样。

    Animal.java

    /*
     * Hibernate - 继承映射(每个类层次一个表)
     * 创建日期 2005-4-9
     * @author javamxj(分享java快乐)
     * @link  Blog: htpp://javamxj.mblogger.cn  
     *              htpp://blog.csdn.net/javamxj/ 
     */
    package javamxj.inheritance.one;
    
    /**
     * @hibernate.class 
     *   table="Animal" 
     *   discriminator-value="Animal"
     * @hibernate.discriminator 
     *   column="ANIMAL_TYPE" 
     *   type="string" 
     *   length = "10"
     */
    public abstract class Animal {
    	private Long id;
    
    	private String name;
    
    	/**
    	 * @hibernate.id 
    	 *   column="ID" 
    	 *   generator-class="hilo" 
    	 *   unsaved-value="null"
    	 */
    	public Long getId() {
    		return id;
    	}
    
    	public void setId(Long id) {
    		this.id = id;
    	}
    
    	/**
    	 * @hibernate.property 
    	 *   length = "24"
    	 */
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public abstract void makeSound();
    }
    
     ·  这个类是父类,值得注意是在类层次标记中添加了一个discriminator标记,并用它定义了一个字段“ANIMAL_TYPE”,这个字段就是用来识别某一列(row)是属于某个子类别或父类别的。
     
     
    ·  子类Cat.java
    Cat.java
    package javamxj.inheritance.one;
    
    /**
     * @hibernate.subclass 
     *   discriminator-value="Cat"
     */
    public class Cat extends Animal {
    	private String FurColor;
    
    	public void makeSound() {
    		System.out.println("喵喵");
    	}
    
    	/**
    	 * @hibernate.property 
    	 *   length = "24"
    	 */
    	public String getFurColor() {
    		return FurColor;
    	}
    
    	public void setFurColor(String furColor) {
    		FurColor = furColor;
    	}
    }
    
     
    ·  子类Dog.java
    Dog.java
    package javamxj.inheritance.one;
    
    /**
     * @hibernate.subclass 
     *   discriminator-value="Dog"
     */
    public class Dog extends Animal {
    	private String category;
    
    	public void makeSound() {
    		System.out.println("汪汪");
    	}
    
    	/**
    	 * @hibernate.property 
    	 *   length = "24"
    	 */
    	public String getCategory() {
    		return category;
    	}
    
    	public void setCategory(String category) {
    		this.category = category;
    	}
    }
    
    ·  这两个子类都很简单,注意添加的hibernate.subclass的标记,指定其识别字段。
     
     
    3. 构建文件Build.xml
     
    ·  在项目根目录下建立一个build.xml,要注意的是环境变量和数据库的设置要符合自己的实际配置,这里库文件目录的设置是"D:/java/Hibernate/lib",参考文章《Eclipse快速上手Hibernate--1. 入门实例》中的设置。
    ·  在MySQL中需要先建立一个HibernateTest数据库,为了解决中文问题,使用了GBK编码,注意“&”,这是由于XDoclet生成Hibernate配置文件时,会丢失一个“amp;”字符串(一个小Bug)。
    ·  这里我用build.xml中的“hibernatedoclet”任务直接生成“hibernate.cfg.xml”配置文件。

    build.xml

    <?xml version="1.0" encoding="GBK"?>
    <project name="Hibernate中的继承映射" default="help" basedir=".">
    
    	<!-- ******  环境设置,可以根据自己的实际配置自行更改 ***** -->
    	<!-- 源文件目录, 可以通过 项目->属性->Java构建路径 更改 -->
    	<property name="src.dir" value="./src" />
    	<!-- 输出的class文件目录,可以通过 项目->属性->Java构建路径 更改 -->
    	<property name="class.dir" value="./bin" />
    	<!-- 库文件目录  -->
    	<property name="lib.dir" value="D:/java/Hibernate/lib" />
    	
    	<!-- ******  数据库设置,可以根据自己的实际配置自行更改 ***** -->	
    	<property name="hibernate.dialect" value="net.sf.hibernate.dialect.MySQLDialect"></property>
    	<property name="hibernate.driver" value="com.mysql.jdbc.Driver"></property>
    	<!--    &amp;amp;    -->
    	<property name="hibernate.jdbc.url" 
    		value="jdbc:mysql://localhost:3306/HibernateTest?useUnicode=true&amp;amp;characterEncoding=GBK">
    	</property>
    	<property name="hibernate.username" value="root"></property>
    	<property name="hibernate.password" value="javamxj"></property>
    	<property name="hibernate.show.sql" value="true"></property>
    	
    	
    
    	<!-- 定义类路径 -->
    	<path id="project.class.path">
    		<fileset dir="${lib.dir}">
    			<include name="*.jar"/>
    		</fileset>
    		<pathelement location="${class.dir}" />
    	</path>
    
    	<!-- ************************************************************** -->
    	<!-- 使用说明 -->
    	<!-- ************************************************************** -->
    	<target name="help">
    		<echo message="利用工具开发Hibernate" />
    		<echo message="-----------------------------------" />
    		<echo message="" />
    		<echo message="提供以下任务:" />
    		<echo message="" />		
    		<echo message="generate-hbm      --> 运行HibernateDoclet,生成 Hibernate 类的映射文件" />
    		<echo message="schemaexportt     --> 运行SchemaExport,利用 hbm.xml 文件生成数据表" />
    		<echo message="" />
    	</target>
    
    
    
    	<!-- ************************************************************** -->
    	<!-- HibernateDoclet 任务 -->
    	<!-- ************************************************************** -->
    	<target name="generate-hbm" >
    		<echo message="运行HibernateDoclet,生成 Hibernate 类的映射文件"/>
    
    		<taskdef name="hibernatedoclet" 
    			classname="xdoclet.modules.hibernate.HibernateDocletTask" 
    			classpathref="project.class.path">
    		</taskdef>
    
    		<hibernatedoclet destdir="${src.dir}" 
    			excludedtags="@version,@author,@todo" force="true" encoding="GBK" 
    			verbose="true">
    
    			<fileset dir="${src.dir}">
    				<include name="**/*.java"/>
    			</fileset>
    
    			<hibernate version="2.0" xmlencoding="GBK" />	
    			
    			<!--   生成配置文件       -->
    			<hibernatecfg
    				dialect="${hibernate.dialect}"
    				driver="${hibernate.driver}" 
    				jdbcUrl="${hibernate.jdbc.url}" 
    				userName="${hibernate.username}" 
    				password="${hibernate.password}"
    				showSql="${hibernate.show.sql}"
    				xmlencoding="GBK"
    			/>			
    			
    		</hibernatedoclet>		
    	</target>
    
    
    	<!-- ************************************************************** -->
    	<!-- SchemaExport 任务 -->
    	<!-- ************************************************************** -->
    	<target name="schemaexport">
    		<echo message="运行SchemaExport,利用 hbm.xml 文件生成数据表"/>
    
    		<taskdef name="schemaexport" 
    			classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask" 
    			classpathref="project.class.path">
    		</taskdef>
    
    		<schemaexport config="${src.dir}/hibernate.cfg.xml" quiet="no" 
    			text="no" drop="no" output="schema-export.sql">
    		</schemaexport>
    	</target>
    
    </project>
    
     
     
    · 好了,只要这四个文件就够了,其它的会自动生成的。整个项目的结构如下:
     
     
     
    4. 运行任务
     
    ·  双击“generate-hbm”任务,会发现在包中多了一个Animal.hbm.xml文件,在src目录下会多了一个hibernate.cfg.xml文件,如果没有,按F5键刷新一下(这里建议打开Eclipse的“首选项”对话框,在“工作台”中勾选“自动刷新工作空间”和“在构建之前自动保存”这两项,这样以后不用每次都刷新了)。

    Animal.hbm.xml

    <?xml version="1.0" encoding="GBK"?>
    
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN" 
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    
    <hibernate-mapping
    >
        <class
            name="javamxj.inheritance.one.Animal"
            table="Animal"
            dynamic-update="false"
            dynamic-insert="false"
            select-before-update="false"
            optimistic-lock="version"
            discriminator-value="Animal"
        >
    
            <id
                name="id"
                column="ID"
                type="java.lang.Long"
                unsaved-value="null"
            >
                <generator class="hilo">
                  <!--  
                      To add non XDoclet generator parameters, create a file named 
                      hibernate-generator-params-Animal.xml 
                      containing the additional parameters and place it in your merge dir. 
                  --> 
                </generator>
            </id>
    
            <discriminator
                column="ANIMAL_TYPE"
                type="string"
                length="10"
            />
    
            <property
                name="name"
                type="java.lang.String"
                update="true"
                insert="true"
                access="property"
                column="name"
                length="24"
            />
    
            <!--
                To add non XDoclet property mappings, create a file named
                    hibernate-properties-Animal.xml
                containing the additional properties and place it in your merge dir.
            -->
            <subclass
                name="javamxj.inheritance.one.Cat"
                dynamic-update="false"
                dynamic-insert="false"
                discriminator-value="Cat"
            >
    
            <property
                name="furColor"
                type="java.lang.String"
                update="true"
                insert="true"
                access="property"
                column="furColor"
                length="24"
            />
    
    	    <!--
                	To add non XDoclet property mappings, create a file named
                    hibernate-properties-Cat.xml
    		containing the additional properties and place it in your merge dir.
    	    -->
    
            </subclass>
            <subclass
                name="javamxj.inheritance.one.Dog"
                dynamic-update="false"
                dynamic-insert="false"
                discriminator-value="Dog"
            >
    
            <property
                name="category"
                type="java.lang.String"
                update="true"
                insert="true"
                access="property"
                column="category"
                length="24"
            />
    
    	    <!--
                	To add non XDoclet property mappings, create a file named
                    hibernate-properties-Dog.xml
    		containing the additional properties and place it in your merge dir.
    	    -->
    
            </subclass>
    
        </class>
    
    </hibernate-mapping>
    
     
     
    hibernate.cfg.xml
    <?xml version="1.0" encoding="GBK"?>
    <!DOCTYPE hibernate-configuration 
      PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" 
      "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
    
    <!-- Generated file - Do not edit! -->
    
    <hibernate-configuration>
    
    	<!-- a SessionFactory instance listed as /jndi/name -->
    	<session-factory>
    
    		<!-- properties -->
    		<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
    		<property name="show_sql">true</property>
    		<property name="use_outer_join">false</property>
    		<property name="connection.username">root</property>
    		<property name="connection.password">javamxj</property>
    		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    	           <property name="connection.url">jdbc:mysql://localhost:3306/HibernateTest?useUnicode=true&amp;characterEncoding=GBK</property>
    
    		<!-- mapping files -->
    		<mapping resource="javamxj/inheritance/one/Animal.hbm.xml"/>
    	</session-factory>
    
    </hibernate-configuration>
    
     
    ·  双击“schemaexport”任务,在项目根目录下,会产生一个“schema-export.sql”文件。
    schema-export.sql
    drop table if exists Animal
    drop table if exists hibernate_unique_key
    create table Animal (
       ID bigint not null,
       ANIMAL_TYPE varchar(10) not null,
       name varchar(24),
       furColor varchar(24),
       category varchar(24),
       primary key (ID)
    )
    create table hibernate_unique_key (
        next_hi integer
    )
    insert into hibernate_unique_key values ( 0 )
     
    ·  切换到数据库中,会发现已经自动产生了数据表animal(表hibernate_unique_key是由于采用hilo主键策略生成的)。
     
     
    5. 测试程序
     
    ·  好了,在包javamxj.inheritance.one下新建一个Demo.java类,很简单,前半部分是添加数据,后半部分是简单的测试。

    Demo.java

    package javamxj.inheritance.one;
    
    import java.util.Iterator;
    import java.util.List;
    
    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.SessionFactory;
    import net.sf.hibernate.Transaction;
    import net.sf.hibernate.cfg.Configuration;
    
    public class Demo {
    	public static void main(String[] args) {
    		try {
    			new Demo();
    		} catch (HibernateException he) {
    			he.printStackTrace();
    		}
    	}
    
    	public Demo() throws HibernateException {
    		
    		SessionFactory sf = new Configuration().configure()
    				.buildSessionFactory();
    
    		Session sess = sf.openSession();
    		Transaction tx = null;
    		try {
    			tx = sess.beginTransaction();
    
    			Cat cat = new Cat();
    			cat.setName("小白");
    			cat.setFurColor("白色");
    			sess.save(cat);
    
    			Dog dog = new Dog();
    			dog.setName("小黑");
    			dog.setCategory("京巴狗");
    			sess.save(dog);
    
    			tx.commit();
    		} catch (HibernateException e) {
    			if (tx != null)
    				tx.rollback();
    			throw e;
    		} finally {
    			sess.close();
    		}
    
    		sess = sf.openSession();
    		tx = null;
    		try {
    			tx = sess.beginTransaction();
    			List animals = sess.find("from " + Animal.class.getName());
    			for (Iterator it = animals.iterator(); it.hasNext();) {
    				Animal animal = (Animal) it.next();
    				System.out.println("动物 '" + animal.getName()
    						+ "' 所在类是: " + animal.getClass().getName());
    				System.out.print("发出叫声: ");
    				animal.makeSound();
    
    			}
    
    			tx.commit();
    		} catch (HibernateException e) {
    			if (tx != null)
    				tx.rollback();
    			throw e;
    		} finally {
    			sess.close();
    		}
    	}
    }
    
     
     
    ·  运行这个类,控制台输出如下:
     
    ·  同时,数据表中生成如下数据:
    注意其中为“NULL”的部分。
     
    ·  最后的项目结构如下:
     
     
    小结:  
    ● 优点:
    · 实现简单。
    · 支持多态——对象角色发生变化,或存在多重角色时。
    · 报表操作实现简单:表中包含了所有信息。

    ● 缺点:
    · 增加类层次中的耦合。类层次中任何类的属性的增加都会导致表的变更;某个子类属性的修改会影响到整个
    层次结构,而不仅仅是该子类。
    · 浪费了一些数据库空间。浪费空间的多少取决于继承层次的深度。层次越深,不同的属性越多,属性的全集就越大,也就越浪费空间。
    · 可能需要指明具体的角色。
     
     
    参考:
    · HIBERNATE - 符合Java习惯的关系数据库持久化(第8章)
     
      下篇文章会谈谈每个子类一个表的策略。

    发表于 @ 2005年04月11日 00:19:00|评论(loading...)|编辑

    旧一篇: Eclipse快速上手Hibernate--3. 利用XDoclet开发

    评论

    #javamxj 发表于2005-04-29 13:48:00  IP:
    TrackBack来自《Eclipse快速上手Hibernate--7. 关联映射(一对多) (1)》

    Ping Back来自:blog.csdn.net
    #javamxj 发表于2005-05-12 00:00:00  IP:
    TrackBack来自《Eclipse快速上手Hibernate--8. 关联映射(多对多)》

    Ping Back来自:blog.csdn.net
    #zesila_80 发表于2005-06-22 09:36:00  IP:
    TrackBack来自《Eclipse快速上手Hibernate--4. 继承映射(2)(摘自javamxj的blog)》

    Ping Back来自:blog.csdn.net
    #zesila_80 发表于2005-06-22 09:39:00  IP:
    TrackBack来自《Eclipse快速上手Hibernate--5. 组件映射》

    Ping Back来自:blog.csdn.net
    #zesila_80 发表于2005-06-22 09:40:00  IP:
    TrackBack来自《Eclipse快速上手Hibernate--6. 关联映射(一对一)》

    Ping Back来自:blog.csdn.net
    #zesila_80 发表于2005-06-22 09:41:00  IP:
    TrackBack来自《Eclipse快速上手Hibernate--7. 关联映射(一对多) (1)》

    Ping Back来自:blog.csdn.net
    #zesila_80 发表于2005-06-22 09:43:00  IP:
    TrackBack来自《Eclipse快速上手Hibernate--8. 关联映射(多对多)》

    Ping Back来自:blog.csdn.net
    #leeshaoqun 发表于2006-02-18 19:37:00  IP: 211.100.21.*
    TrackBack来自《Eclipse快速上手Hibernate--4. 继承映射(2) 》

    上篇文章《Eclipse快速上手Hibernate--4. 继承映射(1) 》中已经谈了每个类层次结构一个表(table per class hierarchy)的策略,这篇文章主要说的是每个子类一个表(table per subclass)的策略。一些重复的部分这里就不说了,请参考上篇文章。
    #leeshaoqun 发表于2006-02-18 19:39:00  IP: 211.100.21.*
    TrackBack来自《Eclipse快速上手Hibernate--5. 组件映射》

    这篇文章主要说的是在Hibernate中的组件(Component)映射,可以参考Hibernate官方文档的第7章。至于环境设置,可以参考这个系列的前面几篇文章。
    #leeshaoqun 发表于2006-02-18 19:39:00  IP: 211.100.21.*
    TrackBack来自《Eclipse快速上手Hibernate--6. 关联映射(一对一) 》

    Hibernate中的关联(Association)映射主要有三种:一对一关联,一对多(或多对一)关联,多对多关联。每种关联都可以分为单向和双向两种。
    #leeshaoqun 发表于2006-02-18 19:40:00  IP: 211.100.21.*
    TrackBack来自《Eclipse快速上手Hibernate--7. 关联映射(一对多) (1)》

    Hibernate中的关联(Association)映射主要有三种:一对一关联,一对多(或多对一)关联,多对多关联。每种关联都可以分为单向和双向两种。
    #leeshaoqun 发表于2006-02-18 19:42:00  IP: 211.100.21.*
    TrackBack来自《Eclipse快速上手Hibernate--8. 关联映射(多对多)》

    Hibernate中的关联(Association)映射主要有三种:一对一关联,一对多(或多对一)关联,多对多关联。每种关联都可以分为单向和双向两种。
    #kxtk 发表于2006-06-09 19:10:00  IP: 211.100.21.*
    TrackBack来自《Eclipse快速上手Hibernate--5. 组件映射 》

    这篇文章主要说的是在Hibernate中的组件(Component)映射,可以参考Hibernate官方文档的第7章。至于环境设置,可以参考这个系列的前面几篇文章。
    #icejava 发表于2006-06-16 13:47:00  IP: 211.100.21.*
    TrackBack来自《Eclipse快速上手Hibernate--5. 组件映射 》

    Eclipse快速上手Hibernate--5. 组件映射
    #icejava 发表于2006-06-16 13:48:00  IP: 211.100.21.*
    TrackBack来自《Eclipse快速上手Hibernate--6. 关联映射(一对一) 》

    Eclipse快速上手Hibernate--6. 关联映射(一对一)

    #Asdic 发表于2005-04-11 12:49:00  IP: 210.25.133.*
    你写的文章很为初学者着想,我非常支持,

    对你表示深深的谢意!
    #javamxj 发表于2005-04-14 16:20:00  IP: 218.79.116.*
    esmiles:
    “构建路径”有没有设置,注意:要使用本文中的build.xml,不要使用前几篇文章中的build.xml。
    #esmiles 发表于2005-04-15 08:59:00  IP: 210.83.200.*
    不好意思,在我这边还是不能实现,构建路径也已经按前面的文章设置好了。而且能输出Animal.hbm.xml和hibernate.cfg.xml,但始终还是提示找不到hibernate.cfg.xml文件。请指教,谢谢。
    #javamxj 发表于2005-04-15 10:36:00  IP: 218.80.137.*
    To esmiles:
    输出的hibernate.cfg.xml文件在哪个目录下面?它应该在src目录下面。
    #pepsi 发表于2005-04-15 16:49:00  IP: 211.155.247.*
    我也有和esmiles一样的问题,它就是报找不到hibernate.cfg.xml,我换成系统绝对路径也报找不到,
    你的src目录对应我的source目录,急亚,请指教!!
    schemaexport:
    [echo] 运行SchemaExport,利用 hbm.xml 文件生成数据表
    [echo] ./source/hibernate.cfg.xml
    [schemaexport] (cfg.Environment 432 ) Hibernate 2.1 final
    [schemaexport] (cfg.Environment 461 ) hibernate.properties not found
    [schemaexport] (cfg.Environment 481 ) using CGLIB reflection optimizer
    [schemaexport] (cfg.Configuration 830 ) configuring from resource: ./source/hibernate.cfg.xml
    [schemaexport] (cfg.Configuration 802 ) Configuration resource: ./source/hibernate.cfg.xml
    [schemaexport] (cfg.Configuration 806 ) ./source/hibernate.cfg.xml not found
    BUILD FAILED: F:\eclipse3\workspace\HibernateBegin_2\build.xml:91: Schema text failed: ./source/hibernate.cfg.xml not found
    Total time: 2 seconds
    #esmiles 发表于2005-04-18 11:46:00  IP: 210.83.200.*
    是,目录结构是一样的。我也不知道是哪里出了问题,我按你的步骤已经做了好几遍了,还是同样的结果
    #pepsi 发表于2005-04-18 13:46:00  IP: 211.155.247.*
    esmiles:你那个问题解决了吗?解决了也告诉我怎么做,好吗?我郁闷死了!QQ8544796
    #javamxj 发表于2005-04-19 20:29:00  IP: 218.80.134.*
    运行“generate-hbm”任务,刷新src目录,看看是否产生hibernate.cfg.xml文件。
    #常笑 发表于2005-04-20 18:09:00  IP: 220.113.0.*
    问题解决了,真是是好文章!!
    #esmiles 发表于2005-04-21 09:15:00  IP: 210.83.200.*
    产生了,已经刷新了也显示出来了,进目录里看也有文件。我就纳闷了。呵呵。
    #fuzy 发表于2005-04-22 16:13:00  IP: 219.139.253.*
    请问学习ant首先应该看什么资料
    #砖头 发表于2005-04-23 22:43:00  IP: 219.137.116.*
    javamxj 请问怎么解决

    运行“schema-export”任务
    但是没有产生schema-export.sql这个文件

    console 输出这个错
    Buildfile: E:\eclipse\workspace\InheritanceMapping\build.xml
    schemaexport:
    [echo] 运行SchemaExport,利用 hbm.xml 文件生成数据表
    [schemaexport] (cfg.Environment 462 ) Hibernate 2.1.2
    [schemaexport] (cfg.Environment 491 ) hibernate.properties not found
    [schemaexport] (cfg.Environment 519 ) using CGLIB reflection optimizer
    [schemaexport] (cfg.Configuration 854 ) configuring from resource: ./src/hibernate.cfg.xml
    [schemaexport] (cfg.Configuration 826 ) Configuration resource: ./src/hibernate.cfg.xml
    [schemaexport] (cfg.Configuration 830 ) ./src/hibernate.cfg.xml not found
    BUILD FAILED: E:\eclipse\workspace\InheritanceMapping\build.xml:97: Schema text failed: ./src/hibernate.cfg.xml not found
    Total time: 1 second
    #砖头 发表于2005-04-23 22:47:00  IP: 219.137.116.*
    运行“generate-hbm”任务已经产生hibernate.cfg.xml文件
    运行“schema-export”任务 多次
    就是没有产生schema-export.sql这个文件
    还是上面的
    BUILD FAILED: E:\eclipse\workspace\InheritanceMapping\build.xml:97: Schema text failed: ./src/hibernate.cfg.xml not found
    #jamy 发表于2005-04-24 19:35:00  IP: 218.22.21.*
    为什么我生成的Animal.hbm.xml文件没有
    <subclass
    name="javamxj.inheritance.one.Cat"
    dynamic-update="false"
    dynamic-insert="false"
    discriminator-value="Cat"
    >

    <property
    name="furColor"
    type="java.lang.String"
    update="true"
    insert="true"
    access="property"
    column="furColor"
    length="24"
    />

    <!--
    To add non XDoclet property mappings, create a file named
    hibernate-properties-Cat.xml
    containing the additional properties and place it in your merge dir.
    -->

    </subclass>
    <subclass
    name="javamxj.inheritance.one.Dog"
    dynamic-update="false"
    dynamic-insert="false"
    discriminator-value="Dog"
    >

    <property
    name="category"
    type="java.lang.String"
    update="true"
    insert="true"
    access="property"
    column="category"
    length="24"
    />

    #砖头 发表于2005-04-23 23:02:00  IP: 219.137.116.*
    qq:8897658
    #helpme 发表于2005-04-25 11:37:00  IP: 59.37.16.*
    向javamxj学习这种分享快乐,分享知识的精神!~~
    我有一个问题:在配置文件hibernate.cfg.xml中为mysql设置编码时的那个&amp有什么作用?
    #huaguol 发表于2005-04-27 15:13:00  IP: 211.155.226.*
    to :esmiles
    我也碰到跟你同样的问题,还有在bin目录下没有产生任何的文件,结果我把eclipse重新装了一下就ok了,所以我想肯定是把哪个文件破坏了.
    #huaguol 发表于2005-04-27 15:49:00  IP: 211.155.226.*
    javamxj:
    我改了Animal.hbm.xml的discriminator-value="Cat_1"发现数据库中的animal-type的值是为 Cat_1了
    但也出现了一个错误:
    net.sf.hibernate.WrongClassException: Object with id: 32769 was not of the specified subclass: javamxj.inheritance.one.Animal (Discriminator: Cat_1)
    at net.sf.hibernate.loader.Loader.getInstanceClass(Loader.java:660)
    at net.sf.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:573)
    at net.sf.hibernate.loader.Loader.getRow(Loader.java:505)
    at net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:218)
    at net.sf.hibernate.loader.Loader.doQuery(Loader.java:285)
    at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
    at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
    at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
    at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
    at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
    at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
    at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1523)
    at javamxj.inheritance.one.Demo.<init>(Demo.java:60)
    at javamxj.inheritance.one.Demo.main(Demo.java:21)
    ,然后我把它改回成"cat ",还是报相同的错误!
    请帮忙解决一下!
    #wyconfig 发表于2005-08-22 16:33:00  IP: 211.100.4.*
    请给你的文章的按照顺序加上:上一篇、下一篇的链接,我想这些大家看起来会方便一些,谢谢
    #月影煞人 发表于2006-05-23 17:43:00  IP: 222.201.178.*
    我做出来了,结果也都运行的了,就是在数据库输出的时候中文出现乱码,请教楼主呀,急~~~~~
    #面瓜 发表于2006-05-24 17:06:00  IP: 61.48.10.*
    控制台输出结果如下:
    Buildfile: d:\cvs\practice\ant\build.xml
    database-schema-export:
    [schemaexport] - Hibernate 3.1.3
    [schemaexport] - hibernate.properties not found
    [schemaexport] - using CGLIB reflection optimizer
    [schemaexport] - using JDK 1.4 java.sql.Timestamp handling
    [schemaexport] - configuring from file: hibernate.cfg.xml
    [schemaexport] - Reading mappings from resource: cn/mapping/User.hbm.xml

    BUILD FAILED
    D:\cvs\practice\ant\build.xml:76: Schema text failed: Resource: cn/mapping/User.hbm.xml not found
    想请问楼主是怎么解决,先谢谢!!
    #xiao li 发表于2006-06-01 16:07:00  IP: 125.96.83.*
    你的文章 简单明了 很容易读懂,谢谢
    #深蓝 发表于2006-08-18 15:26:00  IP: 221.234.210.*
    为什么提示这个问题啊!!!!!!

    Buildfile: D:\jb\MyEclipse\book\src\build.xml
    generate-hbm:
    [echo] 运行HibernateDoclet,生成 Hibernate 类的映射文件

    BUILD FAILED
    D:\jb\MyEclipse\book\src\build.xml:55: taskdef class xdoclet.modules.hibernate.HibernateDocletTask cannot be found
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © javamxj