下面列出了怎么用javax.persistence.PrePersist的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();
}
}
@PrePersist
public void prePersist() throws Exception {
if (StringUtils.isEmpty(this.getId())) {
throw new Exception("basePrePersist error, id is empty, entity class:" + this.getClass().getName()
+ ", entity content:" + XGsonBuilder.toJson(this) + ".");
}
Date date = new Date();
if (null == this.getCreateTime()) {
this.setCreateTime(date);
}
this.setUpdateTime(date);
if (StringUtils.isEmpty(this.getSequence())) {
this.setSequence(StringUtils.join(DateTools.compact(this.getCreateTime()), this.getId()));
}
this.onPersist();
}
@PrePersist
private void persist() {
Calendar cal = Calendar.getInstance();
Date date = new Date();
cal.setTime(date);
this.lifeCycleStatus = "N";
this.lifeCycleStatusName = "New";
int itemNumber = 10;
this.netAmount = new BigDecimal("0.00");
this.taxAmount = new BigDecimal("0.00");
this.grossAmount = new BigDecimal("0.00");
this.createdAt = cal;
for (SalesOrderItem item : salesOrderItems) {
item.setSalesOrderId(this.getSalesOrderId());
item.setItemNumber(itemNumber);
itemNumber += 10;
item.persist();
this.netAmount = this.netAmount.add(item.getNetAmount())
.setScale(3);
this.taxAmount = this.taxAmount.add(item.getTaxAmount())
.setScale(3);
this.grossAmount = this.grossAmount.add(item.getGrossAmount())
.setScale(3);
}
}
@PrePersist
public void initId() {
if (null == this.id || this.id.trim().equals("")) {
this.id = DomainIdBuilder.buildDomainId(null != pluginPackage ? pluginPackage.getId() : null, schemaName,
username);
}
}
/**
* 持久化前处理
*/
@PrePersist
public void prePersist() {
if (getStock() == null) {
setAllocatedStock(0);
}
setScore(0F);
}
@PrePersist
void prePersist() {
callbacks.add(PrePersist.class);
if (this.id == null) {
this.id = nextId();
}
this.createdAt = this.updatedAt = System.currentTimeMillis();
}
@PrePersist
public void touchForCreate(UtcTimeLongIdEntity entity) {
if (entity.getUtcCreate() == null || entity.getUtcModified() == null) {
Instant now = Instant.now();
entity.setUtcCreate(now);
entity.setUtcModified(now);
}
}
/**
* <p>prePersist.</p>
*/
@PrePersist
public void prePersist() {
if(this.getCreatedAt()==null) {
this.setCreatedAt(Calendar.getInstance());
}
if(this.getModifiedAt()==null) {
this.setModifiedAt(Calendar.getInstance());
}
}
/**
* <p>prePersist.</p>
*/
@PrePersist
public void prePersist() {
if(this.getCreatedAt()==null) {
this.setCreatedAt(Calendar.getInstance());
}
if(this.getModifiedAt()==null) {
this.setModifiedAt(Calendar.getInstance());
}
// If uploaded by old client (<3.0.0), the digest also must be set
if(this.sha1!=null && (this.digest==null || this.digestAlgorithm==null)) {
this.digest = this.sha1;
this.digestAlgorithm = DigestAlgorithm.SHA1;
}
}
/**
* Sets {@link GoalExecution#createdAt}.
*/
@PrePersist
public void prePersist() {
if(this.getCreatedAt()==null) {
this.setCreatedAt(Calendar.getInstance());
}
}
/**
* <p>prePersist.</p>
*/
@PrePersist
public void prePersist() {
if(this.getCreatedAt()==null)
this.setCreatedAt(Calendar.getInstance());
this.setModifiedAt(Calendar.getInstance());
}
/**
* <p>prePersist.</p>
*/
@PrePersist
public void prePersist() {
if(this.getTraced()==null) {
this.setTraced(false);
}
}
/**
* <p>prePersist.</p>
*/
@PrePersist
public void prePersist() {
if(this.getCreatedAt()==null)
this.setCreatedAt(Calendar.getInstance());
this.setLastModified(Calendar.getInstance());
}
/**
* <p>prePersist.</p>
*/
@PrePersist
public void prePersist() {
if(this.getCreatedAt()==null) {
this.setCreatedAt(Calendar.getInstance());
}
this.setModifiedAt(Calendar.getInstance());
this.setLastScan(Calendar.getInstance());
this.setLastVulnChange(Calendar.getInstance());
}
/**
* <p>prePersist.</p>
*/
@PrePersist
public void prePersist() {
if(this.getCreatedAt()==null)
this.setCreatedAt(Calendar.getInstance());
this.setLastModified(Calendar.getInstance());
}
@PrePersist
private void initValuesPersist() {
status = (status == null) ? Status.ACTIVE : status;
creationDate = LocalDateTime.now();
code = code.trim();
}
@PrePersist
public void beforePersist(Object entity) {
if (entity instanceof AbstractAuditableEntity) {
AbstractAuditableEntity o = (AbstractAuditableEntity) entity;
final LocalDateTime now = LocalDateTime.now();
o.setCreatedDate(now);
o.setLastModifiedDate(now);
if (o.getCreatedBy() == null) {
o.setCreatedBy(currentUser());
}
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
if (configMap != null) {
this.config = JsonUtil.toJson(configMap);
}
}
@Test
public void testInsert() throws Exception {
User user = new User();
user.name = "Michael";
user.email = "[email protected]";
warpdb.insert(user);
assertTrue(user.callbacks.contains(PrePersist.class));
assertTrue(user.callbacks.contains(PostPersist.class));
assertEquals("0001", user.id);
assertEquals(user.createdAt, user.updatedAt);
assertEquals(System.currentTimeMillis(), user.createdAt, 500.0);
}
@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);
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
if (configMap != null) {
this.config = JsonUtil.toJson(configMap);
}
}
@PrePersist
@PreUpdate
public void save() throws JsonProcessingException {
super.save();
if (ruleDescriptionMap != null) {
this.ruleDescription = JsonUtil.toJson(ruleDescriptionMap);
}
}
@PrePersist
public void setCreatedTime() {
if (createdTime != null)
return;
createdTime = LocalDateTime.now();
}
/**
* 持久化前处理
*/
@PrePersist
public void prePersist() {
if (getArea() != null) {
setAreaName(getArea().getFullName());
}
if (getPaymentMethod() != null) {
setPaymentMethodName(getPaymentMethod().getName());
}
if (getShippingMethod() != null) {
setShippingMethodName(getShippingMethod().getName());
}
}
@PrePersist
private void onCreate() {
createdAt = new Date();
createdBy = getCurrentAuditor();
lastModifiedAt = createdAt;
lastModifiedBy = createdBy;
saveOnly = false;
}
/**
* <p>
* Method called by the JPA layer before persisting the fields.
* </p>
*/
@PrePersist
@SuppressWarnings("unused")
private void setPersistentFields()
{
if (this.permission != null)
this.bitMask = this.permission.getMaskValue();
}
@PrePersist
public void prePersist() {
if(this.createDate == null){
this.setCreateDate(new Date());
}
this.setUpdateDate(new Date());
if(StringUtils.isBlank(this.getDeleteFlag())){
this.setDeleteFlag(BaseEntity.DELETE_FLAG_NORMAL);
}
}
@PrePersist
protected void prePersist() {
if (AuditContextHolder.audit()) {
setCreationAuthor(AuditContextHolder.username());
setCreationDate(Instant.now());
}
}