2013-03-01 19 views
0

我有一个ShippingDestination实体,我只想要一个新的实体,如果它尚不存在。它必须完全匹配。只有当确切的实体不存在时才保存实体

请问Hibernate或Spring能够帮助解决这个问题,还是这个部分完全取决于我?

@javax.persistence.Table(name = "Shipping_Destination", schema = "", catalog = "production_queue") 
@Entity 
public class ShippingDestination { 
    private Integer id; 

    @javax.persistence.Column(name = "id", nullable = false, insertable = true, updatable = true, length = 10, precision = 0) 
    @Id 
    @GeneratedValue 
    public Integer getId() { 
     return id; 
    } 

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

    private String recipient; 

    @javax.persistence.Column(name = "recipient", nullable = false, insertable = true, updatable = true, length = 32, precision = 0) 
    @Basic 
    public String getRecipient() { 
     return recipient; 
    } 

    @JsonProperty("Recipient") 
    public void setRecipient(String recipient) { 
     this.recipient = recipient; 
    } 

    private String street; 

    @javax.persistence.Column(name = "street", nullable = false, insertable = true, updatable = true, length = 255, precision = 0) 
    @Basic 
    public String getStreet() { 
     return street; 
    } 

    @JsonProperty("Street") 
    public void setStreet(String street) { 
     this.street = street; 
    } 

    private String street2; 

    @javax.persistence.Column(name = "street2", nullable = false, insertable = true, updatable = true, length = 255, precision = 0) 
    @Basic 
    public String getStreet2() { 
     return street2; 
    } 

    @JsonProperty("Street2") 
    public void setStreet2(String street2) { 
     this.street2 = street2; 
    } 

    private String city; 

    @javax.persistence.Column(name = "city", nullable = false, insertable = true, updatable = true, length = 128, precision = 0) 
    @Basic 
    public String getCity() { 
     return city; 
    } 

    @JsonProperty("City") 
    public void setCity(String city) { 
     this.city = city; 
    } 

    private String state; 

    @javax.persistence.Column(name = "state", nullable = false, insertable = true, updatable = true, length = 2, precision = 0) 
    @Basic 
    public String getState() { 
     return state; 
    } 

    @JsonProperty("State") 
    public void setState(String state) { 
     this.state = state; 
    } 

    private String postalCode; 

    @javax.persistence.Column(name = "postal_code", nullable = false, insertable = true, updatable = true, length = 12, precision = 0) 
    @Basic 
    public String getPostalCode() { 
     return postalCode; 
    } 

    @JsonProperty("PostalCode") 
    public void setPostalCode(String postalCode) { 
     this.postalCode = postalCode; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     ShippingDestination that = (ShippingDestination) o; 

     if (city != null ? !city.equals(that.city) : that.city != null) return false; 
     if (id != null ? !id.equals(that.id) : that.id != null) return false; 
     if (postalCode != null ? !postalCode.equals(that.postalCode) : that.postalCode != null) return false; 
     if (recipient != null ? !recipient.equals(that.recipient) : that.recipient != null) return false; 
     if (state != null ? !state.equals(that.state) : that.state != null) return false; 
     if (street != null ? !street.equals(that.street) : that.street != null) return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     int result = id != null ? id.hashCode() : 0; 
     result = 31 * result + (recipient != null ? recipient.hashCode() : 0); 
     result = 31 * result + (street != null ? street.hashCode() : 0); 
     result = 31 * result + (city != null ? city.hashCode() : 0); 
     result = 31 * result + (state != null ? state.hashCode() : 0); 
     result = 31 * result + (postalCode != null ? postalCode.hashCode() : 0); 
     return result; 
    } 

    private Collection<Order> orders; 

    @OneToMany(mappedBy = "shippingDestination") 
    public Collection<Order> getOrders() { 
     return orders; 
    } 

    public void setOrders(Collection<Order> orders) { 
     this.orders = orders; 
    } 

    private Collection<Vendor> vendors; 

    @OneToMany(mappedBy = "shippingDestination") 
    public Collection<Vendor> getVendors() { 
     return vendors; 
    } 

    public void setVendors(Collection<Vendor> vendors) { 
     this.vendors = vendors; 
    } 

    public boolean validate() throws InvalidParameterException { 
     if (getRecipient() == null || getRecipient().length() == 0) { 
      throw new InvalidParameterException("Address requires a recipient"); 
     } 

     if (getStreet() == null || getStreet().length() == 0) { 
      throw new InvalidParameterException("Address requires a street"); 
     } 

     if (getCity() == null || getCity().length() == 0) { 
      throw new InvalidParameterException("Address requires a city"); 
     } 

     if (getState() == null || getState().length() == 0) { 
      throw new InvalidParameterException("Address requires a state"); 
     } 

     if (getPostalCode() == null || getPostalCode().length() == 0) { 
      throw new InvalidParameterException("Address requires a postal code"); 
     } 

     return true; 
    } 
} 
+0

如果你尝试插入一个具有相同ID的人,你会得到一个异常。你是这个意思吗? – 2013-03-01 16:52:30

+0

当你说它必须匹配时,你是什么意思?使用散列构建器并使用您关心的参数以​​及hibernate应该处理的参数来构建构建器。 – 2013-03-01 16:52:56

+0

从数据库设计的角度来看,这看起来像是严重的代码异味。 ***但是,除了这一点之外,阻止你将唯一索引放在你的表上的组合列必须只出现一次? – Perception 2013-03-01 16:53:50

回答

1

是的,有在春天或休眠状态没有这样的事情。如果你需要检查重复是否意味着相同的信息是否已经存在,那么确定你可以在服务或dao类中编写查询。