私はCrudRepositoryを拡張するインタフェースを持っています。既存のオブジェクトを新しいオブジェクトで上書きします。 save
をどのように上書きすれば、そのキーを持つオブジェクトがすでに存在するかどうかを確認できますか?CRUD Repository:Save to Implementが実装されています
私はすでに次のことを試してみましたが、それはうまくいきませんでした:
public interface CustomRepo {
public Channel save(Channel channel);
}
public class RepositoryImpl implements CustomRepo {
@Autowired
private DynamoDBMapper mapper;
public Channel save(Channel channel){
channel.setId(UUID.randomUUID().toString());
DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
Map<String, ExpectedAttributeValue> expectedAttributes =
ImmutableMap.<String, ExpectedAttributeValue>builder()
.put(channel.getUrl(), new ExpectedAttributeValue(false))
.put(channel.getTitle(), new ExpectedAttributeValue(false))
.build();
saveExpression.setExpected(expectedAttributes);
saveExpression.setConditionalOperator(ConditionalOperator.AND);
try {
mapper.save(channel, saveExpression);
} catch (ConditionalCheckFailedException e) {
System.err.println("Item with URL {} already exists. Cannot overrwrite."+ channel.getUrl());
System.err.println(e.getMessage());
}
return channel;
}
public interface ChannelRepository extends CrudRepository<Channel, String>, CustomRepo {
@EnableScan
List<Channel> findByTitle(String title);
@Override
@EnableScan
List<Channel> findAll();
List<Channel> findByUrl(String url);
}
のためのいくつかの属性を更新するために使用することができますいくらかの能力。しかし、私は自分のセーブロジックを定義するためにsaveメソッドをオーバーライドすることについて具体的に尋ねました。 – acs254