我有一個簡單的JPA存儲庫,看起來像這樣:春天JPA - 堅持嵌套對象
public interface UserRepository extends JpaRepository<User, String>
{
User findByName(String name);
}
和兩個類,有一個OneToOne映射,像這樣:
@Entity
public class User
{
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
protected String uuid;
@Column(nullable = false, unique = true)
private String name;
@Column(nullable = false)
private String email;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "user")
private PlayerCharacter playerCharacter;
//...
}
。現在
@Entity
public class PlayerCharacter
{
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
protected String uuid;
@OneToOne(optional = true)
private User user;
@Column(nullable = false)
private String characterName;
//...
}
,我知道我可以很容易地編輯和持續的用戶實例是這樣的:
User user = userRepository.findByName("Alice");
PlayerCharacter character = user.getPlayerCharacter();
character.setCharacterName("ConanTheBarbarian");
userRepository.save(user);
但我怎麼會從上下文中堅持一個PlayerCharacter
實例,其中我沒有指針回到User
情況下,網絡連接:
public void changePlayerCharacterName(PlayerCharacter character, String name){
character.setCharacterName(name);
// How to correctly persist the 'character' instance here?
}
可我只是叫userRepository.save(character);
?
是否需要自動調用另一個存儲庫(PlayerCharacterRepository
)並調用.save()?
爲什麼你會建議海報在不是JPA的一部分時使用一些專有註釋'@ LazyCollection'? –
通過這個你可以有雙向映射。 – Deepanjan