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

标签 java spring hibernate

我有一个 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;
    }
}

最佳答案

是的,Spring或Hibernate中没有这样的事情。如果您需要检查重复项,我的意思是相同的信息是否已经存在,那么请确保您可以在 service 或 dao 类中编写查询。

关于java - 仅当确切实体不存在时才保存实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15162759/

相关文章:

java - 设置 HTTP 响应代码和 HTTP 方法

java - AngularJS + Spring + REST——发送多个文件和数据

java - 在 Spring 上实例化 EntityManager

java - 使用变量类名而不是大量的 if 子句?

java - 运行时错误。请让我理解我做错了什么

java - eclipse - 包 org.testng 不存在

java - SequenceInformation 检索不正确 - 结果集中缺少列

java - Java 上的 EL 技术

java - 抛出异常后使用当前 session

hibernate - 如何在使用 spring-boot-jpa 时对 ddl 的编码进行编码