【问题标题】:Spring data CrudRepository (hibernate) findone return nullSpring data CrudRepository (hibernate) findone return null
【发布时间】:2018-02-04 18:52:00
【问题描述】:

由于某种原因,我无法通过 Id 找到现有实体

实体有以下视图:

@Entity
@Table(name = "persons")
@NamedEntityGraph(name = "includeRemunerationAndCredential",attributeNodes = {
    @NamedAttributeNode("remunerations"),
    @NamedAttributeNode("credentials")
})
@JsonSerialize
public class Person implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "first_name", nullable = false, length = 50)
@NotNull
private String firstName;

@Column(name= "second_name",  nullable = false, length = 50)
@NotNull
private String secondName;

@Column(name = "father_name", length = 50)
private String fatherName;

@Column(name = "phone_number", length = 20)
private String phoneNumber;

@Column(name = "date_of_birth")
private LocalDateTime dateOfBirth;

@Column(name = "role", nullable = false, length = 50)
@Enumerated(EnumType.STRING)
private Role role;

@Column(name = "date_of_creation", nullable = false)
@NotNull
private LocalDateTime dateOfCreation;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "carwash")
private CarWash carWash;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "person")
private List<Remuneration> remunerations;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "person")
private Set<Credential> credentials;

@Column(name = "enable", nullable = false)
private Boolean enable;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "owner", nullable = false)
private Owner owner;

// getters and setters

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Person person = (Person) o;

    return id != null ? id.equals(person.id) : person.id == null;

}

@Override
public int hashCode() {
    return id != null ? id.hashCode() : 0;
}

@Override
public String toString() {
    return "Person{" +
            "id=" + id +
            ", firstName='" + firstName + '\'' +
            ", secondName='" + secondName + '\'' +
            ", fatherName='" + fatherName + '\'' +
            ", phoneNumber='" + phoneNumber + '\'' +
            ", dateOfBirth=" + dateOfBirth +
            ", role=" + role +
            ", dateOfCreation=" + dateOfCreation +
            ", carWash=" + (carWash == null ? carWash : carWash.getId()) +
            ", remunerations=" + (remunerations == null ? remunerations : remunerations.size()) +
            ", enable=" + enable +
            ", owner=" + (owner == null ? "N/A" : owner.getName()) +
            '}';
}

我的DAO如下视图:

public interface PersonDAO extends CrudRepository<Person, Long> {}

我的服务是由这个班级提供的

@Service("userDetailsService")
@Transactional
public class MyUserDetailService implements UserDetailsService {

@Autowired
PersonDAO personDAO;

@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
    //final Iterable<Person> all = personDAO.findAll();
    final Person one = personDAO.findOne(15L);

    return null;
}

因此,如果在服务中 personDAO.findAll() 填充被注释,则 personDAO.findOne(15L) 返回 null 但如果 findAll() 将未提交,则将找到 findOne(15L)。热修复这个 findOne 在没有 findAll() 的情况下工作?

【问题讨论】:

  • 你为什么返回null?
  • 显然,持久化的 id 不是 15。证明这一点 - 显示您的数据库。
  • 我返回 null 只是为了减小方法大小。无论如何它对弹簧数据没有任何反应
  • 数据如何进入数据库?它已经存在了吗?还是被其他代码插入?如果稍后向我们展示该代码。
  • 听起来 findAll 可能会触发刷新。

标签: java hibernate spring-data


【解决方案1】:

几天前我刚刚在同一个问题上运行过,然后我发现发生这种情况的原因是我正在检索的实体有一个对另一个实体的外部引用,此后又有另一个外部实体对第三个实体的引用,并且这些 [references] 中的后者被配置为不可为空,但问题是在检查数据库时,所查找的项目在外键中实际上具有空值;因此,当持久性框架将服务转换为查询时,它进行的是内部联接而不是外部联接,这会从结果中删除实体,因为第二个和第三个实体之间的联接由于空值而失败。

删除第二个实体字段中的上述不可为空子句解决了[在检索第一个实体的服务中]的问题

请检查您的实体是否未以类似方式映射:例如,我可以看到实体 Person 具有对不可为空的实体 Owner 的外部引用;也许所有者为空,这就是问题所在?如果它不为空,请检查所有其他外部引用,然后检查所有对其他其他第三方实体的内部引用链,以确保您没有在其中一个相对列的字段中强制执行 not null而是实际上可以为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-26
    • 2017-04-25
    • 1970-01-01
    • 2017-10-01
    • 2015-06-19
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    相关资源
    最近更新 更多