下面列出了怎么用javax.persistence.PreUpdate的API类实例代码及写法,或者点击链接到github查看源代码。
@PrePersist
@PreUpdate
private void persist() {
EntityManagerFactory emf = Utility.getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
if (this.quantity.compareTo(this.minStock) < 0) {
this.quantityLessMin = true;
} else {
this.quantityLessMin = false;
}
em.getTransaction().commit();
} finally {
em.close();
}
}
@PreUpdate
public void beforeUpdate(Object entity) {
if (entity instanceof AbstractAuditableEntity) {
AbstractAuditableEntity o = (AbstractAuditableEntity) entity;
o.setLastModifiedDate(LocalDateTime.now());
if (o.getLastModifiedBy()== null) {
o.setLastModifiedBy(currentUser());
}
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
if (configMap != null) {
this.predicateConfig = JsonUtil.toJson(configMap);
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
if (configMap != null) {
this.config = JsonUtil.toJson(configMap);
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
if (checkpointMap != null) {
this.checkpoint = JsonUtil.toJson(checkpointMap);
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
if (sinksList != null) {
this.sinks = JsonUtil.toJson(sinksList);
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
if (detailsMap != null) {
this.details = JsonUtil.toJson(detailsMap);
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
if (detailsMap != null) {
this.details = JsonUtil.toJson(detailsMap);
}
if (outList != null) {
this.out = JsonUtil.toJson(outList);
}
}
@Override
@PreUpdate
protected void preUpdate() {
super.preUpdate();
if (!this.decryptionNeeded) {
encryptExternalId();
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
super.save();
if (ruleDescriptionMap != null) {
this.ruleDescription = JsonUtil.toJson(ruleDescriptionMap);
}
}
@PreUpdate
void onPreUpdate(Object o) {
String txId = (String)ThreadLocalContext.get(CompositeTransactionParticipantService.CURRENT_TRANSACTION_KEY);
if (null == txId){
LOG.info("onPreUpdate outside any transaction");
} else {
LOG.info("onPreUpdate inside transaction [{}]", txId);
enlist(o, EntityCommand.Action.UPDATE, txId);
}
}
@PreUpdate
public void beforeUpdate(Object entity) {
if (entity instanceof AbstractAuditableEntity) {
AbstractAuditableEntity o = (AbstractAuditableEntity) entity;
o.setLastModifiedDate(LocalDateTime.now());
if (o.getLastModifiedBy() == null) {
o.setLastModifiedBy(currentUser());
}
}
}
@PreUpdate
private void update() {
String userName = null;
try {
userName = (String) ODataAuthorization.getThreadLocalData().get().get("UserName");
} catch (RuntimeException e) {};
if (userName != null) {
this.updatedAt = Calendar.getInstance();
this.updatedBy = userName;
}
}
@PreUpdate
public void beforeUpdate() {
setUpdatedAt(LocalDateTime.now());
if (PUBLISHED == this.status) {
setPublishedAt(LocalDateTime.now());
}
}
@PreUpdate
public void beforeUpdate() {
setUpdatedAt(LocalDateTime.now());
if (PUBLISHED == this.status) {
setPublishedAt(LocalDateTime.now());
}
}
@PreUpdate
public void beforeUpdate() {
setUpdatedAt(LocalDateTime.now());
if (PUBLISHED == this.status) {
setPublishedAt(LocalDateTime.now());
}
}
@PreUpdate
protected void preUpdate() {
if (AuditContextHolder.audit()) {
setLastModificationAuthor(AuditContextHolder.username());
setLastModificationDate(Instant.now());
}
}
/**
* 更新前处理
*/
@PreUpdate
public void preUpdate() {
if (getStock() == null) {
setAllocatedStock(0);
}
if (getTotalScore() != null && getScoreCount() != null && getScoreCount() != 0) {
setScore((float) getTotalScore() / getScoreCount());
} else {
setScore(0F);
}
}
/**
* 更新前处理
*/
@PreUpdate
public void preUpdate() {
if (getArea() != null) {
setAreaName(getArea().getFullName());
}
if (getPaymentMethod() != null) {
setPaymentMethodName(getPaymentMethod().getName());
}
if (getShippingMethod() != null) {
setShippingMethodName(getShippingMethod().getName());
}
}
/**
*
* @param entity
*/
@PreUpdate
public <U, I extends Serializable, T> void preUpdate(TreeEntity<U, I, T> entity) {
@SuppressWarnings("unchecked")
TreeEntity<U, I, T> parent = (TreeEntity<U, I, T>) entity.getParent();
if (parent != null) {
entity.setTreePath(parent.getTreePath() + parent.getId() + TREE_PATH_SEPARATOR);
} else {
entity.setTreePath(TREE_PATH_SEPARATOR);
}
}
@PreUpdate
protected void preUpdate() {
if (StringUtils.isEmpty(getObjectId())) {
setObjectId(UUID.randomUUID().toString());
}
lastUpdateTimestamp = new Timestamp(System.currentTimeMillis());
}
/**
* A listener method which is invoked on instances of TransactionalEntity
* (or their subclasses) prior to being updated. Sets the
* <code>updated</code> audit values for the entity. Attempts to obtain this
* thread's instance of username from the RequestContext. If none exists,
* throws an IllegalArgumentException. The username is used to set the
* <code>updatedBy</code> value. The <code>updatedAt</code> value is set to
* the current timestamp.
*/
@PreUpdate
public void beforeUpdate() {
String username = RequestContext.getUsername();
if (username == null) {
throw new IllegalArgumentException(
"Cannot update a TransactionalEntity without a username "
+ "in the RequestContext for this thread.");
}
setUpdatedBy(username);
setUpdatedAt(new DateTime());
}
/**
* A listener method which is invoked on instances of TransactionalEntity (or their subclasses) prior to being
* updated. Sets the <code>updated</code> audit values for the entity. Attempts to obtain this thread's instance of
* username from the RequestContext. If none exists, throws an IllegalArgumentException. The username is used to set
* the <code>updatedBy</code> value. The <code>updatedAt</code> value is set to the current timestamp.
*/
@PreUpdate
public void beforeUpdate() {
final String username = RequestContext.getUsername();
if (username == null) {
throw new IllegalArgumentException("Cannot update a TransactionalEntity without a username "
+ "in the RequestContext for this thread.");
}
setUpdatedBy(username);
setUpdatedAt(Instant.now());
}
@PreUpdate
@PrePersist
private void prePersist() {
if ("*".equals(userIdHolder)) {
userId = null;
} else {
userId = userIdHolder;
}
}
@PrePersist
@PreUpdate
public void validate() {
if (parameters != null) {
for (Map.Entry<String, String> e : parameters.entrySet()) {
if (e.getValue() == null) {
throw new IllegalStateException(
format(
"Parameter '%s' of the source %s is null. This is illegal.", e.getKey(), this));
}
}
}
}
@PrePersist
@PreUpdate
private void setCurrentTimestamp(Object entity) {
if(entity instanceof Updatable) {
Updatable updatable = (Updatable) entity;
updatable.setTimestamp(new Date());
}
}
/**
* Life-cycle event callback, which automatically sets the last modification date.
*/
@PreUpdate
protected void updateAuditInformation()
{
lastModifiedAt = new Date();
// TODO - obtain currently logged-on user
}
@PrePersist
@PreUpdate
public void validate(final Object object) {
final Validator validator = ApplicationContextProvider.getBeanFactory().getBean(Validator.class);
Set<ConstraintViolation<Object>> violations = validator.validate(object);
if (!violations.isEmpty()) {
LOG.warn("Bean validation errors found: {}", violations);
Class<?> entityInt = null;
for (Class<?> interf : ClassUtils.getAllInterfaces(object.getClass())) {
if (!Entity.class.equals(interf)
&& !ProvidedKeyEntity.class.equals(interf)
&& !Schema.class.equals(interf)
&& !Task.class.equals(interf)
&& !Policy.class.equals(interf)
&& !GroupableRelatable.class.equals(interf)
&& !Any.class.equals(interf)
&& !DynMembership.class.equals(interf)
&& Entity.class.isAssignableFrom(interf)) {
entityInt = interf;
}
}
throw new InvalidEntityException(entityInt == null
? "Entity" : entityInt.getSimpleName(), violations);
}
}
/**
* Makes sure only {@link User}s with encrypted {@link Password} can be persisted.
*/
@PrePersist
@PreUpdate
void assertEncrypted() {
if (!password.isEncrypted()) {
throw new IllegalStateException("Tried to persist/load a user with a non-encrypted password!");
}
}
@PrePersist
@PreUpdate
public void preUpdate() {
if (this.passwordHash != null && !this.passwordHash.equals(this.storedPassword))
this.expirationDate = DateUtils.addDays(new Date(), 90);
}