gpt4 book ai didi

java - Spring 数据 CrudRepository (hibernate) findone 返回 null

转载 作者:行者123 更新时间:2023-12-05 07:40:42 25 4
gpt4 key购买 nike

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

实体有以下 View :

@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 有以下 View :

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() 的情况下工作的问题?

最佳答案

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

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

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

关于java - Spring 数据 CrudRepository (hibernate) findone 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45903120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com