问题描述
当从JPA会话外部更改数据库中的值时,实体没有刷新的问题。例如,我有一个用户实体:
I have a problem with entities not being refreshed when values in the database are changed from outside the JPA session. For instance, I have a user entity:
@Entity
@Cacheable(false)
public class UserBean implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToMany(mappedBy = "receiver")
@JoinTable(name = "NOTIFICATIONS_RECEIVED")
private List<NotificationBean> notificationsReceived;
...
}
通知实体:
@Entity
@Cacheable(false)
public class NotificationBean implements Serializable{
@Id
@GeneratedValue
private Long id;
@ManyToOne
private UserBean receiver;
...
}
我在a JSF应用程序,并具有一个SessionScoped bean,该bean在登录后加载用户并进行存储:
I use this inside a JSF application and have a SessionScoped bean, which loads the user after login and stores it:
@Named("sessionManager")
@SessionScoped
public class SessionManagerBean implements Serializable {
@PersistenceUnit(unitName = "PU")
private EntityManagerFactory emf;
private UserBean user;
public UserBean getUser() throws Exception {
if (user == null) {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
String username = request.getRemoteUser();
if (username != null) {
EntityManager em = null;
try {
utx.begin();
em = emf.createEntityManager();
Query query = em.createQuery("SELECT u from UserBean u WHERE u.username = ?1");
query.setParameter(1, username);
user = (UserBean) query.getSingleResult();
}
catch (Exception e) {
try {
utx.rollback();
} catch (Exception e) {
} finally {
utx.commit();
em.close();
}
}
return user;
}
}
}
public void refreshUser() {
EnitytManager em = emf.createEntityManager();
// similar code as above to retrieve the user from the database
em.refresh(user);
}
}
显示通知呼叫的页面 refreshUser()
加载时:
The page which displays the notifications calls refreshUser()
when it loads:
<f:metadata>
<f:event type="preRenderView" listener="#{sessionManager.refreshUser()}" />
</f:metadata>
虽然我的数据没有刷新,但刷新时页面上显示的通知没有更新
The user data is not refreshed though and notifications which are displayed on the page are not updated when I refresh the page.
但是,如果我将 refreshUser()
更改为:
However, if I change refreshUser()
to:
public void refreshUser() {
EntityManager em = emf.createEntityManager();
List<NotificationBean> notifications = em.createNativeQuery("SELECT * FROM NOTIFICATIONBEAN WHERE RECEIVER_ID = " +
user.getId() + ";").getResultList();
user.setMatchChallengesReceived(notifications);
}
通知已更新。
我需要从数据库中刷新的通知比我有更多的变量,并且每个通知都需要执行很多代码。我认为 em.refresh(user)
应该为我重新加载数据库中所有已更改的变量。我认为这是一个缓存问题,因此我向 UserBean
和 @Cacheable(false) > NotificationBean ,但无效。
I have more variable than notifications that I need to refresh from the database and it would be a lot of code to do the same for each one. I thought em.refresh(user)
should reload all variables that have changed from the database for me. I thought it is a caching issue, so I added @Cacheable(false)
to UserBean
and NotificationBean
, but it has no effect.
我在做什么错了?
推荐答案
如果问题出在通知上,则由于未将刷新用户设置为级联刷新,因此很容易。在notificationsReceived映射上设置CascadeType.REFRESH。
If the problem is with notifications, then itis because refreshing user is not set to cascade the refresh. Set the CascadeType.REFRESH on the notificationsReceived mapping.
这篇关于JPA EclipseLink实体未刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!