My favorites | Sign in
Project Logo
                
Search
for
Updated Feb 25, 2009 by kuisong.tong
Trackable  
Track (Creator, CreatedTime) and/or (Modifier, ModifiedTime) automatically

Introduction

We always need track user and time for audit. To eliminate this boring code, we use JPA's entity listener to set this info automatically.

Setup

First, we need configuration let JPA know this entity listener. We add this to orm.xml:

	<persistence-unit-metadata>
		<persistence-unit-defaults>
			<entity-listeners>
				<entity-listener
					class="cn.muthos.polyforms.dao.jpa.EntityAuditListener" />
			</entity-listeners>
		</persistence-unit-defaults>
	</persistence-unit-metadata>

Second, we need a bean which implemented UserHolder to let entity listener to know how to get current user:

@Component
public class MockUserHolder implements UserHolder<User> {
	public User getCurrentUser() {
		return new User("admin");
	}
}

Details

Now, we just add @Trackable to your entity.

@Traceable
public class User {
	@ManyToOne
	private User creator;
	private Date createdTime;
        ...
}

Sign in to add a comment
Hosted by Google Code