0

I have a quite interesting issue. I set some values on the server, send the bean to client and then it comes back to the server without that values.

Here is what I do:
1. Client requests some beans from DB through EntityRequest.getEntity(params)
2. I fetch a bean from database through Hibernate
3. I set some transient property (I need them on client side and don't want them to store in DB)
4. Send the bean through EntityRequest's method to client.
5. Client changes some other values and calls persist.
6. Server receives back his bean and
- is has properly set properties from the client
- the property set from the server (step no. 3) are ERASED / IGNORED.

It looks like RF mechanism would send me freshly DB loaded version with only changes from client. I did some research and it looks like there must have been some bean version issue. The bean has version set (see below) and it is used by Hibernate and I guess by RF also.

What should I do to get my value to client and back? I tried some "entity.version++;" on step 3, but it doesn't work.

@Entity
public class Person {

    @Id
    @GeneratedValue
    private Long id;

    @Version
    private Integer version;

    @Field
    private String name;

    @Transient
    private Long participationId;

    ...

    public Long getId() {
        return id;
    }

    public Integer getVersion() {
        return version;
    }

    ...
}

public static Person findPerson(Long id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    try {

        Object p = session.get(Person.class, id);
        session.getTransaction().commit();

        return p != null ? (Person) p : null;

    } catch (RuntimeException e){
        logger.error("Person.findPerson", e);
        session.getTransaction().rollback();
        throw e;
    }
}
7
  • Could you share your Locator code? (or the public static Person findPerson(Long id) method on the Person class, depending on what you're using) Nov 8, 2012 at 15:08
  • Thomas, I have added the findPerson code above to the question. I've done some research myself as well and have found that actually findPerson(id) is called on reconstructing domain object. Why it doesn't use internal cache? Can I code findPerson differently to use domain objects cache? Thank you.
    – hostnik
    Nov 9, 2012 at 1:06
  • You SHOULD (as in: MUST, unless you really know what you're doing) use the open session in view pattern with JPA/Hibernate/whatever, so share the same session (and its internal cache) throughout a request (for other reasons than performance). In between requests though, maybe use another, shared, cache, but better wait until you really need it (premature optimization is the root of all evil) Nov 9, 2012 at 9:49
  • BTW, you don't need a transaction for a readonly operation, and you don't need the nullcheck either (casting null to a Person will still work) Nov 9, 2012 at 9:52
  • Thomas, thank you for your answers! It helped me to understand more the RF mechanism and get to the solution. Ugly hack: another transient value participationStoreId. Whenever I want to persits, I copy it in client from participationId to participationStoreId and it gets transferred to server. Would you mind making an answer that I can give you credit for it and I close the question?
    – hostnik
    Nov 12, 2012 at 13:30

1 Answer 1

0

With the help of Thomas Broyer I understood more the RequestFactory mechanism and this has led me to the solution. RequestFactory on returning the object from client side does not take domaing object out of a cache but rather fetches fresh one and does diff to it. The fetch is done either using Locator or domain objet's find(id) function. In my case was find(id).

The solution is an ugly hack, I know, but it works. This is on my entity

public class Person {
...
@Transient
private Long participationId;

@Transient
private Long participationStoreId;
...
}

Before I sent it to RF I load participationId. On the client side, to keep it, before sending back I do:

person.setParticipationStoreId(person.getParticipationId());

Before you throw eggs at me, I repeat: I know it is an ugly hack. But in some cases I need some value on entity to the client and back and I don't want it in DB. This was the only way that works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.