在Spring Boot下,Hibernate说表中的行不存在

时间:2017-07-21 19:56:14

标签: java oracle hibernate jpa spring-boot

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

查询基本上是:

# automated loop testing values in range
upper = 0x100
for n in range(upper):
    if check(n, second, target):
        print(hex(n))

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

select * from mstrgenstate where stateshort='TX';

当我使用JPA和Hibernate在Java中运行它时,我得知表中没有记录。我得到的消息(跟踪设置打开)如下所示。实体代码( Mstrgenstate.java )是使用 Hibernate 生成的。数据访问代码( MstrgenstateDAO.java )正在使用 CrudRepository

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

TIA

更新 我添加到DAO文件中:

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

和控制器:

public List<Mstrgenstate> findByStateid ( BigDecimal stateid );

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

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

所以我看到的是检索数据的工作正常   整数但是当使用字符时,似乎有某种形式   问题。数据库是Oracle - 在所有的地方都存在一个错误   这个?下面是我尝试使用字符串(TX)

获取时获得的内容

消息我正在接受

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

控制器代码

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)

MstrgenstateDAO.java(使用CrudRepository)

@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());
        }
    }

Mstrgenstate.java(由Hibernate生成)

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

}

2 个答案:

答案 0 :(得分:0)

没关系,我刚刚将列更改为VARCHAR,并且数据库中的一切工作正常。我想hibernate生成程序将我的CHAR(3)注册到VARCHAR。愚蠢的错误 - mods,你可以删除帖子。谢谢!

答案 1 :(得分:0)

你能为你的桌子做一个DESC吗?

我怀疑你的STATESHORT列是CHAR(3)而不是VARCHAR。您的专栏内容不是“TX&#39;但是&#39; TX&#39;所以没有看到这一行。