You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by sommeralex <al...@gmail.com> on 2012/10/25 02:41:01 UTC

Hibernate & springframework.security question

Hi!

I know that this is more a hibernate than a tapestry question, but i am not
sure if my question is really more hibernate specific, or a standard
problem. 

If i am logged in as User user with session x, but some values of my user
get changed from the outside of a session y my User user in my session x has
still the "old" values. If i am now persisting my User user from session x,
the new values from session y are overwritten and are therefore lost. I am
merging the object via DAO.merge - but it seems that hibernate does not know
which values are old, and new. 

So the question is, what do i have to do, to ensure that my user object
retrieved from my DB is always uptodate?

merge function:

	public T merge(T detachedInstance) {
		logger.debug("Merging " + detachedInstance + ".");
		try {
			Date now = new Date();
			detachedInstance.setUpdatedAt(now);
			T result = getHibernateTemplate().merge(detachedInstance);
			return result;
		} catch (RuntimeException re) {
			logger.error("Merging " + detachedInstance + " failed.", re);
			throw re;
		}
	}

This is the way i am getting my user object (but this object is never
updated if i am changing some values of the user directly in the DB - only
if i am login out and in again:

UserService.java

	public User getCurrentUser() {
		return authenticationHelper.getPrincipal();
	}

AuthenticationHelper.java

public User getPrincipal() {
    Authentication authentication =
SecurityContextHolder.getContext().getAuthentication();
    User user = null;
    if (authentication != null) { // is user logged in?
    AirwritingUserDetails userDetail = (AirwritingUserDetails)
authentication.getPrincipal();
      user = userDetail.getUser();
    }
    return user;
}

AirwritingUserDetails.java

public abstract class AirwritingUserDetails extends
		org.springframework.security.core.userdetails.User {

	private User user;

	public AirwritingUserDetails(User user, Collection<? extends
GrantedAuthority> authorities) {
		super(user.getUsername(), user.getPassword(), user.getStatus() ==
Status.ACTIVE, true, true, true,
				authorities);
		this.user = user;
	}

	public User getUser() {
		return user;
	}

}



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Hibernate-springframework-security-question-tp5717207.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Hibernate & springframework.security question

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Wed, 24 Oct 2012 22:41:01 -0200, sommeralex  
<al...@gmail.com> wrote:

> Hi!

Hi!

> I know that this is more a hibernate than a tapestry question, but i am  
> not sure if my question is really more hibernate specific, or a standard
> problem.

This is a pure Hibernate question as far as I can see.

> If i am logged in as User user with session x, but some values of my user
> get changed from the outside of a session y my User user in my session x  
> has still the "old" values. If i am now persisting my User user from  
> session x, the new values from session y are overwritten and are  
> therefore lost. I am merging the object via DAO.merge - but it seems  
> that hibernate does not know which values are old, and new.

Check the Hibernate documentation about optimistic locking:  
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/transactions.html.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org