使用 JpaRepository,getById() 函数返回 null

Posted

技术标签:

【中文标题】使用 JpaRepository,getById() 函数返回 null【英文标题】:Working with JpaRepository, the getById () function returns null 【发布时间】:2022-01-23 14:24:52 【问题描述】:

我有以下情况:一个用户实体可以有很多朋友,他们自己也是用户。

为了解决这种情况,我使用了这个答案Hibernate recursive many-to-many association with the same entity

有效!但不完全。在我将数据放入表 TBL_FRIENDS 后,数据按预期保存但是然后接口 JpaRepository 的 getById 函数为我输入到 TBL_FRIENDS 表中的用户返回 NULL。

这是我的代码:

package com.example.demo.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
 
@Entity
public class User 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "username", nullable = false)
    private String username;
    @Column(name = "gender", nullable = false)
    private String gender;
    @Column(name = "date", nullable = true)
    private Date date;
    @Column(name = "phone", nullable = false)
    private String phone;   
    @Column(name = "email", nullable = false)
    private String email;
    @Column(name = "description", nullable = true)
    private String description;
    @Column(name = "hashpassword", nullable = false)
    private String hashpassword;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name="tbl_friends",
     joinColumns=@JoinColumn(name="personId"),
     inverseJoinColumns=@JoinColumn(name="friendId")
    )
    private List<User> friends = new ArrayList<>();

    @ManyToMany(mappedBy = "friends")
    @JoinTable(name="tbl_friends",
     joinColumns=@JoinColumn(name="friendId"),
     inverseJoinColumns=@JoinColumn(name="personId")
    )
    private List<User> friendOf = new ArrayList<>();
           
    protected User() 
        
     

    public User(Long id, String username, String gender,Date date, String phone, String email, String description,String hashpassword ) 
        super();
        this.id = id;
        this.username = username;
        this.gender = gender;
        this.date = date;
        this.phone = phone;
        this.email = email;
        this.description = description;
        this.hashpassword = hashpassword;
    

    public Long getId() 
        return id;
    

    public void setId(Long id) 
        this.id = id;
    

    public String getUsername() 
        return username;
    

    public void setUsername(String username) 
        this.username = username;
    

    public String getGender() 
        return gender;
    

    public void setGender(String gender) 
        this.gender = gender;
    

    public Date getDate() 
        return date;
    

    public void setDate(Date date) 
        this.date = date;
    

    public String getPhone() 
        return phone;
    

    public void setPhone(String phone) 
        this.phone = phone;
    


    public String getEmail() 
        return email;
    


    public void setEmail(String email) 
        this.email = email;
    
    
    public String getDescription() 
        return description;
    


    public void setDescription(String description) 
        this.description = description;
    
    
    public String getHashpassword() 
        return hashpassword;
    

    public void setHashpassword(String hashpassword) 
        this.hashpassword = hashpassword;
    

    public List<User> getFriends() 
        return friends;
    

    public void addFriends(User friend) 
        this.friends.add(friend);
    
    
    public List<User> getFriendOf() 
        return friendOf;
    

    public void addFriendOf(User friend) 
        this.friendOf.add(friend);
    
    
    @Override
    public int hashCode() 
        return Objects.hash(id);
    

    @Override
    public boolean equals(Object obj) 
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        return id == other.id;
    

public void insertUserAndFriends(User sourceUser,User targetUser)  
    User su = userJpaRepository.getOne(sourceUser.getId());
    su.addFriends(targetUser);
    su.addFriendOf(targetUser);
    userJpaRepository.save(su);

@PostMapping("/jpa/users/userId/friends")
public ResponseEntity<Void>insertFriends(@RequestBody Map<String,User> jason)
  userJpaRepository.insertUserAndFriends(jason.get("sourceUser"),jason.get("targetUser"));  
    return ResponseEntity.ok().build();     

@GetMapping("/jpa/users/userId")
public User getUser(@PathVariable Long userId)
    return userJpaRepository.findById(userId).get();

我想听听任何建议。:

【问题讨论】:

【参考方案1】:

也许您的请求的 id 值为 null 或者您可以使用:

@GeneratedValue(strategy = GenerationType.AUTO)

【讨论】:

以上是关于使用 JpaRepository,getById() 函数返回 null的主要内容,如果未能解决你的问题,请参考以下文章

查询验证失败:JpaRepository

将 @EmbeddedId 与 JpaRepository 一起使用

将 OrderBy 与 JPARepository 一起使用

使用 jparepository 从 getmapping 返回布尔结果

JPARepository,名称方法返回包含“文本”的字段[关闭]

我应该如何将 JpaRepository.findOne() 与 SpringBoot 一起使用?