有没有办法避免让JPA自动持久化对象?
我需要使用第三方API,我必须从/向它提取/推送数据.我有一个负责接口API的类,我有一个像这样的方法:
public User pullUser(int userId) {
Map<String,String> userData = getUserDataFromApi(userId);
return new UserJpa(userId, userData.get("name"));
}
Run Code Online (Sandbox Code Playgroud)
凡UserJpa类的样子:
@Entity
@Table
public class UserJpa implements User
{
@Id
@Column(name = "id", nullable = false)
private int id;
@Column(name = "name", nullable = false, length = 20)
private String name;
public UserJpa() {
}
public UserJpa(int id, String name) {
this.id = id;
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用方法(例如pullUser(1))时,返回的用户将自动存储在数据库中.我不希望这种情况发生,是否有避免它的解决方案?我知道一个解决方案可能是创建一个新类实现User并在pullUser()方法中返回此类的实例,这是一个好习惯吗?
谢谢.
新的创建UserJpa实例不会保留在pullUser中.我还假设getUserDataFromApi中没有一些奇怪的实现实际上持久存在同样的id.
在您的情况下,实体管理器对UserJPA的新实例一无所知.通常,实体通过合并/持久调用或级联合并/持久操作持久化.在代码库中检查其他地方.
| 归档时间: |
|
| 查看次数: |
2794 次 |
| 最近记录: |