hibernate框架无主键保存的实现
假如你现在是需要保存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; } }