【问题标题】:Spring error converting nvarchar to bigint将 nvarchar 转换为 bigint 的 Spring 错误
【发布时间】:2015-07-23 03:12:27
【问题描述】:

每当我尝试保存对象时都会出错。

导致错误的字段是在数据库中映射到 varchar(200)string
该字段的名称是localContact

这是我的代码:

Site.java:

@Entity
@Table(name = "SITE", uniqueConstraints = { @UniqueConstraint(columnNames = { "SITE_COUNTRY_ID", "SITE_NAME" }) })
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED, withModifiedFlag = true)
public class Site implements ISite {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = -390717603276436784L;

/** The id. */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SITE_ID", unique = true, nullable = false)
private long id;

/** The site address. */
@Column(name = "SITE_ADDRESS", length = BusinessConstants.SITE_ADDRESS)
private String address;

/** The site analog phone number. */
@Column(name = "SITE_ANALOG_PHONE_NUMBER", length = BusinessConstants.SITE_ANALOG_PHONE_NUMBER)
private String analogPhoneNumber;

/** The site comment. */
@Column(name = "SITE_COMMENT", length = BusinessConstants.SITE_COMMENT)
private String comment;

/** The site entity code. */
@Digits(integer = 3, fraction = 0, message = "Please enter max 3 digits")
@Column(name = "SITE_ENTITY_CODE", nullable = false)
private long entityCode;

/** The site invoice code. */
@Digits(integer = 10, fraction = 0, message = "Please enter max 10 digits")
@Column(name = "SITE_INVOICE_CODE", nullable = false)
private long invoiceCode;

/** The site local it phone. */
@Column(name = "SITE_LOCAL_IT_PHONE", length = BusinessConstants.SITE_LOCAL_IT_PHONE)
private String localItPhone;

/** The site name. */
@NotBlank
@Column(name = "SITE_NAME", nullable = false, length = BusinessConstants.SITE_NAME)
private String siteName;

/** The site subnet. */
@NotBlank
@Column(name = "SITE_SUBNET", nullable = false, length = BusinessConstants.SITE_SUBNET)
private String subnet;

/** The site user number. */
@Digits(integer = 4, fraction = 0, message = "Please enter max 4 digits")
@Column(name = "SITE_USER_NUMBER")
private Long userNumber;

/** The business. */
@Valid
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SITE_BUSINESS_ID", nullable = false)
private Business business;

/** The country. */
@Valid
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "SITE_COUNTRY_ID")
private Country country;

/** The local contact. */
@Column(name = "SITE_LOCAL_CONTACT", nullable = true, length = BusinessConstants.LOCAL_CONTACT)
private String localContact; //the field that cause the error

/** The local it. */
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SITE_LOCAL_IT", nullable = true)
private User localIt;

/** The rif. */
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SITE_RIF", nullable = true)
private User rif;

/** The status. */
@Valid
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SITE_STATUS_ID", nullable = false)
private Status status;

/** The end date. */
@Column(name = "SITE_END_DATE")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(iso = ISO.DATE_TIME)
private Date endDate = null;

@ManyToMany(targetEntity = User.class, mappedBy = "userSites", fetch = FetchType.LAZY)
@NotAudited
private Set<IUser> siteUsers;

/**
 * Gets the local contact.
 *
 * @return the local contact
 */
public String getLocalContact() {
    return this.localContact;
}

/**
 * Sets the local contact.
 *
 * @param localIt the new local contact
 */
public void setLocalContact(String localContact) {
    this.localContact = localContact;
}

我的jsp:

<td class="label"><spring:message code="site.localcontact" /></td>
        <td class="value">
            <form:input path="site.localContact"></form:input>
        </td>

我的 DAO:

public void saveSite(ISite siteObj){
    Assert.notNull(siteObj);
    merge((Site) siteObj);
}

编辑

抛出的错误是:

原因:com.microsoft.sqlserver.jdbc.SQLServerException: 将数据类型 nvarchar 转换为 bigint 时出错

【问题讨论】:

  • 完整的错误信息是什么?
  • @LaurentiuL。原因:com.microsoft.sqlserver.jdbc.SQLServerException:将数据类型 nvarchar 转换为 bigint 时出错。
  • 在某个地方,一个字符串被解析为一个 bigint 列。我怀疑您没有正确匹配数据类型。在 db 表上尝试 sp_help、sp_helptable。我认为您可以将 columnn 用作字符串,但您必须确保它有一个数字。否则,更改类型。但请发布表格说明
  • @Laurentiu L. 根据 sp_help,类型为 varchar(200)
  • 您如何确定是现场本地联系人造成的?我在错误消息中没有看到它。

标签: java spring type-conversion varchar bigint


【解决方案1】:

我发现了错误,

我有另一个表记录了我的 SITE 的历史,名为 SITE_AUD,其中的字段 SITE_LOCAL_CONTACT 是一个 bigint。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 2011-08-15
    • 2017-05-06
    • 1970-01-01
    相关资源
    最近更新 更多