Is there a way to prevent JPA from automatically saving objects?
I need to use a third-party API, and I have to pull / delete data from / to it. I have a class responsible for the API, and I have a method like this:
public User pullUser(int userId) {
Map<String,String> userData = getUserDataFromApi(userId);
return new UserJpa(userId, userData.get("name"));
}
Where the class is UserJpaas follows:
@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;
}
}
When I call a method (e.g. pullUser(1)), the returned user is automatically stored in the database. I do not want this to happen, is there a solution to avoid this? I know that the solution may be to create a new class that implements Userand return an instance of this class in the method pullUser(), is this good practice?
Thank.
source
share