简体   繁体   English

CrudRepository findById 返回 null mysql

[英]CrudRepository findById returns null mysql

I have a Databaseintem, and I have as Id a string.我有一个 Databaseintem,我有一个字符串作为 Id。 I created an ItemRepository and declared findById, the entry is in the database, but I get null on findById.我创建了一个 ItemRepository 并声明了 findById,该条目在数据库中,但我在 findById 上得到了 null。

import org.springframework.data.repository.CrudRepository;导入 org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Transactional;导入 org.springframework.transaction.annotation.Transactional;

Repository:存储库:

@Transactional 
public interface ItemRepository extends CrudRepository<Item, String> {
    public Item findById(String id);
}

Item:物品:

@Entity
public class Item{
    private String id;
    private String idTrans;
    private String name;
    private Date created;
    private Date modified;

@Id
public String getId() {
    return id;
}
----

I have an ItemService that interacts with the repository and from ItemController I implement a rest api, get.我有一个与存储库交互的 ItemService,我从 ItemController 实现了一个 rest api,get。

ItemController:项目控制器:

   @Controller
   @RequestMapping(value = "/item")
   public class ItemController {

   @RequestMapping( value = "/{id}", method = RequestMethod.GET )
   @ResponseStatus( HttpStatus.OK )
   @ResponseBody
   public String getItem( @PathVariable( "id" ) String id){
      Preconditions.checkNotNull( id );
      logger.info("1. getAAA " + id); // prints
      Item a = itemService.getItem( id );
      logger.info("2. getAAA " + a);
      return "a : ";// + a.getName();
   }

entry in DB"数据库中的条目”

| token1495528421393 | 2017-05-23 11:33:41 | Itemnull |               0 |        |          0 |          0 | 2017-05-23 11:33:41 |Item1

Request GET:请求获取:

curl -H "Content-Type: application/json" -X GET http://localhost:8090/item/token1495528421393
  • item is null on findById findById 上的项目为空

I have just run exactly in the same issue some days ago, then i have found out that the reason why this was happening was that the entity which i was retrieving had a foreign reference to another entity which thereafter had as well another foreign reference to a third entity and the latter of these [references] was configured as not null , but the problem was that when checking in the database the looked item had effectively a null value in the foreign key;几天前我刚刚遇到了完全相同的问题,然后我发现发生这种情况的原因是我正在检索的实体具有对另一个实体的外部引用,此后又对另一个实体进行了外部引用第三个实体和这些 [references] 中的后者被配置为not null ,但问题是在数据库中检查时,所查找的项目在外键中实际上具有空值; so when the persistence framework was translating the service into a query, it was making an inner join rather than an outer join which removed the entity from the results since the join between the second and third entity was failing due to the null values.因此,当持久性框架将服务转换为查询时,它会进行内部连接而不是外部连接,从而从结果中删除实体,因为第二个和第三个实体之间的连接由于空值而失败。 Removing the not null clause in the second entity helped in fixing the issue.删除第二个实体中的not null子句有助于解决问题。

Please check if your entities are not mapped in similar way.请检查您的实体是否未以类似方式映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM