Fail to delete using eclipselink

Asked

Viewed 77 times

1

Good afternoon, everyone.

I am maintaining a system that another developer started, before he was using Hibernate and then migrated to eclipselink.

When using Hibernate was working normal, and when migrated started to fail delete.

The Controller is like this.

@RequestMapping(value = {deletePath}, method = RequestMethod.POST)
public String delete(@PathVariable("id") String id, Division division) {
       divisionService.delete(division);
       return "redirect:"+defaultPath;
}

stackoverflow

Error: org.springframework.transaction.Transactionsystemexception: Could not commit JPA transaction; nested Exception is javax.persistence.Rollbackexception: Exception [Eclipselink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.Databaseexception Internal Exception: org.postgresql.util.Psqlexception: ERROR: null value in column "name" violates not-null Constraint Detail: Failing Row contains (d1ba96c6-baac-4664-86f1-af22386324bd, AB8D5E1F-66C1-40F9-B98E-DEC21FFA7626, null, d1ba96c6-baac-4664-86f1-af22386324bc, 2015-03-27 11:42:27.427, d1ba96c6-baac-4664-86f1-af22386324bc, 2015-03-27 12:27:29.097, f). Error Code: 0 Call: UPDATE _login.Division SET ACTIVE = ? , MODIFIEDBY = ? , MODIFIEDON = ? , NAME = ? , Parent = ? WHERE (id = ?) bind => [false, d1ba96c6-baac-4664-86f1-af22386324bc, 2015-03-27 12:27:29.097, null, AB8D5E1F-66C1-40F9-B98E-DEC21FFA7626] Query: Updateobjectquery(com.xphub.core.model.Division@7cc1d214) URL: http://localhost:8080/xphub/admin/user/Division/AB8D5E1F-66C1-40F9-B98E-DEC21FFA7626/delete

1 answer

0

I don’t have all the code to state 100% what the problem is, but it seems to me that your class DivisionService is trying to update the entity Division received by the controlling being.

Probably this system is one of those that doesn’t really erase the data, just alters a flag to disable or disable the record that will no longer be displayed.

When trying to update the object, probably calling the method merge, Eclipselink validated the entity again and saw that the data was not filled in. This is probably because the controller method receives only the ID and not the other entity fields.

To solve this, it seems to me that the most direct output would first recover the original entity so that it contains the required fields, thus:

division = divisionService.find(id);
divisionService.delete(division);

In JPA this type of thing is common. You may not know that an object is only a JPA entity if it is in the JPA context. The parameter Division of your controller method is a common object. It will only be a managed entity if you recover the data by EntityManager or if you use the merge. But the latter will replace the values of the record in the database with the values of the new object, which in this case are null.

  • Thanks Luiz for the help, the way you did it worked right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.