简体   繁体   English

仅当确切的实体不存在时才保存实体

[英]Save entity only if exact entity doesn't exist

I have a ShippingDestination entity that I only want a new one saved if it doesn't already exist. 我有一个ShippingDestination实体,我只想保存一个不存在的新实体。 It must match exactly. 它必须完全匹配。

Will Hibernate or Spring be able to help with this, or is this portion all up to me? 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;
    }
}

yes, there is no such thing in spring or hibernate. 是的,春天或冬眠都没有这种事情。 If you need to check for duplicate i mean whether same information already exist or not then sure you can write a query in service or dao class. 如果您需要检查重复项,我的意思是说是否已经存在相同的信息,那么请确保您可以在service或dao类中编写查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM