Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Hibernate JPA更改不会立即反映(相同的调用)_Hibernate_Jpa_Spring Data Jpa - Fatal编程技术网

Spring Hibernate JPA更改不会立即反映(相同的调用)

Spring Hibernate JPA更改不会立即反映(相同的调用),hibernate,jpa,spring-data-jpa,Hibernate,Jpa,Spring Data Jpa,使用者 @实体 @表(name=“账户\用户”) @AllArgsConstructor @诺尔格构装师 @吸气剂 @塞特 公共类用户{ @身份证 @生成值 私有int-id; 私有字符串名称; 私人约会日期; @OneToMany(fetch=FetchType.EAGER) @JoinColumn(name=“user\u id”) 列出账目; 公共作废账户(账户){ 此.accounts.add(account); } 公共用户(字符串s、日期、列表帐户){ this.name=s; th

使用者

@实体
@表(name=“账户\用户”)
@AllArgsConstructor
@诺尔格构装师
@吸气剂
@塞特
公共类用户{
@身份证
@生成值
私有int-id;
私有字符串名称;
私人约会日期;
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name=“user\u id”)
列出账目;
公共作废账户(账户){
此.accounts.add(account);
}
公共用户(字符串s、日期、列表帐户){
this.name=s;
this.dob=日期;
这个账户=账户;
}
}
召唤

userRepository.saveAndFlush(新用户(“arpi”),新日期(1992年10月9日),空);
saveAndFlush(新帐户(userRepository.findAll().get(0),199));
//保存(新用户(“arpi”),新日期(1992年10月9日),空);
//save(新帐户(userRepository.findAll().get(0),199));
//accountRepository.flush();
//userRepository.flush();
List s=userRepository.findAll().get(0.getAccounts();
//s为空

也许您的实体用户在字段accounts中将@OneToMany注释设置为fetch lazy,如果是这样,您必须将其设置为eager

userRepository.saveAndFlush(new User("arpi", new Date(1992, 10, 9), null));
accountRepository.saveAndFlush(new Account(userRepository.findAll().get(0), 199));
//        userRepository.save(new User("arpi", new Date(1992, 10, 9), null));
//        accountRepository.save(new Account(userRepository.findAll().get(0), 199));
//        accountRepository.flush();
//        userRepository.flush();
    List<Account> s = userRepository.findAll().get(0).getAccounts();
// s is null
@OneToMany(fetch=FetchType.EAGER)
私人名单账户;

实体映射中存在错误

因此,基本上您需要的是
帐户
用户
之间的双向映射,正如您添加的
@manytone
@OneToMany
一样

如果是这样,将有一个所有者和一个参考方

@JoinColumn
用于所有者端,而
@mappedBy
用于参考端

两个实体上都有
@JoinColumn

您的映射应该是这样的

@OneToMany(fetch = FetchType.EAGER)
private List<Account> accounts;
创建了一个没有帐户链接的用户

userRepository.saveAndFlush(new User("arpi", new Date(1992, 10, 9), null));
创建了一个链接到用户的帐户

accountRepository.saveAndFlush(new Account(userRepository.findAll().get(0), 199));
List s=userRepository.findAll().get(0.getAccounts();
现在您希望用户的帐户为空,因为用户和帐户之间没有引用

userRepository.saveAndFlush(new User("arpi", new Date(1992, 10, 9), null));
尝试将代码更改为此,然后重试。请更改用户实体中的构造函数,如上所述

List<Account> s = userRepository.findAll().get(0).getAccounts();
User-User=新用户(“arpi”,新日期(1992年10月9日))
user.addAccount(新帐户(199)):
userRepository.saveAndFlush();
List s=userRepository.findAll().get(0.getAccounts();

我必须自动连接entitymanager并分离对象,因为它属于同一个会话,所以不会刷新。它触发了查询,但这仍然很奇怪

您的方法或类是否标记为@Transactional?不,它没有标记为@Transactional请参考这一点不相似,但我尝试了所有建议,我可以通过id获取,但不能在一个多人关系上获取。可能
userRepository.findAll().get(0)
返回另一个没有帐户的用户记录。不,它的fetch-eager在上面添加了其他详细信息。当我通过它工作的用户实体保存时,我的疑问是当我将它们单独保存到自己的存储库中并尝试在同一个rest调用中提取时,另外,当我再次调用同一个api时,两边都有join列的较旧实现也能工作,这更多地是与我感觉到的实体的缓存有关,但是我可以看到sql打印出来再次获取它,顺便说一句,您提供的更改可以工作,但我只是尝试了一些其他的东西
userRepository.saveAndFlush(new User("arpi", new Date(1992, 10, 9), null));
accountRepository.saveAndFlush(new Account(userRepository.findAll().get(0), 199));
List<Account> s = userRepository.findAll().get(0).getAccounts();
User user = new User("arpi", new Date(1992, 10, 9))
user.addAccount(new Account(199)):
userRepository.saveAndFlush();
List<Account> s = userRepository.findAll().get(0).getAccounts();