在ActiveJDBC中,如何更新其中一个复合键字段的值?



我正在使用ActiveJDBC。 我有一个由复合键(parentId,childId,effStartDate(组成的表。 我有一个新的要求来允许更新effStartDate字段。 当我尝试更新字段并保存时,它出错了。 有没有合适的方法可以使用ORM或ActiveJDBC中的原始SQL方法来更新它?

当前方法:

mymodel.setEffStartDate(newStartDate);
mymodel.saveIt();

已更新以提供更多详细信息。 这是确切的代码:

try {
ri.setEffStartDate(ld);
boolean didItSave = ri.saveIt(user);
System.out.println("here - " + didItSave);
} catch(Exception e) {
System.out.println("Exception: " + e);
}

当 saveIt 运行时,它不会出错。 但是,它返回 FALSE,并且数据库中没有任何更新。 所以我在前面的陈述中误用了"错误"。我应该说它什么都没做。

我们正在使用甲骨文数据库。

尝试更新的模型类:

package com.brookdale.model;
import java.time.LocalDate;
import org.javalite.activejdbc.annotations.BelongsTo;
import org.javalite.activejdbc.annotations.BelongsToParents;
import org.javalite.activejdbc.annotations.CompositePK;
import org.javalite.activejdbc.annotations.Table;
import org.javalite.activejdbc.validation.ValidationException;
import com.brookdale.model.activejdbc.CecilModel;
@Table("MONET.CONTACTREL")
@CompositePK({ "parentContactID", "childContactID", "contactRelTypeID", "effStartDate" })
@BelongsToParents({
@BelongsTo(foreignKeyName="relationshipId",parent=Relationship.class),
@BelongsTo(foreignKeyName="contactRelTypeID",parent=ContactRelType.class),
//  @BelongsTo(foreignKeyName="parentContactID",parent=Contact.class),
//  @BelongsTo(foreignKeyName="childContactID",parent=Contact.class),
@BelongsTo(foreignKeyName="childContactID",parent=Contact.class),
@BelongsTo(foreignKeyName="childContactID",parent=ResidentContactDetail.class)
})
public class ContactRel extends CecilModel {
private ResidentContactDetail emergResidentContactDetail;
private ResidentContactDetail healthResidentContactDetail;
private ResidentContactDetail finResidentContactDetail;
private int emergresidentind = 0;
public Long getParentContactID() {
return getLong("parentContactID");
}
public void setParentContactID(Long parentContactID) {
set("parentContactID",parentContactID);
}
public Long getChildContactID() {
return getLong("childContactID");
}
public void setChildContactID(Long childContactID) {
set("childContactID",childContactID);
}   
public LocalDate getEffStartDate() {
return getLocalDate("effStartDate");
}
public void setEffStartDate(LocalDate effStartDate) {
setLocalDate("effStartDate",effStartDate);
}
public LocalDate getEffEndDate() {
return getLocalDate("effEndDate");
}
public void setEffEndDate(LocalDate effEndDate) {
setLocalDate("effEndDate",effEndDate);
}
public Integer getContactRelTypeID() {
return getInteger("contactRelTypeID");
}
public void setContactRelTypeID(Integer contactRelTypeID) {
set("contactRelTypeID",contactRelTypeID);
}
public Integer getRelationshipId() {
return getInteger("relationshipId");
}
public void setRelationshipId(Integer relationshipId) {
set("relationshipId",relationshipId);
}
public Integer getParentIsPrimaryResidentInd() {
return getInteger("parentIsPrimaryResidentInd");
}
public void setParentIsPrimaryResidentInd(Integer parentIsPrimaryResidentInd) {
set("parentIsPrimaryResidentInd",parentIsPrimaryResidentInd);
}
public Integer getParentIsSecondPersonInd() {
return getInteger("parentIsSecondPersonInd");
}
public void setParentIsSecondPersonInd(Integer parentIsSecondPersonInd) {
set("parentIsSecondPersonInd",parentIsSecondPersonInd);
}
public int getCourtesyCopyInd() {
return getInteger("courtesyCopyInd");
}
public void setCourtesyCopyInd(Integer courtesyCopyInd) {
set("courtesyCopyInd",courtesyCopyInd);
}
/* Additional helper methods */
public Contact getParentContact() {
return Contact.findById(getParentContactID());
}
public Contact getChildContact() {
return Contact.findById(getChildContactID());
}
public int getEmergresidentind() {
return emergresidentind;
}
public void setEmergresidentind(int emergresidentind) {
this.emergresidentind = emergresidentind;
}
@Override
public void validate(){
super.validate();
validatePresenceOf("parentContactID", "Parent Contact is required.");
validatePresenceOf("childContactID", "Contact is required.");
validatePresenceOf("contactRelTypeID", "Resident relationship type is required.");
validatePresenceOf("effStartDate", "Effective Start Date is required.");
validatePresenceOf("relationshipid", "Relationship is required.");
if(this.getEffEndDate() != null) {
if(this.getEffEndDate().isBefore(this.getEffStartDate())) {
this.addError("effenddate", "End date must be on or after the start date.");
}
}
if(this.hasErrors()) {
throw new ValidationException(this);
}
}
}

我们的 CecilModel 类我们正在扩展 Model 类。 Package com.brookdale.model.activejdbc;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.axis.utils.StringUtils;
import org.apache.log4j.Logger;
import org.javalite.activejdbc.Model;
import org.javalite.activejdbc.validation.ValidationBuilder;
import org.javalite.activejdbc.validation.ValidationException;
import org.javalite.activejdbc.validation.ValidatorAdapter;
import com.brookdale.core.CLArgs;
import com.brookdale.exception.CecilErrorsException;
import com.brookdale.message.CecilMessage;
import com.brookdale.model.Building;
import com.brookdale.security.bo.User;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import oracle.sql.DATE;
public abstract class CecilModel extends Model {
final static Logger logger = Logger.getLogger(CecilModel.class);
private static final transient TypeReference<HashMap<String, Object>> mapType = new TypeReference<HashMap<String, Object>>() {};
private static final transient TypeReference<ArrayList<HashMap<String, Object>>> listMapType = new TypeReference<ArrayList<HashMap<String, Object>>>() {};
//add -0600 to specify cental time zone
private static final transient SimpleDateFormat jsonDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); 
private transient Map<String, Collection<Map<String, Object>>> jsonInclude = new HashMap<>();
public Timestamp getUpdateDateTime() {
return getTimestamp("updateDateTime");
}
public void setUpdateDateTime(LocalDateTime updateDateTime) {
set("updateDateTime",updateDateTime == null ? null : Timestamp.valueOf(updateDateTime));
}
public Timestamp getCreateDateTime() {
return getTimestamp("createDateTime");
}
public void setCreateDateTime(LocalDateTime createDateTime) {
set("createDateTime",createDateTime == null ? null : Timestamp.valueOf(createDateTime));
}
public String getUpdateUsername() {
return getString("updateUsername");
}
public void setUpdateUsername(String updateUsername) {
set("updateUsername",updateUsername);
}
public String getCreateUsername() {
return getString("createUsername");
}
public void setCreateUsername(String createUsername) {
set("createUsername",createUsername);
}
public Long getUpdateTimeId() {
return getLong("updatetimeid");
}
public void setUpdateTimeId(Long updateTimeId) {
if (updateTimeId == null)
setLong("updatetimeid", 1);
else
setLong("updatetimeid",updateTimeId);
}
public void incrementUpdateTimeId() {
Long updatetimeid = this.getUpdateTimeId();
if (updatetimeid == null)
this.setUpdateTimeId(1L);
else
this.setUpdateTimeId(updatetimeid+1L);
}

public boolean save(User user) {
String userId = (CLArgs.args.isAuthenabled()) ? user.getUserid() : "TEST_MODE";
// insert
java.sql.Timestamp now = java.sql.Timestamp.valueOf(java.time.LocalDateTime.now());
if (this.getId() == null || this.getId().toString().equals("0")) {
this.setId(null);
this.set("createDateTime", now);
this.set("createUsername", userId);
this.set("updateDateTime", now);
this.set("updateUsername", userId);
this.set("updateTimeId", 1);
} 
// update
else {
Long updatetimeid = this.getLong("updateTimeid");
this.set("updateDateTime", now);
this.set("updateUsername", userId);
this.set("updateTimeId",  updatetimeid == null ? 1 : updatetimeid + 1);
}
return super.save();
}
public boolean saveIt(User user) {
String userId = (CLArgs.args.isAuthenabled()) ? user.getUserid() : "TEST_MODE";
// insert
java.sql.Timestamp now = java.sql.Timestamp.valueOf(java.time.LocalDateTime.now());
if (this.isNew()) {
this.setId(null);
this.set("createDateTime", now);
this.set("createUsername", userId);
this.set("updateDateTime", now);
this.set("updateUsername", userId);
this.set("updateTimeId", 1);
} 
// update
else {
Long updatetimeid = this.getLong("updateTimeid");
this.set("updateDateTime", now);
this.set("updateUsername", userId);
this.set("updateTimeId",  updatetimeid == null ? 1 : updatetimeid + 1);
}
return super.saveIt();
}
public boolean saveModel(User user, boolean insert) {
return saveModel(user.getUserid(), insert);
}
public boolean saveModel(String userId, boolean insert) {
userId = (CLArgs.args.isAuthenabled()) ? userId : "TEST_MODE";
if(insert){
return this.insertIt(userId);
}else{
return this.updateIt(userId);
}
}
public boolean insertIt(String user) {
user = (CLArgs.args.isAuthenabled()) ? user : "TEST_MODE";
// insert
java.sql.Timestamp now = java.sql.Timestamp.valueOf(java.time.LocalDateTime.now());     
this.set("createDateTime", now);
this.set("createUsername", user);
this.set("updateDateTime", now);
this.set("updateUsername", user);
this.set("updateTimeId", 1);
this.validate();
if(this.hasErrors()){
throw new ValidationException(this);
}
boolean inserted = super.insert();
if (!inserted)
throw new CecilErrorsException(new CecilMessage().error("Failed to insert " + this.getClass().getSimpleName()));
return inserted;
}
public boolean updateIt(String user) {
user = (CLArgs.args.isAuthenabled()) ? user : "TEST_MODE";
// update
java.sql.Timestamp now = java.sql.Timestamp.valueOf(java.time.LocalDateTime.now());     
this.set("updateDateTime", now);
this.set("updateUsername", user);
this.incrementUpdateTimeId();
this.validate();
if(this.hasErrors()){
throw new ValidationException(this);
}
boolean updated = super.save();
if (!updated)
throw new CecilErrorsException(new CecilMessage().error("Failed to update " + this.getClass().getSimpleName()));
return updated;
}
@Override
public <T extends Model> T set(String field, Object value) {
if (value instanceof LocalDate) {
return super.set(field, java.sql.Date.valueOf((LocalDate)value));
} else if (value instanceof LocalDateTime) {
return super.set(field, java.sql.Timestamp.valueOf((LocalDateTime)value));
} else {
return super.set(field, value);
}
}
public LocalDate getLocalDate(String field) {
if (field == null || "".equals(field))
return null;
else {
java.sql.Date d = getDate(field);
return d == null ? null : d.toLocalDate();
}
}
public void setLocalDate(String field, LocalDate value) {
if (value == null)
setDate(field, null);
else
setDate(field, java.sql.Date.valueOf(value));
}
public LocalDateTime getLocalDateTime(String field) {
java.sql.Timestamp d = getTimestamp(field);
return d == null ? null : d.toLocalDateTime();
}
public void setLocalDateTime(String field, LocalDateTime value) {
if (value == null)
setTimestamp(field, null);
else
setTimestamp(field, java.sql.Timestamp.valueOf(value));
}
}

如果这样的记录(根据主键(已经存在,则 Model.saveIt(( 方法会将模型属性保存到表中。如果将主键设置为数据库中未找到的值,则此方法将退出而不执行太多操作。

此外,您的代码中有一些问题不是 JavaLite 的惯用语。

更新:根据您在下面的评论,如果记录不是"新",框架将更新记录,这意味着如果 PK 或组合键不为空,它将假定您知道自己在做什么,并期望通过主键或组合键在表中找到该记录,因此,如果它们设置了键而不是 null, 它将始终生成更新而不是插入。因此,如果将主键设置为表中不存在的值,则saveIt()save()将不执行任何操作。有关详细信息,请参阅 http://javalite.io/surrogate_primary_keys。

此外,还可以启用日志记录以查看它生成的 SQL。

相关内容

最新更新