Corda使用配置文件保持状态

时间:2018-11-19 15:48:49

标签: corda

我使用下面的代码将IOU对象存储到数据库中。

    .mobile-card-container {
      display: block;
    }

    .card-background {
      background-color: #ffffff;
      border-radius: 6px;
      margin-top: 16px;
      height: 100px;
      -webkit-box-shadow: 0px 2px 4px 1px rgba(0, 51, 102, 0.1);
      -moz-box-shadow: 0px 2px 4px 1px rgba(0, 51, 102, 0.1);
      box-shadow: 0px 2px 4px 1px rgba(0, 51, 102, 0.1);
    }

我想知道是否可以在配置文件中定义持久性逻辑(类似于JPA using multiple database schemas),以便为多个对象/表轻松定义持久性逻辑。

1 个答案:

答案 0 :(得分:0)

确实可以在CorDapp中定义持久性逻辑。您希望保留的状态应该实现Queryable State接口,它将需要两种方法:supportedSchemas()和generateMappedObject()。

supportedSchemas方法将确定应将哪些Schema类传递给generateMappedObject方法。然后,generateMappedObject方法(包括其中的代码片段)将创建要持久保存的数据的表示形式。

   @Override
    public Iterable<MappedSchema> supportedSchemas() {
        return ImmutableList.of(new TokenSchemaV1());
    }
    @Override
    public PersistentState generateMappedObject(MappedSchema schema) {
        if (schema instanceof TokenSchemaV1) {
            return new TokenSchemaV1.PersistentToken(
                    this.getOwner().getName().toString(),
                    this.getIssuer().getName().toString(),
                    this.getAmount(),
                    this.linearId.getId(),
                    this.getListOfPersistentChildTokens()
            );
        } else {
            throw new IllegalArgumentException("Unrecognised schema $schema");
        }
    }

从GenerateMappedObject方法生成的表示形式将传递到ORM,并按照Schema中的JPA批注指定的方式持久化到数据库。有关Java中的模式架构示例,请参见下面的代码段。该片段还演示了如何为集合和子集合制作多个表。

/**
 * An IOUState schema.
 */

@CordaSerializable
public class TokenSchemaV1 extends MappedSchema {

    public TokenSchemaV1() {
        super(TokenSchema.class, 1, ImmutableList.of(PersistentToken.class, PersistentChildToken.class, PersistentGrandChildToken.class));
    }

    @Entity
    @Table(name = "token_states")
    public static class PersistentToken extends PersistentState {
        @Column(name = "owner") private final String owner;
        @Column(name = "issuer") private final String issuer;
        @Column(name = "amount") private final int amount;
        @Column(name = "linear_id") public final UUID linearId;
        @OneToMany(cascade = CascadeType.PERSIST)
        @JoinColumns({
                @JoinColumn(name = "output_index", referencedColumnName = "output_index"),
                @JoinColumn(name = "transaction_id", referencedColumnName = "transaction_id"),
        })
        private final List<PersistentChildToken> listOfPersistentChildTokens;

        public PersistentToken(String owner, String issuer, int amount, UUID linearId, List<PersistentChildToken> listOfPersistentChildTokens) {
            this.owner = owner;
            this.issuer = issuer;
            this.amount = amount;
            this.linearId = linearId;
            this.listOfPersistentChildTokens = listOfPersistentChildTokens;
        }

        // Default constructor required by hibernate.
        public PersistentToken() {
            this.owner = "";
            this.issuer = "";
            this.amount = 0;
            this.linearId = UUID.randomUUID();
            this.listOfPersistentChildTokens = null;
        }

        public String getOwner() {
            return owner;
        }

        public String getIssuer() {
            return issuer;
        }

        public int getAmount() {
            return amount;
        }

        public UUID getLinearId() {
            return linearId;
        }

        public List<PersistentChildToken> getChildTokens() { return listOfPersistentChildTokens; }
    }

    @Entity
    @CordaSerializable
    @Table(name = "token_child_states")
    public static class PersistentChildToken {
        @Id
        private final UUID Id;
        @Column(name = "owner")
        private final String owner;
        @Column(name = "issuer")
        private final String issuer;
        @Column(name = "amount")
        private final int amount;
        @Column(name = "child_proof")
        private final String childProof;
        @ManyToOne(targetEntity = PersistentToken.class)
        private final TokenState persistentToken;
        @OneToMany(cascade = CascadeType.PERSIST)
        @JoinColumn(name = "parent_linear_id", referencedColumnName = "Id")
        private final List<PersistentGrandChildToken> listOfPersistentGrandChildTokens;


        public PersistentChildToken(String owner, String issuer, int amount, List<PersistentGrandChildToken> listOfPersistentGrandChildTokens) {
            this.Id = UUID.randomUUID();
            this.owner = owner;
            this.issuer = issuer;
            this.amount = amount;
            this.persistentToken = null;
            this.childProof = "I am a child";
            this.listOfPersistentGrandChildTokens = listOfPersistentGrandChildTokens;
        }

        // Default constructor required by hibernate.
        public PersistentChildToken() {
            this.Id = UUID.randomUUID();
            this.owner = "";
            this.issuer = "";
            this.amount = 0;
            this.persistentToken = null;
            this.childProof = "I am a child";
            this.listOfPersistentGrandChildTokens = null;
        }

        public UUID getId() {
            return Id;
        }

        public String getOwner() {
            return owner;
        }

        public String getIssuer() {
            return issuer;
        }

        public int getAmount() {
            return amount;
        }

        public TokenState getPersistentToken() {
            return persistentToken;
        }



    }

    @Entity
    @CordaSerializable
    @Table(name = "token_grand_child_states")
    public static class PersistentGrandChildToken {
        @Id
        private final UUID Id;
        @Column(name = "owner")
        private final String owner;
        @Column(name = "issuer")
        private final String issuer;
        @Column(name = "amount")
        private final int amount;
        @Column(name = "child_proof")
        private final String childProof;
        @ManyToOne(targetEntity = PersistentChildToken.class)
        private final TokenSchemaV1.PersistentChildToken persistentChildToken;

        public PersistentGrandChildToken(String owner, String issuer, int amount) {
            this.Id = UUID.randomUUID();
            this.owner = owner;
            this.issuer = issuer;
            this.amount = amount;
            this.persistentChildToken = null;
            this.childProof = "I am a child";
        }

        // Default constructor required by hibernate.
        public PersistentGrandChildToken() {
            this.Id = UUID.randomUUID();
            this.owner = "";
            this.issuer = "";
            this.amount = 0;
            this.persistentChildToken = null;
            this.childProof = "I am a child";
        }

        public UUID getId() {
            return Id;
        }

        public String getOwner() {
            return owner;
        }

        public String getIssuer() {
            return issuer;
        }

        public int getAmount() {
            return amount;
        }

        public TokenSchemaV1.PersistentChildToken getPersistentChildToken() {
            return persistentChildToken;
        }
    }

}