Java 仅当精确实体不存在时保存实体';不存在

Java 仅当精确实体不存在时保存实体';不存在,java,spring,hibernate,Java,Spring,Hibernate,我有一个ShippingDestination实体,如果它不存在,我只希望保存一个新实体。它必须完全匹配 Hibernate或Spring能帮上忙吗,还是这部分都由我决定 @javax.persistence.Table(name = "Shipping_Destination", schema = "", catalog = "production_queue") @Entity public class ShippingDestination { private Integer id

我有一个
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;
    }
}
@javax.persistence.Table(name=“Shipping\u Destination”,schema=”“,catalog=“production\u queue”)
@实体
公共类目的地{
私有整数id;
@Column(name=“id”,nullable=false,insertable=true,updateable=true,length=10,precision=0)
@身份证
@生成值
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
私有字符串接收者;
@Column(name=“recipient”,nullable=false,insertable=true,updateable=true,length=32,precision=0)
@基本的
公共字符串getRecipient(){
返回收件人;
}
@JsonProperty(“收件人”)
公共空集合收件人(字符串收件人){
this.recipient=收件人;
}
私家弦街;;
@Column(name=“street”,nullable=false,insertable=true,updateable=true,length=255,precision=0)
@基本的
公共字符串getStreet(){
返回街;;
}
@JsonProperty(“街道”)
公共街道(字符串街){
这条街;
}
私家弦街2号;
@Column(name=“street2”,nullable=false,insertable=true,updateable=true,length=255,precision=0)
@基本的
公共字符串getStreet2(){
返回街2号;
}
@JsonProperty(“街道2”)
公共空设置街2(字符串街2){
this.street2=street2;
}
私人城市;
@Column(name=“city”,nullable=false,insertable=true,updateable=true,length=128,precision=0)
@基本的
公共字符串getCity(){
回归城市;
}
@JsonProperty(“城市”)
公共城市(字符串城市){
this.city=城市;
}
私有字符串状态;
@Column(name=“state”,nullable=false,insertable=true,updateable=true,length=2,precision=0)
@基本的
公共字符串getState(){
返回状态;
}
@JsonProperty(“州”)
公共无效设置状态(字符串状态){
this.state=状态;
}
专用字符串后代码;
@Column(name=“postal_code”,nullable=false,insertable=true,updateable=true,length=12,precision=0)
@基本的
公共字符串getPostalCode(){
返回后代码;
}
@JsonProperty(“PostalCode”)
公共无效setPostalCode(字符串postalCode){
this.postalCode=postalCode;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
ShippingDestination that=(ShippingDestination)o;
如果(city!=null?!city.equals(that.city):that.city!=null)返回false;
如果(id!=null?!id.equals(that.id):that.id!=null)返回false;
如果(postalCode!=null?!postalCode.equals(that.postalCode):that.postalCode!=null)返回false;
如果(recipient!=null?!recipient.equals(that.recipient):that.recipient!=null)返回false;
如果(state!=null?!state.equals(that.state):that.state!=null)返回false;
如果(street!=null?!street.equals(that.street):that.street!=null)返回false;
返回true;
}
@凌驾
公共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);
结果=31*result+(postalCode!=null?postalCode.hashCode():0);
返回结果;
}
私人托收令;
@OneToMany(mappedBy=“shippingDestination”)
公开募捐令(){
退货订单;
}
公共作废集合订单(托收订单){
这个。订单=订单;
}
私人收藏品供应商;
@OneToMany(mappedBy=“shippingDestination”)
公共集合getVendors(){
退货供应商;
}
公共供应商(收款供应商){
这是。供应商=供应商;
}
public boolean validate()引发InvalidParameterException{
if(getRecipient()==null | | getRecipient().length()==0){
抛出新的InvalidParameterException(“地址需要收件人”);
}
如果(getStreet()==null | | getStreet().length()==0){
抛出新的InvalidParameterException(“地址需要街道”);
}
如果(getCity()==null | | getCity().length()==0){
抛出新的InvalidParameterException(“地址需要一个城市”);
}
如果(getState()==null | | getState().length()==0){
抛出新的InvalidParameterException(“地址需要状态”);
}
如果(getPostalCode()==null | | getPostalCode().length()==0){
抛出新的InvalidParameterException(“地址需要邮政编码”);
}
返回true;
}
}

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

如果您尝试插入具有相同id的查询,则会出现异常。这就是你的意思吗?当你说它必须匹配时,你是什么意思?使用散列生成器和具有您关心的参数的equals生成器,hibernate应该处理它。但是,除此之外,是什么阻止了你