Skip to content

Spring Data ArangoDB : Save parent and child together #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
naveen-kinnal opened this issue Jun 20, 2020 · 4 comments
Closed

Spring Data ArangoDB : Save parent and child together #189

naveen-kinnal opened this issue Jun 20, 2020 · 4 comments
Labels

Comments

@naveen-kinnal
Copy link

naveen-kinnal commented Jun 20, 2020

I am trying to build my Java Spring Boot interpretation of saving ArangoDB nodes. My code looks like below Asset.java

@Data
@Document("assets")
public class Asset {

    @Id
    String key;
    String name;
    String description;
    String path;
    LocalDateTime imported_at;
}

and AssetContent.java as

@Data
@Document("asset_contents")
public class AssetContent {

    @Id
    String key;
    String name;
    String extension;
    String content;
}

ChildOf.java to give a relation between Asset and AssetContent

@Data
@Edge("edges")
public class ChildOf<T1, T2> {

    @Id
    private String id;

    @From
    private T1 child;

    @To
    private T2 parent;

    public ChildOf(final T1 child, final T2 parent) {
        super();
        this.child = child;
        this.parent = parent;
    }
}

where One Asset has multiple AssetContents. Now I want to achieve 3 things

  1. Save an Asset and list of Asset Contents at once (Create a list of AssetContents and store in my Asset.java, then save Asset should also save AssetContents).
  2. Get the asset using the AssetContent repository.
  3. Get the list of asset contents using the AssetRepository.

Since I am new to ArangoDB and also because of the limited tutorial. Could someone help me with the changes I have to make in the code?

@rashtao
Copy link
Collaborator

rashtao commented Jul 21, 2020

  1. Spring Data ArangoDB doesn't handle cascading operations. So if you save a parent, the child is not saved automatically. You can implement such functionality by yourself performing the 2 operations in sequence, but note that in this case there is no transactions support (Feature: support of @Transactional #80).

  2. In order to follow the edge from child to parent you can use the @Relations annotation (https://www.arangodb.com/docs/stable/drivers/spring-data-reference-mapping-relations.html). You can anyways return Asset from AssetContentRepository, eg.:

public interface AssetContentRepository extends ArangoRepository<AssetContent, String> {

    @Query("<your AQL query returning Asset>")
    Asset findAsset(AssetContent assetContent);

}
  1. Same as (2).

@rashtao
Copy link
Collaborator

rashtao commented Dec 21, 2020

Closing for now, please reopen if still relevant.

@rashtao rashtao closed this as completed Dec 21, 2020
@bobaikato
Copy link

So inconvenient to this tool without Transaction support.

From the first point of your #189 (comment). When performing two sequential transactions, if the second fails, the developer is responsible for reverting the operation?

Other than deleting the first entry because the second failed? Is there is a recommended way to handle such cases with ArangoDB?

@rashtao
Copy link
Collaborator

rashtao commented Dec 13, 2021

Until support for stream transactions will be implemented, a possible workaround could be saving multiple documents (or edges) from within the same AQL query, e.g. something like:

    @Query("FOR d IN [@a, @b] INSERT d INTO #collection RETURN NEW")
    <T> List<T> saveBoth(@Param("a") T a, @Param("b") T b)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants