跳至主要内容

博文

目前显示的是 十月, 2013的博文

JPA Data Auditing

Auditing Sometime we need to keep track with the change of the Entity, eg. When it is created Who created it Last modified date Last modified by somebody Implement via JPA JPA specification provides a complete lifecycle of an Entity, which is easy to archive those purposes. An example(dummy codes). @Entity public class Conference{ private User createdBy; private Date createdDate; private user lastModifiedBy; private Date lastModifiedDate; @PrePersist public void prePersist(){ setCreatedBy(currentUser); setCreatedDate(new Date()); } @PreUpdate public void preUpdate(){ setLastModifiedBy(currentUser); setLastModifiedDate(new Date()); } } In this example, the method   prePersist   annotated with   @PrePresist indicates it will be executed before   em.persist (em is stand for jpa EntityManager) is executed. And   @PreUpdate   will be executed before   em.merge   is executed. JPA also provides a   EntityListener   feature. Using EntityListen

Data crossstore between Mongo and JPA

Data crossstore between Mongo and JPA Spring Data Mongo provides another attractive feature, when you mix to use Mongo and JPA in your projects, you can unite the models of Mongo and JPA. Model The   Conference   and   Signup   are still used as example models, Conference   is JPA entity class and   Signup   is a Mongo Document. @Entity public class Conference { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private Long id; @RelatedDocument private Signup signup; } The   Signup   Document is defined as the following. @Document public class Signup { @Id private String id; } @RelatedDocument   annotation indicates the   signup   property of Conference is a reference of   Signup   Mongo Document. This annotation will be processed by a AspectJ aspect which provided in Spring Data Mongo. You have to add some code fragments to Spring configuration. <!-- Mongo cross-store aspect config --> <bean class="org.spri