Chitika

Showing posts with label spring-mvc. Show all posts
Showing posts with label spring-mvc. Show all posts

Tuesday, January 6, 2015

Spring MVC Hibernate entity update

Following is the steps involved in updating an entity, in Spring MVC:

function(Entity ent) {
....
// where ent is the entity passed through spring mvc.

1. First get entity from repo
   Entity savedEntity = repo.getById(entId);

2. Merge savedEntity with ent passed from spring MVC
    savedEntity.setA(ent.getA());
    savedEntity.setB(ent.getB());
    // store last field going to delete, do not delete it yet (if any)
    Ent2 ent = savedEntity.getEnt2();
    // repopulate with new field
    savedEntity.setEnt2(new Ent2());

3. Save (update) entity
    repo.save(savedEntity);

4. Delete last field (if any), from step 2
    repoEnt2.delete(ent);

Not following above step will result in following exceptions:

org.hibernate.ObjectDeletedException: deleted instance passed to merge.
java.lang.IllegalStateException: An entity copy was already assigned to a different entity.

Tuesday, December 30, 2014

Failed to read candidate component class

I was having following error,

Failed to read candidate component class: file [/a/b/c/z$1.class]; nested exception is java.lang.SecurityException: Prohibited package name: java.lang: Prohibited package name: java.lang applicationContext-jpa.xml /im4/src/main/resources/META-INF/spring line 5 Spring Beans Problem

When clicked on it, it opens applicationContext-jpa.xml, with line <repositories base-package="a.b.c" />

Looking into the code of z.java, it is found out that it is util class having package a.b.c, which spring scans for beans.

Changing the package to a.b.z resolves the issue.