跳至主要内容

博文

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

JPA Data Auditing

Auditing Sometime we need to keep track with the change of the Entity, eg. When it is createdWho created itLast modified dateLast 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 methodprePersistannotated with@PrePresistindicates it will be executed beforeem.persist(em is stand for jpa EntityManager) is executed. And

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 TheConferenceandSignupare still used as example models,Conferenceis JPA entity class andSignupis a Mongo Document. @Entity public class Conference { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private Long id; @RelatedDocument private Signup signup; } TheSignupDocument is defined as the following. @Document public class Signup { @Id private String id; } @RelatedDocumentannotation indicates thesignupproperty of Conference is a reference ofSignupMongo 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 cl