Java 在SpringBoot下,Hibernate说表中的一行不存在,但它确实存在

Java 在SpringBoot下,Hibernate说表中的一行不存在,但它确实存在,java,oracle,hibernate,jpa,spring-boot,Java,Oracle,Hibernate,Jpa,Spring Boot,我正在使用JPA->Hibernate->JDBC->Oracle运行一个简单的查询 查询基本上是: select * from mstrgenstate where stateshort='TX'; 当我在Oracle级别运行它时,我得到: STATEID COUNTRYID STA STATEMED STATELONG ---------- ---------- --- --------------- ------------------------------

我正在使用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)正在使用crudepository

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 );

}
为了从数据库中获取数据,需要做一些特殊的事情吗

短暂性脑缺血发作

更新 我添加到DAO文件中:

public List<Mstrgenstate> findByStateid ( BigDecimal stateid );
List<Mstrgenstate> testme = statedao.findByStateid(new BigDecimal(1));
System.out.println("first test here " + testme.size());
所以我看到的是,检索数据对于 整数,但当使用字符时,似乎有某种 当然有问题。数据库是Oracle—在所有数据库中是否都存在bug 这下面是我尝试使用字符串(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)

没关系,我只是将列更改为VARCHAR,数据库中的一切都很好。我猜hibernate生成程序将我的CHAR(3)注册到了VARCHAR。愚蠢的错误-mods,你可以删除后。谢谢

你能描述一下你的桌子吗


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

在哪里使用jpa来获取数据?我使用控制器代码中的“statefund=statedao.findByStateshort(input.getState());”进行调用。在数据库端的表中保存数据。这是一个Oracle表。但是,问题是在将数据保存到Oracle表之前,我需要获取STATEID。一张桌子上有所有的州。问题是,当我从包含所有状态的表中选择(或尝试获取)数据时,Hibernate说那里什么都没有-select*from mstrgenstate where stateshort='TX';从DB命令行界面工作,这是我发现的(见我的上一篇文章)。该表被定义为CHAR(3),Hibernate正在查找VARCHAR。我在数据库中将它改为VARCHAR,一切都正常了。
@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());
        }
    }
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 );

}
// 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;
    }

}