Corda persiste state using configuration file
I use below code to store IOU object to database.
return when (schema) {
is IOUSchemaV1 -> IOUSchemaV1.PersistentIOU(
this.lender.name.toString(),
this.borrower.name.toString(),
this.value,
this.externalRef,
this.linearId.id
)
I would like to know if it is possible to define the persistence logic in the configuration file (similar to JPA using multiple database schemas) so that I it easy to define persistence logic for multiple objects/tables.
corda
add a comment |
I use below code to store IOU object to database.
return when (schema) {
is IOUSchemaV1 -> IOUSchemaV1.PersistentIOU(
this.lender.name.toString(),
this.borrower.name.toString(),
this.value,
this.externalRef,
this.linearId.id
)
I would like to know if it is possible to define the persistence logic in the configuration file (similar to JPA using multiple database schemas) so that I it easy to define persistence logic for multiple objects/tables.
corda
add a comment |
I use below code to store IOU object to database.
return when (schema) {
is IOUSchemaV1 -> IOUSchemaV1.PersistentIOU(
this.lender.name.toString(),
this.borrower.name.toString(),
this.value,
this.externalRef,
this.linearId.id
)
I would like to know if it is possible to define the persistence logic in the configuration file (similar to JPA using multiple database schemas) so that I it easy to define persistence logic for multiple objects/tables.
corda
I use below code to store IOU object to database.
return when (schema) {
is IOUSchemaV1 -> IOUSchemaV1.PersistentIOU(
this.lender.name.toString(),
this.borrower.name.toString(),
this.value,
this.externalRef,
this.linearId.id
)
I would like to know if it is possible to define the persistence logic in the configuration file (similar to JPA using multiple database schemas) so that I it easy to define persistence logic for multiple objects/tables.
corda
corda
edited Nov 20 '18 at 9:27
Joel
10.1k11228
10.1k11228
asked Nov 19 '18 at 15:48
Raghav G
33
33
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It is indeed possible to define persistence logic inside a CorDapp. The state you wish to persist should implement the Queryable State interface which will require two methods: supportedSchemas() and generateMappedObject().
The supportedSchemas methods will determine which Schema classes should be passed to the generateMappedObject method. The generateMappedObject method (which you have included a snippet from) will then create a representation of the data to persist.
@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");
}
}
The representation generated from the generateMappedObject method is passed to the ORM and persisted to the database as specified by JPA annotations in the Schema. See the code snippet below for an example schema class in Java. The snippet also demonstrates how one might make multiple tables for collections and sub-collections.
/**
* 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;
}
}
}
Corda provides documentation on persistence as well docs.corda.net/api-persistence.html
– Austin Moothart
Nov 20 '18 at 21:42
Thanks for the answers. Just one doubt. Is it possible in corda to keep the table mapping in config file. Please refer docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/… for the configuration based entity mapping.
– Raghav G
Nov 22 '18 at 9:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378220%2fcorda-persiste-state-using-configuration-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is indeed possible to define persistence logic inside a CorDapp. The state you wish to persist should implement the Queryable State interface which will require two methods: supportedSchemas() and generateMappedObject().
The supportedSchemas methods will determine which Schema classes should be passed to the generateMappedObject method. The generateMappedObject method (which you have included a snippet from) will then create a representation of the data to persist.
@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");
}
}
The representation generated from the generateMappedObject method is passed to the ORM and persisted to the database as specified by JPA annotations in the Schema. See the code snippet below for an example schema class in Java. The snippet also demonstrates how one might make multiple tables for collections and sub-collections.
/**
* 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;
}
}
}
Corda provides documentation on persistence as well docs.corda.net/api-persistence.html
– Austin Moothart
Nov 20 '18 at 21:42
Thanks for the answers. Just one doubt. Is it possible in corda to keep the table mapping in config file. Please refer docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/… for the configuration based entity mapping.
– Raghav G
Nov 22 '18 at 9:24
add a comment |
It is indeed possible to define persistence logic inside a CorDapp. The state you wish to persist should implement the Queryable State interface which will require two methods: supportedSchemas() and generateMappedObject().
The supportedSchemas methods will determine which Schema classes should be passed to the generateMappedObject method. The generateMappedObject method (which you have included a snippet from) will then create a representation of the data to persist.
@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");
}
}
The representation generated from the generateMappedObject method is passed to the ORM and persisted to the database as specified by JPA annotations in the Schema. See the code snippet below for an example schema class in Java. The snippet also demonstrates how one might make multiple tables for collections and sub-collections.
/**
* 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;
}
}
}
Corda provides documentation on persistence as well docs.corda.net/api-persistence.html
– Austin Moothart
Nov 20 '18 at 21:42
Thanks for the answers. Just one doubt. Is it possible in corda to keep the table mapping in config file. Please refer docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/… for the configuration based entity mapping.
– Raghav G
Nov 22 '18 at 9:24
add a comment |
It is indeed possible to define persistence logic inside a CorDapp. The state you wish to persist should implement the Queryable State interface which will require two methods: supportedSchemas() and generateMappedObject().
The supportedSchemas methods will determine which Schema classes should be passed to the generateMappedObject method. The generateMappedObject method (which you have included a snippet from) will then create a representation of the data to persist.
@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");
}
}
The representation generated from the generateMappedObject method is passed to the ORM and persisted to the database as specified by JPA annotations in the Schema. See the code snippet below for an example schema class in Java. The snippet also demonstrates how one might make multiple tables for collections and sub-collections.
/**
* 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;
}
}
}
It is indeed possible to define persistence logic inside a CorDapp. The state you wish to persist should implement the Queryable State interface which will require two methods: supportedSchemas() and generateMappedObject().
The supportedSchemas methods will determine which Schema classes should be passed to the generateMappedObject method. The generateMappedObject method (which you have included a snippet from) will then create a representation of the data to persist.
@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");
}
}
The representation generated from the generateMappedObject method is passed to the ORM and persisted to the database as specified by JPA annotations in the Schema. See the code snippet below for an example schema class in Java. The snippet also demonstrates how one might make multiple tables for collections and sub-collections.
/**
* 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;
}
}
}
/**
* 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;
}
}
}
/**
* 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;
}
}
}
answered Nov 20 '18 at 14:26
Nicholas Rogers
233
233
Corda provides documentation on persistence as well docs.corda.net/api-persistence.html
– Austin Moothart
Nov 20 '18 at 21:42
Thanks for the answers. Just one doubt. Is it possible in corda to keep the table mapping in config file. Please refer docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/… for the configuration based entity mapping.
– Raghav G
Nov 22 '18 at 9:24
add a comment |
Corda provides documentation on persistence as well docs.corda.net/api-persistence.html
– Austin Moothart
Nov 20 '18 at 21:42
Thanks for the answers. Just one doubt. Is it possible in corda to keep the table mapping in config file. Please refer docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/… for the configuration based entity mapping.
– Raghav G
Nov 22 '18 at 9:24
Corda provides documentation on persistence as well docs.corda.net/api-persistence.html
– Austin Moothart
Nov 20 '18 at 21:42
Corda provides documentation on persistence as well docs.corda.net/api-persistence.html
– Austin Moothart
Nov 20 '18 at 21:42
Thanks for the answers. Just one doubt. Is it possible in corda to keep the table mapping in config file. Please refer docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/… for the configuration based entity mapping.
– Raghav G
Nov 22 '18 at 9:24
Thanks for the answers. Just one doubt. Is it possible in corda to keep the table mapping in config file. Please refer docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/… for the configuration based entity mapping.
– Raghav G
Nov 22 '18 at 9:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378220%2fcorda-persiste-state-using-configuration-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown