在我的代码中,我试图创建一个现有实体的副本,其中某些属性已更改。首先,我使旧实体处于非活动状态,然后创建具有新属性的新副本,并将新实体连接到与旧实体相同的关系。由于某种原因,在保存新实体时,新属性(在下面的代码中称为editProperties)也会自动复制到旧属性中,这是不希望的。
如果我将深度为0的新实体保存为正常,并且不会复制属性,但是使用默认保存时,我会遇到上述问题
下面是我在定义的类中使用的代码
public void editAsset(String company, String assetCode, Map<String, String> editProperties) {
Asset asset = assetRepository.findByCodeAndActiveIndAndCurrentInd(assetCode, true, true)
;
AssetType assetType = assetTypeRepository.findByCompanyAndType(company, asset.getType())
;
for (String editProperty : editProperties.keySet()) {
if (assetType.getAllowedProperties().stream()
.anyMatch(ap -> ap.getName().equals(editProperty) && ap.getUpgradeOnUpdate())) {
asset = upgradeAsset(asset);
break;
}
}
for (Entry<String, String> newAssetProp : editProperties.entrySet()) {
asset.getProperties().put(newAssetProp.getKey(), newAssetProp.getValue());
}
assetRepository.save(asset,0); // Problem when saved with default depth
}
private Asset upgradeAsset(Asset oldAsset) {
oldAsset.setCurrentInd(false);
Asset newAsset = new Asset(oldAsset.getName(), oldAsset.getType(), oldAsset.getCode(), oldAsset.getParentOrg(),
oldAsset.getProperties(), true, true);
for (AssetConnection oldConOut : oldAsset.getConnnectionOut()) {
AssetConnection newAssetConnectionOut = new AssetConnection(newAsset, oldConOut.getEndAsset(),
oldConOut.getType(), oldConOut.getProperties());
newAsset.getConnnectionOut().add(newAssetConnectionOut);
}
for (AssetConnection oldConIn : oldAsset.getConnnectionIn()) {
AssetConnection newAssetConnectionIn = new AssetConnection(oldConIn.getStartAsset(), newAsset,
oldConIn.getType(), oldConIn.getProperties());
newAsset.getConnnectionIn().add(newAssetConnectionIn);
}
assetRepository.saveAll(Arrays.asList(oldAsset,newAsset));
return newAsset;
}
@NodeEntity
public class Asset {
@Id
@GeneratedValue
@JsonIgnore
private Long id;
@Property("name")
private String name;
@Property("type")
private String type;
@ApiModelProperty(hidden = true)
@Property("code")
private String code = UUID.randomUUID().toString();
@ApiModelProperty(hidden = true)
@Property("active_ind")
private boolean activeInd;
@ApiModelProperty(hidden = true)
@Property("current_ind")
private boolean currentInd;
@ApiModelProperty(hidden = true)
@Relationship(type = "ASSET_CONNECTION")
private Set<AssetConnection> connnectionOut = new HashSet<>();
@JsonIgnore
@Relationship(type = "ASSET_CONNECTION", direction = Relationship.INCOMING)
private Set<AssetConnection> connnectionIn = new HashSet<>();
@JsonIgnore
@Relationship(type = "ORG_ASSET", direction = Relationship.INCOMING)
private Organisation parentOrg;
@Properties
private Map<String, String> properties = new HashMap<>();
public Asset() {
}
}
public class AssetType {
@Id
@GeneratedValue
@JsonIgnore
private Long id;
@Property("type")
private String type;
@Property("company")
private String company;
@Relationship(type = "ALLOWED_PROPERTY")
private Set<PropertyType> allowedProperties;
}