hibernate框架无主键保存的实现

本文介绍了EcCProperty实体类的定义及其实现细节。该类用于保存组件属性信息,并通过EmbeddedId注解实现了复合主键。同时,还定义了与其对应的EcCPropertyId类。

假如你现在是需要保存EcCProperty,则代码可以如下配置:

package com.asiainfo.easyconfig2.entity;

import java.io.Serializable;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
 * 
 * @author gushun
 * @table 组件属性表
 * @time 2016/07/15
 *
 */
@Entity
@Table(name  = "ec_c_property")
public class EcCProperty implements Serializable {

    private static final long serialVersionUID = 2370835650380167533L;
    public static final String ID = "id";
    
    private EcCPropertyId id;

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "propertyId", column = @Column(name = "PROPERTY_ID")),
            @AttributeOverride(name = "componentId", column = @Column(name = "COMPONENT_ID")) })
    public EcCPropertyId getId() {
        return id;
    }

    public void setId(EcCPropertyId id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "EcCProperty [id=" + id + "]";
    }
    
}
package com.asiainfo.easyconfig2.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

@Embeddable
public class EcCPropertyId  implements Serializable {
    
    private static final long serialVersionUID = 1L;

    public final static String PROPERTY = "property";
    public final static String COMPONENT_ID = "componentId";
    
    private SysProperty property;
    private Integer componentId;
    
    public EcCPropertyId(SysProperty property, Integer componentId) {
        super();
        this.setProperty(property);
        this.componentId = componentId;
    }
    public EcCPropertyId() {
        super();
    }
    
    @Column(name = "COMPONENT_ID")
    public Integer getComponentId() {
        return componentId;
    }
    public void setComponentId(Integer componentId) {
        this.componentId = componentId;
    }
    
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((componentId == null) ? 0 : componentId.hashCode());
        result = prime * result + ((property.getSyspId() == null) ? 0 : property.getSyspId().hashCode());
        return result;
    }
    
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        EcCPropertyId other = (EcCPropertyId) obj;
        if (componentId == null) {
            if (other.componentId != null)
                return false;
        } else if (!componentId.equals(other.componentId))
            return false;
        if (property.getSyspId() == null) {
            if (other.property.getSyspId() != null)
                return false;
        } else if (!property.getSyspId().equals(other.property.getSyspId()))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "EcCPropertyId [propertyId=" + property.getSyspId() + ", componentId="
                + componentId + "]";
    }
    
    @ManyToOne
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name = "PROPERTY_ID",referencedColumnName = "SYSP_ID", nullable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    public SysProperty getProperty() {
        return property;
    }
    public void setProperty(SysProperty property) {
        this.property = property;
    }
    
    
}

 

转载于:https://www.cnblogs.com/mengzhongyunying/p/8721334.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值