Spring数据CrudRepository(hibernate)findone返回null

时间:2017-08-27 08:34:24

标签: java hibernate spring-data

由于某些原因,我无法通过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()

1 个答案:

答案 0 :(得分:0)

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

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

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