【问题标题】:Under Spring Boot, Hibernate says a row in table does not exist when it does在Spring Boot下,Hibernate说表中的一行不存在
【发布时间】:2017-12-27 23:36:09
【问题描述】:

我正在使用 JPA -> Hibernate -> JDBC -> Oracle 运行一个简单的查询。

查询基本上是:

select * from mstrgenstate where stateshort='TX';

当我在 Oracle 级别运行它时,我得到:

   STATEID  COUNTRYID STA STATEMED        STATELONG
---------- ---------- --- --------------- ------------------------------
         1          1 TX  Texas           Texas

当我使用 JPA 和 Hibernate 在 Java 中运行它时,我发现表中不存在任何记录。下面列出了我收到的消息(启用了跟踪)。实体代码 (Mstrgenstate.java) 是使用 Hibernate 生成的。数据访问代码 (MstrgenstateDAO.java) 正在使用 CrudRepository

为了从数据库中取出数据,是否需要做一些特殊的事情?

TIA

更新 我添加到 DAO 文件中:

public List<Mstrgenstate> findByStateid ( BigDecimal stateid );

对于控制器:

List<Mstrgenstate> testme = statedao.findByStateid(new BigDecimal(1));
System.out.println("first test here " + testme.size());

我得到了以下结果 - 它有效

2017-07-21 15:46:13.344 DEBUG 10496 --- [nio-8080-exec-1] org.hibernate.SQL                        : select mstrgensta0_.stateid as stateid1_6_, mstrgensta0_.countryid as countryid2_6_, mstrgensta0_.statelong as statelong3_6_, mstrgensta0_.statemed as statemed4_6_, mstrgensta0_.stateshort as stateshort5_6_ from oraapps.mstrgenstate mstrgensta0_ where mstrgensta0_.stateid=?
2017-07-21 15:46:13.477 TRACE 10496 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [NUMERIC] - [1]
2017-07-21 15:46:13.487 TRACE 10496 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([stateid1_6_] : [NUMERIC]) - [1]
2017-07-21 15:46:13.496 TRACE 10496 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([countryid2_6_] : [NUMERIC]) - [1]
2017-07-21 15:46:13.497 TRACE 10496 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([statelong3_6_] : [VARCHAR]) - [Texas]
2017-07-21 15:46:13.498 TRACE 10496 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([statemed4_6_] : [VARCHAR]) - [Texas]
2017-07-21 15:46:13.498 TRACE 10496 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([stateshort5_6_] : [VARCHAR]) - [TX ]
first test here 1

所以我看到的是检索数据适用于 整数,但当一个人使用字符时,似乎有某种 的问题。数据库是甲骨文 - 在所有地方都有错误吗 这?以下是我尝试使用字符串 (TX) 获取时得到的结果

我收到的消息

017-07-21 14:36:42.198  INFO 20212 --- [nio-8080-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2017-07-21 14:36:42.438 DEBUG 20212 --- [nio-8080-exec-1] org.hibernate.SQL                        : select mstrgensta0_.stateid as stateid1_6_, mstrgensta0_.countryid as countryid2_6_, mstrgensta0_.statelong as statelong3_6_, mstrgensta0_.statemed as statemed4_6_, mstrgensta0_.stateshort as stateshort5_6_ from oraapps.mstrgenstate mstrgensta0_ where mstrgensta0_.stateshort=?
2017-07-21 14:36:42.531 TRACE 20212 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [TX]
2017-07-21 14:36:42.557 DEBUG 20212 --- [nio-8080-exec-1] org.hibernate.SQL                        : select mstrgensta0_.stateid as stateid1_6_, mstrgensta0_.countryid as countryid2_6_, mstrgensta0_.statelong as statelong3_6_, mstrgensta0_.statemed as statemed4_6_, mstrgensta0_.stateshort as stateshort5_6_ from oraapps.mstrgenstate mstrgensta0_ where mstrgensta0_.statelong=?
2017-07-21 14:36:42.557 TRACE 20212 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [TX]

Fri Jul 21 14:36:42 CDT 2017 => DATA_INPUT_ERROR :   => CLASS :  CustomerController => EMSG-20008 - finding state id for state name : ->TX<- has failed  State passed in TX number of items found 0
ccinfw.messages.DataInputError: Fri Jul 21 14:36:42 CDT 2017 => DATA_INPUT_ERROR :   => CLASS :  CustomerController => EMSG-20008 - finding state id for state name : ->TX<- has failed  State passed in TX number of items found 0
    at ccinfw.controller.CustomerController.addCustomer(CustomerController.java:107)

控制器代码

@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = "application/json")
    public ResponseEntity<Lawncustomer> addCustomer(
            @RequestBody CustomerIOPOJO input) throws Exception {

        try {

            Lawncustomer cust = new Lawncustomer();

            cust.setTenantid(input.getTenantid());
            cust.setCustomerid(new BigDecimal(200));
            cust.setFirstname(input.getFirstname());
            cust.setLastname(input.getLastname());
            cust.setCellphoneno(input.getCellphoneno());
            cust.setEmail(input.getEmail());
            cust.setAddress(input.getAddress());
            cust.setCity(input.getCity());

            if (input.getState() == null
                    || input.getState().trim().length() <= 1) {
                throw new DataInputError
                        ( " => CLASS :  " +   this.getClass().getSimpleName() 
                        + " => EMSG-20000 - invalid state sent in for evaluation - make sure 2-char def is used. Passed in : "
                        + input.getState() + " EMail processed " + input.getEmail() );
            }


            List<Mstrgenstate> statefound;
            statefound = statedao.findByStateshort(input.getState());

            if (statefound.size() > 1 || statefound.size() < 0 ){                           
                throw new ApplicationError
                ( " => CLASS :  " +   this.getClass().getSimpleName() 
                + " => EMSG-20012 - invalid number of items found for STATE: ->" + input.getState() + "<- verify DB entries " 
                + " State passed in " + input.getState());
            }

            if (statefound.size() == 0 ) {
                statefound = statedao.findByStatelong(input.getState());

                if (statefound.size() != 1) {                           
                    throw new DataInputError
                    ( " => CLASS :  " +   this.getClass().getSimpleName() 
                    + " => EMSG-20008 - finding state id for state name : ->" + input.getState() + "<- has failed " 
                    + " State passed in " + input.getState() + " number of items found " + statefound.size() );  
                }
            }

            cust.setStateid(statefound.get(0).getStateid());
            cust.setZipcode(input.getZipcode());

            MapFunctionality mapping = new MapFunctionality(input.getAddress(),
                    input.getCity(), input.getState(), input.getZipcode());
            mapping.calcLatLongPositions();
            cust.setLoclatitude(mapping.getCalclat());
            cust.setLoclongitude(mapping.getCalclon());

            customer.save(cust);

            return new ResponseEntity<Lawncustomer>(cust, HttpStatus.OK);

        } catch (HibernateException e) {
            throw new RuntimeError
            ( " => CLASS :  " +   this.getClass().getSimpleName() 
            + " => EMSG-20010 - finding state id for state name : ->" + input.getState() + "<- has failed "
            + " EMail processed " + input.getEmail());
        }
    }

MstrgenstateDAO.java(使用 CrudRepository)

package ccinfw.general.dao;

import java.math.BigDecimal;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import ccinfw.general.entities.Mstrgenstate;


@Transactional
@Repository
public interface MstrgenstateDAO extends CrudRepository<Mstrgenstate, BigDecimal>{

    public List<Mstrgenstate> findByStateshort( String stateshort );

    public List<Mstrgenstate> findByStatelong( String statelong );

}

Mstrgenstate.java(由 Hibernate 生成)

// Generated Jul 16, 2017 9:14:14 AM by Hibernate Tools 4.0.0

import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Mstrgenstate generated by hbm2java
 */
@Entity
@Table(name = "MSTRGENSTATE", schema = "ORAAPPS")
public class Mstrgenstate implements java.io.Serializable {

    /**
     * serial item added as required
     */
    private static final long serialVersionUID = 3354389768807065484L;

    private BigDecimal stateid;
    private BigDecimal countryid;
    private String stateshort;
    private String statemed;
    private String statelong;

    public Mstrgenstate() {
    }

    public Mstrgenstate(BigDecimal stateid) {
        this.stateid = stateid;
    }

    public Mstrgenstate(BigDecimal stateid, BigDecimal countryid,
            String stateshort, String statemed, String statelong) {
        this.stateid = stateid;
        this.countryid = countryid;
        this.stateshort = stateshort;
        this.statemed = statemed;
        this.statelong = statelong;
    }

    @Id
    @Column(name = "STATEID", unique = true, nullable = false, precision = 22, scale = 0)
    public BigDecimal getStateid() {
        return this.stateid;
    }

    public void setStateid(BigDecimal stateid) {
        this.stateid = stateid;
    }

    @Column(name = "COUNTRYID", precision = 22, scale = 0)
    public BigDecimal getCountryid() {
        return this.countryid;
    }

    public void setCountryid(BigDecimal countryid) {
        this.countryid = countryid;
    }

    @Column(name = "STATESHORT", length = 3)
    public String getStateshort() {
        return this.stateshort;
    }

    public void setStateshort(String stateshort) {
        this.stateshort = stateshort;
    }

    @Column(name = "STATEMED", length = 15)
    public String getStatemed() {
        return this.statemed;
    }

    public void setStatemed(String statemed) {
        this.statemed = statemed;
    }

    @Column(name = "STATELONG", length = 30)
    public String getStatelong() {
        return this.statelong;
    }

    public void setStatelong(String statelong) {
        this.statelong = statelong;
    }

}

【问题讨论】:

  • 您究竟在哪里使用 jpa 来获取数据?
  • 我拨打电话时使用:“statefound = stateao.findByStateshort(input.getState());”在控制器代码中。
  • 您将数据保存在哪里?
  • 进入数据库端的表。它是一个 Oracle 表。但是,问题是我需要获取STATEID ~before~ 我将他的数据保存到Oracle 表中。有一个包含所有状态的表。问题是当我从包含所有状态的表中选择(或尝试获取)数据时,Hibernate 说那里什么都没有 -
  • select * from mstrgenstate where stateshort='TX';从 DB 命令行界面工作

标签: java oracle hibernate jpa spring-boot


【解决方案1】:

没关系,我只是将列更改为 VARCHAR 并且在数据库中一切正常。我猜休眠生成程序将我的 CHAR(3) 注册到 VARCHAR。愚蠢的错误 - 模组,您可以删除帖子。谢谢!

【讨论】:

    【解决方案2】:

    你可以为你的桌子做一个 DESC 吗?

    我怀疑您的 STATESHORT 列是 CHAR(3) 而不是 VARCHAR。您的列的内容不是“TX”而是“TX”,因此看不到该行。

    【讨论】:

    • 是的,这就是我发现的(参见我的上一篇文章)。该表被定义为 CHAR(3) 并且 Hibernate 正在寻找 VARCHAR。我在数据库中将其更改为 VARCHAR 并且一切正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2015-06-16
    • 2022-01-07
    • 2015-03-29
    • 2016-10-29
    • 2021-09-25
    相关资源
    最近更新 更多