这个问题What is the right way to use entitymanager提供了指向EntityManagerHelper.java的链接,该链接已添加到我的代码库中。当我使用此帮助程序类对数据库进行后续调用时,它将先前的结果返回到同一查询。
我最常看到的场景是检索用户类的lastentry属性。在浏览器上,我发出AJAX请求,下面的servlet部分获取用户对象并调用方法以返回我的lastentry。
我已经读过有关.clear()的信息,但是将其添加到EntityManagerHelper时出现服务器错误。我想避免在每次要调用数据库时都创建EntityManager。
我该如何解决这个问题?
部分来自servlet
User user = User.getUser();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(user.getLastentry());
用户类别
package entities;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Persistence;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.shiro.SecurityUtils;
import responseablees.EntityManagerHelper;
/**
*
* @author Christopher Loughnane <[email protected]>
*/
@Entity
@Table(name = "user")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")
, @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id")
, @NamedQuery(name = "User.findByUsername", query = "SELECT u FROM User u WHERE u.username = :username")
, @NamedQuery(name = "User.getUsernameByUserEmail", query = "SELECT u.username FROM User u WHERE u.useremail = :useremail")
, @NamedQuery(name = "User.findByUserEmail", query = "SELECT u FROM User u WHERE u.useremail = :useremail")
, @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password")})
public class User implements Serializable {
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 2048)
@Column(name = "lastentry")
private String lastentry;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 128)
@Column(name = "useremail")
private String useremail;
@Basic(optional = false)
@Column(name="created", insertable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date created;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "username")
private String username;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "password")
private String password;
public User() {
}
public User(Integer id) {
this.id = id;
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof User)) {
return false;
}
User other = (User) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "DAOs.User[ id=" + id + " ]";
}
public String getUseremail() {
return useremail;
}
public void setUseremail(String useremail) {
this.useremail = useremail;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getLastentry() {
return lastentry;
}
public void setLastentry(String lastentry) {
this.lastentry = lastentry;
}
public static User getUser(){
String currentUser = (String) SecurityUtils.getSubject().getPrincipal();
User user = (User) em.createNamedQuery("User.findByUserEmail")
.setParameter("useremail", currentUser)
.getSingleResult();
return user;
}
}
要求的代码
详细方法
String currentUser = (String) SecurityUtils.getSubject().getPrincipal();
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("com.mycompany_responseableES_war_1.0-SNAPSHOTPU");
EntityManager em = emfactory.createEntityManager();
User user = (User) em.createNamedQuery("User.findByUserEmail")
.setParameter("useremail", currentUser)
.getSingleResult();
em.getTransaction().begin();
user.setLastentry(JSON);
em.getTransaction().commit();
EntityManagerHelper方法
User user = User.getUser();
EntityManager em = EntityManagerHelper.getEntityManager();
em.getTransaction().begin();
user.setLastentry(JSON);
em.getTransaction().commit();
最佳答案
首先,我会小心地将所有数据获取/修改代码放在
em.getTransaction().begin()
和
em.getTransaction().commit()
这样可以确保对您的实体进行管理,并自动保留您的更改。在您的代码中,看起来
User
实例来自事务外部。我在JPA和手动事务方面工作不多,但是如果用户已分离(即不受EntityManager
管理),因此更改不会自动保存到数据库中,我不会感到惊讶。如果是这种情况,我想您可以通过以下方式解决// merge(...) takes an entity that is not managed, apply changes to
// the persistence context and returns a new *managed* entity.
// The original entity REMAINS *detached* or *non persistent*
User managedUser = em.merge(user);
// I assume that you have something like this to set your user in the
// current session or whatever
User.setUser(managedUser);
在提交之前。
首先,JPA词汇可能会有点难,请阅读此书以获取有关the JPA entity lifecycle的更多详细信息(特别是底部的图)。
关于java - 为什么我的EntityManagerHelper类返回以前的查询结果,并且该如何解决?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50489471/