`
tanlingcau
  • 浏览: 138172 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate 映射视图的联合主键

阅读更多
Hibernate 映射视图会生成联合主键.在查询时,如果联合主键里有一项值为null,则整个结果返回null.

原文件:由Hibernate生成:
V4cDiseasesprevention.java
package com.SAFAS.mobilecatch.server.hibernate.model;    
   
/**   
 * V4cDiseasesprevention entity.   
 *    
 * @author MyEclipse Persistence Tools   
 */   
   
public class V4cDiseasesprevention implements java.io.Serializable {    
   
    // Fields    
   
    private V4cDiseasespreventionId id;    
   
    // Constructors    
   
    /** default constructor */   
    public V4cDiseasesprevention() {    
    }    
   
    /** full constructor */   
    public V4cDiseasesprevention(V4cDiseasespreventionId id) {    
        this.id = id;    
    }    
   
    // Property accessors    
   
    public V4cDiseasespreventionId getId() {    
        return this.id;    
    }    
   
    public void setId(V4cDiseasespreventionId id) {    
        this.id = id;    
    }    
   
}  


V4cDiseasespreventionId.java
package com.SAFAS.mobilecatch.server.hibernate.model;    
   
/**   
 * V4cDiseasespreventionId entity.   
 *    
 * @author MyEclipse Persistence Tools   
 */   
   
public class V4cDiseasespreventionId implements java.io.Serializable {    
   
    // Fields    
   
    private Integer produceClassify4id;    
    private String produceClassify4name;    
    private Short qualityStandardId;    
    private Short environmentStandardId;    
    private Short producingRuleId;    
    private Short produceSampleStandardId;    
    private String producingRuleCode;    
    private String producingRuleName;    
    private String remark;    
    private String pesticideName;    
    private String preventionMethod;    
   
    // Constructors    
   
    /** default constructor */   
    public V4cDiseasespreventionId() {    
    }    
   
    /** minimal constructor */   
    public V4cDiseasespreventionId(Integer produceClassify4id) {    
        this.produceClassify4id = produceClassify4id;    
    }    
   
    /** full constructor */   
    public V4cDiseasespreventionId(Integer produceClassify4id,    
            String produceClassify4name, Short qualityStandardId,    
            Short environmentStandardId, Short producingRuleId,    
            Short produceSampleStandardId, String producingRuleCode,    
            String producingRuleName, String remark, String pesticideName,    
            String preventionMethod) {    
        this.produceClassify4id = produceClassify4id;    
        this.produceClassify4name = produceClassify4name;    
        this.qualityStandardId = qualityStandardId;    
        this.environmentStandardId = environmentStandardId;    
        this.producingRuleId = producingRuleId;    
        this.produceSampleStandardId = produceSampleStandardId;    
        this.producingRuleCode = producingRuleCode;    
        this.producingRuleName = producingRuleName;    
        this.remark = remark;    
        this.pesticideName = pesticideName;    
        this.preventionMethod = preventionMethod;    
    }    
   
    // Property accessors    
   
    public Integer getProduceClassify4id() {    
        return this.produceClassify4id;    
    }    
   
    public void setProduceClassify4id(Integer produceClassify4id) {    
        this.produceClassify4id = produceClassify4id;    
    }    
   
    public String getProduceClassify4name() {    
        return this.produceClassify4name;    
    }    
   
    public void setProduceClassify4name(String produceClassify4name) {    
        this.produceClassify4name = produceClassify4name;    
    }    
   
    public Short getQualityStandardId() {    
        return this.qualityStandardId;    
    }    
   
    public void setQualityStandardId(Short qualityStandardId) {    
        this.qualityStandardId = qualityStandardId;    
    }    
   
    public Short getEnvironmentStandardId() {    
        return this.environmentStandardId;    
    }    
   
    public void setEnvironmentStandardId(Short environmentStandardId) {    
        this.environmentStandardId = environmentStandardId;    
    }    
   
    public Short getProducingRuleId() {    
        return this.producingRuleId;    
    }    
   
    public void setProducingRuleId(Short producingRuleId) {    
        this.producingRuleId = producingRuleId;    
    }    
   
    public Short getProduceSampleStandardId() {    
        return this.produceSampleStandardId;    
    }    
   
    public void setProduceSampleStandardId(Short produceSampleStandardId) {    
        this.produceSampleStandardId = produceSampleStandardId;    
    }    
   
    public String getProducingRuleCode() {    
        return this.producingRuleCode;    
    }    
   
    public void setProducingRuleCode(String producingRuleCode) {    
        this.producingRuleCode = producingRuleCode;    
    }    
   
    public String getProducingRuleName() {    
        return this.producingRuleName;    
    }    
   
    public void setProducingRuleName(String producingRuleName) {    
        this.producingRuleName = producingRuleName;    
    }    
   
    public String getRemark() {    
        return this.remark;    
    }    
   
    public void setRemark(String remark) {    
        this.remark = remark;    
    }    
   
    public String getPesticideName() {    
        return this.pesticideName;    
    }    
   
    public void setPesticideName(String pesticideName) {    
        this.pesticideName = pesticideName;    
    }    
   
    public String getPreventionMethod() {    
        return this.preventionMethod;    
    }    
   
    public void setPreventionMethod(String preventionMethod) {    
        this.preventionMethod = preventionMethod;    
    }    
   
    public boolean equals(Object other) {    
        if ((this == other))    
            return true;    
        if ((other == null))    
            return false;    
        if (!(other instanceof V4cDiseasespreventionId))    
            return false;    
        V4cDiseasespreventionId castOther = (V4cDiseasespreventionId) other;    
   
        return ((this.getProduceClassify4id() == castOther    
                .getProduceClassify4id()) || (this.getProduceClassify4id() != null   
                && castOther.getProduceClassify4id() != null && this   
                .getProduceClassify4id().equals(    
                        castOther.getProduceClassify4id())))    
                && ((this.getProduceClassify4name() == castOther    
                        .getProduceClassify4name()) || (this   
                        .getProduceClassify4name() != null   
                        && castOther.getProduceClassify4name() != null && this   
                        .getProduceClassify4name().equals(    
                                castOther.getProduceClassify4name())))    
                && ((this.getQualityStandardId() == castOther    
                        .getQualityStandardId()) || (this   
                        .getQualityStandardId() != null   
                        && castOther.getQualityStandardId() != null && this   
                        .getQualityStandardId().equals(    
                                castOther.getQualityStandardId())))    
                && ((this.getEnvironmentStandardId() == castOther    
                        .getEnvironmentStandardId()) || (this   
                        .getEnvironmentStandardId() != null   
                        && castOther.getEnvironmentStandardId() != null && this   
                        .getEnvironmentStandardId().equals(    
                                castOther.getEnvironmentStandardId())))    
                && ((this.getProducingRuleId() == castOther    
                        .getProducingRuleId()) || (this.getProducingRuleId() != null   
                        && castOther.getProducingRuleId() != null && this   
                        .getProducingRuleId().equals(    
                                castOther.getProducingRuleId())))    
                && ((this.getProduceSampleStandardId() == castOther    
                        .getProduceSampleStandardId()) || (this   
                        .getProduceSampleStandardId() != null   
                        && castOther.getProduceSampleStandardId() != null && this   
                        .getProduceSampleStandardId().equals(    
                                castOther.getProduceSampleStandardId())))    
                && ((this.getProducingRuleCode() == castOther    
                        .getProducingRuleCode()) || (this   
                        .getProducingRuleCode() != null   
                        && castOther.getProducingRuleCode() != null && this   
                        .getProducingRuleCode().equals(    
                                castOther.getProducingRuleCode())))    
                && ((this.getProducingRuleName() == castOther    
                        .getProducingRuleName()) || (this   
                        .getProducingRuleName() != null   
                        && castOther.getProducingRuleName() != null && this   
                        .getProducingRuleName().equals(    
                                castOther.getProducingRuleName())))    
                && ((this.getRemark() == castOther.getRemark()) || (this   
                        .getRemark() != null   
                        && castOther.getRemark() != null && this.getRemark()    
                        .equals(castOther.getRemark())))    
                && ((this.getPesticideName() == castOther.getPesticideName()) || (this   
                        .getPesticideName() != null   
                        && castOther.getPesticideName() != null && this   
                        .getPesticideName()    
                        .equals(castOther.getPesticideName())))    
                && ((this.getPreventionMethod() == castOther    
                        .getPreventionMethod()) || (this.getPreventionMethod() != null   
                        && castOther.getPreventionMethod() != null && this   
                        .getPreventionMethod().equals(    
                                castOther.getPreventionMethod())));    
    }    
   
    public int hashCode() {    
        int result = 17;    
   
        result = 37   
                * result    
                + (getProduceClassify4id() == null ? 0 : this   
                        .getProduceClassify4id().hashCode());    
        result = 37   
                * result    
                + (getProduceClassify4name() == null ? 0 : this   
                        .getProduceClassify4name().hashCode());    
        result = 37   
                * result    
                + (getQualityStandardId() == null ? 0 : this   
                        .getQualityStandardId().hashCode());    
        result = 37   
                * result    
                + (getEnvironmentStandardId() == null ? 0 : this   
                        .getEnvironmentStandardId().hashCode());    
        result = 37   
                * result    
                + (getProducingRuleId() == null ? 0 : this.getProducingRuleId()    
                        .hashCode());    
        result = 37   
                * result    
                + (getProduceSampleStandardId() == null ? 0 : this   
                        .getProduceSampleStandardId().hashCode());    
        result = 37   
                * result    
                + (getProducingRuleCode() == null ? 0 : this   
                        .getProducingRuleCode().hashCode());    
        result = 37   
                * result    
                + (getProducingRuleName() == null ? 0 : this   
                        .getProducingRuleName().hashCode());    
        result = 37 * result    
                + (getRemark() == null ? 0 : this.getRemark().hashCode());    
        result = 37   
                * result    
                + (getPesticideName() == null ? 0 : this.getPesticideName()    
                        .hashCode());    
        result = 37   
                * result    
                + (getPreventionMethod() == null ? 0 : this   
                        .getPreventionMethod().hashCode());    
        return result;    
    }    
   
}  


映射文件:V4cDiseasesprevention.hbm.xml
<?xml version="1.0" encoding="utf-8"?>   
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
<!--    
    Mapping file autogenerated by MyEclipse Persistence Tools   
-->   
<hibernate-mapping>   
    <class name="com.SAFAS.mobilecatch.server.hibernate.model.V4cDiseasesprevention" table="v_4c_diseasesprevention" catalog="safas">   
        <composite-id name="id" class="com.SAFAS.mobilecatch.server.hibernate.model.V4cDiseasespreventionId">   
            <key-property name="produceClassify4id" type="java.lang.Integer">   
                <column name="produceClassify_4ID" />   
            </key-property>   
            <key-property name="produceClassify4name" type="java.lang.String">   
                <column name="produceClassify_4Name" length="20" />   
            </key-property>   
            <key-property name="qualityStandardId" type="java.lang.Short">   
                <column name="qualityStandardID" />   
            </key-property>   
            <key-property name="environmentStandardId" type="java.lang.Short">   
                <column name="environmentStandardID" />   
            </key-property>   
            <key-property name="producingRuleId" type="java.lang.Short">   
                <column name="producingRuleID" />   
            </key-property>   
            <key-property name="produceSampleStandardId" type="java.lang.Short">   
                <column name="produceSampleStandardID" />   
            </key-property>   
            <key-property name="producingRuleCode" type="java.lang.String">   
                <column name="producingRuleCode" />   
            </key-property>   
            <key-property name="producingRuleName" type="java.lang.String">   
                <column name="producingRuleName" length="50" />   
            </key-property>   
            <key-property name="remark" type="java.lang.String">   
                <column name="remark" />   
            </key-property>   
            <key-property name="pesticideName" type="java.lang.String">   
                <column name="pesticideName" length="50" />   
            </key-property>   
            <key-property name="preventionMethod" type="java.lang.String">   
                <column name="preventionMethod" />   
            </key-property>   
        </composite-id>   
    </class>   
</hibernate-mapping> 


解决方案:把视图当作表处理
1.将将V4cDiseasesprevention.java的属性全部替换为V4cDiseasespreventionId.java的,以及它的get,set方法.
2.删除V4cDiseasespreventionId.java;
3.修改映射文件为:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.SAFAS.mobilecatch.server.hibernate.model.V4cDiseasesprevention" table="v_4c_diseasesprevention" catalog="safas">
        <id name="produceClassify4id" type="java.lang.Integer">
            <column name="produceClassify_4ID" />
        </id>
            <property name="produceClassify4name" type="java.lang.String">
                <column name="produceClassify_4Name" length="20" />
            </property>
            <property name="qualityStandardId" type="java.lang.Short">
                <column name="qualityStandardID" />
            </property>
            <property name="environmentStandardId" type="java.lang.Short">
                <column name="environmentStandardID" />
            </property>
            <property name="producingRuleId" type="java.lang.Short">
                <column name="producingRuleID" />
            </property>
            <property name="produceSampleStandardId" type="java.lang.Short">
                <column name="produceSampleStandardID" />
            </property>
            <property name="producingRuleCode" type="java.lang.String">
                <column name="producingRuleCode" />
            </property>
            <property name="producingRuleName" type="java.lang.String">
                <column name="producingRuleName" length="50" />
            </property>
            <property name="remark" type="java.lang.String">
                <column name="remark" />
            </property>
            <property name="pesticideName" type="java.lang.String">
                <column name="pesticideName" length="50" />
            </property>
            <property name="preventionMethod" type="java.lang.String">
                <column name="preventionMethod" />
            </property>
    </class>
</hibernate-mapping>

分享到:
评论
2 楼 tanlingcau 2010-10-19  
这个我已经完全记不清楚了,而且项目也没有留档,不好意思。
1 楼 livexu 2010-10-19  
<id name="produceClassify4id" type="java.lang.Integer"> 
            <column name="produceClassify_4ID" /> 
</id>

是唯一的字段?
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    struts2+hibernate+spring整合

    它支持实体类(Entity)、持久化字段(Field)、主键(Id)、一对多(OneToMany)、多对一(ManyToOne)、多对多(ManyToMany)等关系映射。 3. Spring:Spring框架是Java企业级应用的核心。它提供了一个容器...

    Struts、Hibernate、Spring经典面试题收藏

    - **一对一关系**:可以通过主键联合或者外键关联两种方式实现。 - **一对多关系**:通常是通过外键关联的方式实现,即在“多”的一方添加指向“一”的外键字段。 - **多对多关系**:通常需要一个中间表来关联...

    Struts、hibernate、jquey、oracle项目整合

    多表操作则可能涵盖多对多关联,需要理解联合主键、中间表等概念。 在实际项目中,整合这些技术的关键在于配置和协调。例如,Struts 需要配置 Action 映射和结果页面,Hibernate 需要配置实体类和映射文件,而 ...

    Struts,Spring,Hibernate三大框架的面试题目

    - **一对一关系:** 表示两个实体之间存在一对一的关联,通常通过主键外键的方式实现,也可以通过联合主键的方式表示。 - **一对多关系:** 一个实体对应多个实体的关系,可以通过外键表示。在 Hibernate 中,可以...

    SpringMVC+Hibernate+Jpa Maven环境搭建

    在IT行业中,构建高效、可维护的Web应用是至关重要的,SpringMVC、Hibernate和JPA作为Java领域中三大主流框架,常常被联合使用来实现这一目标。本篇将详细介绍如何利用Maven来搭建整合SpringMVC、Hibernate和JPA的...

    Hibernate多表查询.txt

    例如,在示例代码中,通过`from tBookInfo book, BookSelection sel where book.id = sel.bookId`这样的HQL语句,实现了对“书籍”和“选择”两个表的联合查询,并根据主键与外键的关系进行了匹配。 #### 2. 构建...

    2014—SHH面试题

    - **联合主键**:在Hibernate中,可以通过创建一个类来表示联合主键,并实现Serializable接口,重写equals()和hashCode()方法。在配置文件中使用`&lt;composite-id&gt;`替代`&lt;id&gt;`标签,指定类路径。 - **load()与get()...

    Hibernate-Many-to-many-Relationship-in-Spring-MVC:展示如何在 Spring MVC HIbernate 项目中建立多对多关系的示例项目

    实体类需要包含双向@ManyToMany注解,而映射文件则需指定中间表的配置,包括联合主键等。 3. **JPA注解**:在Java中,使用Java Persistence API (JPA) 的@ManyToMany注解可以声明多对多关系。这个注解会在两个关联...

    sh框架新闻发布系统

    字段可能为`user_id`和`role_id`,两者作为联合主键。 6. **评论表**:如果系统支持用户评论,这个表将存储评论内容、评论者、被评论的新闻ID等。字段可能包括`comment_id`(主键)、`content`、`user_id`、`news_...

    Java 亚信上海 面试题

    **Hibernate的联合主键:** - 通过定义复合主键来唯一标识一个实体对象。 - 可以是多个字段的组合。 **缓存框架:** - 如Ehcache、OSCache等,用于改善应用性能。 - 支持数据的高速读取和写入。 **iBATIS与...

    JPA技术:年初写的办公室OA系统

    4. **持久化上下文(Persistence Context)**: 存储了实体的内存视图,管理实体的状态和生命周期。在事务范围内,实体的修改会被自动同步到数据库。 ### JPA核心特性 1. **对象/关系映射(ORM)**: 使用注解或XML...

    Spring.3.x企业应用开发实战(完整版).part2

    10.5 联合军种作战的混乱 10.5.1 Spring事务管理器的应对 10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接...

    Spring3.x企业应用开发实战(完整版) part1

    10.5 联合军种作战的混乱 10.5.1 Spring事务管理器的应对 10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接...

    oracle入门基础教程全面详细

    - **对象关系映射**:介绍对象与关系型数据库之间的映射关系。 - **对象关系数据库**:学习对象关系数据库的特性和优势。 - **ORM框架**:掌握Hibernate、JPA等ORM框架的应用。 #### 二十八、其他数据库 - **MySQL*...

    springmybatis

    MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录. orm工具的基本思想 无论是用过的hibernate,mybatis,你都可以法相他们有一个...

    JAVA上百实例源码以及开源项目源代码

    创建发送者和映射消息。发送消息,同时对文本进行少量修改,发送end-of-messages消息,最后关闭连接。 Tcp服务端与客户端的JAVA实例源代码 2个目标文件 摘要:Java源码,文件操作,TCP,服务器 Tcp服务端与客户端的JAVA...

Global site tag (gtag.js) - Google Analytics