jpa - EclipseLink Descriptor Customizer & History Policy and JSF. How to insert user principals in history? -
in jsf web application, use eclipselink
descriptor customizer
and
history policy
to populate history table in database. corresponding jpa entity class annotated @customizer(beans.historylesionhcustomizer.class)
the history table has same fields source table, plus 2 fields (start_date & end_date) specify start , end of operation on row. working. need populate field in history table. field called user, should populated user principals, , allow me trace user performed cud (create/update/delete) operation. thought history policy allow me add field indicating corresponding name in database , indicate object value must inserted. not case, or may not able figure how can done. in other words, along start_date , end_date, want populate user field :
facescontext.getcurrentinstance().getexternalcontext().getremoteuser();
package beans; /** * whenever there change on record or insert, change traced. * @author mediterran * */ import javax.faces.context.facescontext; import org.eclipse.persistence.config.descriptorcustomizer; import org.eclipse.persistence.descriptors.classdescriptor; import org.eclipse.persistence.history.historypolicy; public class historylesionhcustomizer implements descriptorcustomizer { @override /** * implementation method use */ public void customize(classdescriptor cd) throws exception { string user = facescontext.getcurrentinstance().getexternalcontext().getremoteuser(); historypolicy policy = new historypolicy(); // instantiates new policy //policy.postupdate(); policy.usedatabasetime(); // use default database time avoid date conflict policy.addhistorytablename("history_lesionh"); // indicate source table name policy.addstartfieldname("start_date"); // indicate start date db column policy.addendfieldname("end_date"); // indicate end date db column cd.sethistorypolicy(policy); // use policy entity class used @customizer(historylesionhcustomizer.class) } } any or workarounds appreciated. thanks
unfortunately historypolicy adds start , end date. can add user information entity of entitylisteners . here example. add user information each persist/update of customer table:
import javax.persistence.entitylisteners; @entity @entitylisteners(auditlistener.class) @table(name = "customer") public class customer implements serializable { @column(name = "user") private string user; // getter , setter } and auditlistener:
import javax.persistence.prepersist; import javax.persistence.preupdate; //... public class auditlistener { @prepersist @preupdate public void setuserinformation(object entity) { if (entity instanceof customer) { customer myentity = (customer) entity; myentity.setuser(facescontext.getcurrentinstance().getexternalcontext().getremoteuser()); } } } if have more 1 column needs user information, can use mappedsuperclass entity , put user column in class. let auditable entities extend mappedsuperclass , check in auditlistener if entity instance of superclass.
Comments
Post a Comment