在Spring JPA本机查询中使用Spel表达式时出错(将字符串转换为Hexa)

时间:2017-05-31 22:09:29

标签: spring-boot spring-data spring-data-jpa

我使用以下方法创建了一个存储库。

@Modifying(clearAutomatically = true)
@Query(
            value = "UPDATE address  SET address_line_1 = :#{#address.getAddressLine1()} , address_line_2 = :#{#address.getAddressLine2()} ," +
                    " address_line_3 = :#{#address.getAddressLine3()} , city = :#{#address.getCity()}  , address_type = :#{#address.getAddressType().toString()} ," +
                    " postal_code = :#{#address.getPostalCode()} , state = :#{#address.getState().toString()} , country= :#{#address.getCountry().toString()} , residence_type = :#{#address.getResidenceType().toString()}  ," +
                    " discriminator = :#{#address.getAddressType().toString()},created_by = :#{#address.getCreatedBy()} , created_date = :#{#address.getCreatedDateTime()}  WHERE address_id = :#{#address.getId()}",
            nativeQuery = true)
 void updateAddress(@Param("address") Address address);

在更新期间,addressLine2 / addressLine3将以hexa格式转换并存储在数据库中。

例如,如果将addressLine2作为 1OFFICE OF HOBBITS 传递给方法,则会将其转换为并存储为 \ x314f6666696365206f6620486f6262697473

这只发生在一些更新(并非全部)上。我无法辨别按预期更新的值和转换为hexa格式的值之间的可区分模式。

帮助!!

其他信息:

我甚至在没有Spel Expression的情况下尝试过,我看到同样的错误:

以下是一些更多细节:

存储库接口:

import com.company.domain.Address;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface AddressRepository extends JpaRepository<Address, Long> {

    @Modifying(clearAutomatically = true)
    @Query(
            value = "UPDATE address  SET address_line_1 = :address_line_1 , address_line_2 = :address_line_2 ," +
                    " address_line_3 = :address_line_3 WHERE address_id = :address_id ",
            nativeQuery = true)
    void updateAddress(@Param("address_line_1") String address_line_1,@Param("address_line_2") String address_line_2,@Param("address_line_3") String address_line_3,@Param("address_id") Long address_id);
}

服务类:

import com.company.hibernate.AddressRepository;
import com.company.domain.Address;
import com.company.domain.AddressService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Class by AddressServiceImpl
 */
@Service
public class AddressServiceImpl implements AddressService {


    @Autowired
    private AddressRepository addressRepository;

    @Override
    public void updateAddress(Address address) {
        System.out.println(address);
        System.out.println(address.getId());
        System.out.println(address.getAddressLine2());
        addressRepository.updateAddress(address.getAddressLine1() , address.getAddressLine2() ,
        address.getAddressLine3(),address.getId());
    }
}

当我调用服务方法时

addressService.updateAddress(地址);

我看到以下sysouts:

 {"addressId":2000112115,"addressLine1":"2001 Hussle
 Road2","addressLine2":"Office of Hobbits","addressLine3":null }

 2000112115 
 Office of Hobbits

但是在数据库中我看到:以下是address_line_2

\ x4f6666696365206f6620486f6262697473

更新2 - 添加了地址类:

package com.company.domain;

import com.company.common.Identifiable;
import com.company.enums.AddressType;
import com.company.ResidenceType;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.EqualsExclude;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.HashCodeExclude;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.time.LocalDateTime;


public class Address implements Identifiable<Long>, Serializable {

  private static final long serialVersionUID = 599052439022921076L;

  protected static final String INVALID_ADDRESS = "InvalidAddress";

  private Long addressId;
  private String addressLine1;
  private String addressLine2;
  private String addressLine3;

  @Override
  public Long getId() {
    return addressId;
  }

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

  public Customer getCustomer() {
    return customer;
  }


  public String getAddressLine1() {
    return addressLine1;
  }

  public void setAddressLine1(String addressLine1) {
    this.addressLine1 = addressLine1;
  }

  public String getAddressLine2() {
    return addressLine2;
  }

  public void setAddressLine2(String addressLine2) {
    this.addressLine2 = addressLine2;
  }

  public String getAddressLine3() {
    return addressLine3;
  }

  public void setAddressLine3(String addressLine3) {
    this.addressLine3 = addressLine3;
  }



  @Override
  public boolean equals(Object obj) {
    return EqualsBuilder.reflectionEquals(this, obj, false);
  }


  @Override
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this, false);
  }


  @Override
  public String toString() {
    return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
  }


}

0 个答案:

没有答案