Foren

CRUD operations on entities

Csaba Meszaros, geändert vor 7 Jahren.

CRUD operations on entities

Junior Member Beiträge: 42 Beitrittsdatum: 10.10.13 Neueste Beiträge
Hi!
This post is somehow a duplicate, but I hope now it is clearer, so I can get an answer. In LR 6.2 I had no issues with manipulating an entity's fields, but I am confused now. I used to do the following in LR 6.2:

User user=UserLocalServiceUtil.getUser(userId);
System.out.println(user.getFirstName()); // -> prints Jack
user.setFirstName("Paul");
UserLocalServiceUtil.updateUser(user);
System.out.println(user.getFirstName()); // -> prints Paul


Now in LR 7.0 GA-3, the new way:

@Reference
public void setUserLocalService(UserLocalService userLocalService) {
this.userLocalService = userLocalService;
}

public UserLocalService getUserLocalService() {
return userLocalService;
}

private UserLocalService userLocalService;
....
User user=userLocalService.getUser(userId); // Or User user=ThemeDisplay.getUser();
System.out.println(user.getFirstName()); // -> prints Jack
user.setFirstName("Paul");
userLocalService.updateUser(user);
System.out.println(user.getFirstName()); // -> prints Jack


But if I do a reload of page, it becomes "Paul" as expected. The same thing happens also with portraitURL of user. I don't understan why, and I also need to know the correct new way of updating things without reload. I need a code, please!
I am using LR on MySQL backend. I am also running into Hibernate exceptions with StalledData.

Thanks!
Csaba Meszaros, geändert vor 7 Jahren.

RE: CRUD operations on entities

Junior Member Beiträge: 42 Beitrittsdatum: 10.10.13 Neueste Beiträge
Another issue:

11:38:47,650 ERROR [http-nio-8080-exec-10][DefaultTransactionExecutor:45] Application exception overridden by commit exception
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.liferay.portal.model.impl.UserImpl] with identifier [77772]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.liferay.portal.model.impl.UserImpl#77772]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:698)


for:

User user = userLocalService.fetchUser(lrUserId);
long contactId = user.getContactId();

user.setFirstName(ParamUtil.getString(actionRequest, "firstName"));
user.setLastName(ParamUtil.getString(actionRequest, "lastName"));
userLocalService.updateUser(user);

try {
Contact contact = contactLocalService.getContact(contactId);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
try {
Date date = formatter.parse(ParamUtil.getString(actionRequest,
"birthday"));
contact.setBirthday(date);
contactLocalService.updateContact(contact);
} catch (ParseException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("UpdateUserProfileMVCActionCommand.doProcessAction invalid date format", e);
}
}
} catch (PortalException e) {
LOG.error("Failed to get users Contact object.", e);
}


Any help?