Ids of one-to-many relations many side returning as nulls in spring boot / hibernate

时间:2019-01-28 19:59:37

标签: hibernate spring-boot

I have two domain classes, lets call them Account and AccountInfo. Those are defined like this:

@Entity
@Table(name = "account")
@EqualsAndHashCode(of = {"id", "created", "changed"})
@Getter
@Setter
@AllArgsConstructor
@RequiredArgsConstructor
public class Account {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "account", cascade = 
CascadeType.ALL)
@OrderBy("created DESC")
private List<AccountInfo> accountInfos;

When eg. fetching account by id and then calling:

account.getAccountInfos().stream().
map(AccountInfo::getSerialNo).collect(Collectors.toList()));

each serialNo comes out fine. But if I try to:

account.getAccountInfos().stream().
map(AccountInfo::getId).collect(Collectors.toList()));

each id comes out as null.

So why are ids null, are the accountInfos detached right after fetching them or what?

0 个答案:

没有答案