I am a beginner with hyperledger. I want to retrieve all data from Asset of Car for do something in javascript file in hyperledger composer. I don't need to input id of asset car one by one. how can i do?
Model
participant Car identified by participant id{
o String id
o String name
}
asset CarAsset identified by id{
o String id
o String type
}
transaction CarTransaction{
--> UserAsset asset
//what can i do in here?
}
Transaction
function carTransaction(tx){
//what can i do in here?
}
If you want data of all the cars, you can write a query and use it in a transaction function
query.qry
query select Cars {
description: "Select all cars"
statement:
SELECT <your namespace>.Car
}
Take a look at this query tutorial it might help
Related
I am working with Grails 3.2.8. I would like to allow both options when generating an id. Is there a way to use assigned and falling back to sequence if unassigned? I tried getting the next id within a constructor and setting id there but running into issues. Any help/guidance would be most appreciated.
class Foo {
static mapping = {
id generator:'assigned'
}
}
vs
class Foo {
static mapping = {
id generator:'sequence'
}
}
Ive tried using mapping set to assigned and setting the id within the domain constructor of beforeValidate function. Neither are working for me. Examples below.
class Foo{
Foo(){
def id = Foo.find("from Foo order by id desc")
id = id ? id : 0
this.id = id
}
static mapping = {
id generator:'assigned'
}
}
class Foo{
def beforeValidate() {
def id = Foo.find("from Foo order by id desc")
id = id ? id : 0
this.id = id
}
static mapping = {
id generator:'assigned'
}
}
Thanks in advance for your help.
Is there a way to use assigned and falling back to sequence if
unassigned?
No, not directly anyway. You could use assigned and when you wanted to fall back on the sequence you could send a query to the database to retrieve the next sequence value and then assign it yourself. I think that is probably as close as you can probably get.
First of all, you need to understand HQL; in your examples you are returning Foo not id, so it's clearly wrong.
Second, you shouldn't depend on the table for next value of id, because it's not guaranteed to be correct -- you might face id is not unique kinda errors.
Third, you aren't incrementing the id, this way you will end up getting the same, most likely 0, every time.
Here is what you can do, use database sequence -- depends on the database.
"select id_seq.nextval from dual" // for Oracle
Finally, it's a very bad idea -- unless you are having a very good reason for it.
Is there a deterministic way to get timestamp in transaction function, similar to stub.GetTxTimestamp() that can be used in Go version of Fabric's chaincode.
Just sharing an example that works with basic-sample-network network:
In the model file (lib/org.acme.sample.cto) I extended SampleAsset definition any added new property called timestamp of type DateTime:
asset SampleAsset identified by assetId {
o String assetId
--> SampleParticipant owner
o String value
o DateTime timestamp
}
In the script file (lib/logic.js), the onSampleTransaction function to update SampleAsset's timestamp with current transaction's timestamp:
function onSampleTransaction(sampleTransaction) {
sampleTransaction.asset.value = sampleTransaction.newValue;
sampleTransaction.asset.timestamp = sampleTransaction.timestamp;
return getAssetRegistry('org.acme.sample.SampleAsset')
.then(function (assetRegistry) {
return assetRegistry.update(sampleTransaction.asset);
});
}
All transactions have a system property called timestamp, so you can use myTransaction.timestamp.
we cannot use the proto from the vendor folder ...
https://github.com/hyperledger-archives/fabric/issues/1832
I have two domain classes (simplified) which are related by a Many-to-Many relationship.
A Team can have many Players and a Player can belong to many Teams.
When i call the 'save' action of the Team controller :
A player gets saved in the table. (as expected)
A Team gets saved in the table. (as expected)
When i print team.players and player.teams, i see the correct output (See code below)
Nothing is saved in the relationship table (TEAM_PLAYERS). Why is this happening? Do i need to make entries in the join table myself? If this table is not getting updated, how am i seeing the correct output for point 3. ?
Team.groovy is :
class Team {
static hasMany = [players : Player]
String name;
String size;
}
Player.groovy is :
class Player {
static hasMany = [teams : Team]
static belongsTo = Team
String fullName;
String age;
}
TeamController.groovy is :
class TeamController {
def save() {
def player = new Player(fullName : "John Doe", age : "21").save()
def team = new Team(name : "LocalXI", size : "1").addToPlayers(player).save();
println "The players in the team are : " + team.players
println "The teams this player belongs to are : " + player.teams
}
}
Output for the above (when i call 'save' action ) :
The players in the team are : [John Doe]
The teams this player belongs to are : [LocalXI]
I am new to Grails and Groovy and have spent a lot of time trying to figure this out.
It should work. Take into consideration that the save method is transactional by default and it won't actually persist the data until the method is finished. Within the transaction everything looks correct, that is why the println output is correct.
If you are testing this controller via integration test, your data won't be persisted as the transaction is rolled back automatically for each test.
If you are testing this manually via browser make sure your are not using an in-memory DB in your current runtime environment.
Can someone help me with my situation. I have the following code.
class Employee {
Integer empId
String firstName
String lastName
static constraints = {
empId()
firstName()
lastName()
}
static mapping = {
id generator:'assigned', name:'empId'
version false
}
}
The code allows me to save employee through 'create' but gives the following error message
"Employee not found with id null" and also in the list, all the employees are listed but clicking on any Emp Id gives the same error. Please help. This is driving me nuts.
Rocky
Thanks for your reply. Like I mentioned I am able to save the empId in the database but get that message nonetheless and see the list with assigned ids (empId). The link points to employee/show with no number at end. However employee/show/22(empId) works fine. employee/edit/22 works too but update does not work.
I am not using any assigned sequence. Just some random integer. Maybe a better example would be to use SSN instead of empId.
Thank you once again.
You are a great help buddy. Appreciate your time and patience. I am not writing any special update or save (I am too new to dig too deep). Just using grails to generate-all. However, I did find a workaround. I changed the domain class to add variable id (Long) and added empId setter method to allocate the empId value to id. That did it. Here is my code.
class Employee {
Long id
Long empId
String firstName
String lastName
static constraints = {
empId()
firstName()
lastName()
}
static mapping = {
id generator:'assigned', name:'empId', column: 'emp_id'
version false
}
public void setEmpId(Long empId){
this.empId = empId
this.id = empId
}
}
Please feel free to suggest if you have a better way of doing that.
Regards
Rocky
If you are using the "assigned" sequence, then you have to assign the objects ids yourself before saving them. Otherwise your objects will be saved with a null or 'default 0' id. If you want GORM to assign an id for you, you need to use another type of generator, like "sequence" generator. It would be like:
id name: 'customId', generator: 'sequence', params: [sequence:'some_sequence']
More info on id generators here.
why the following Groovy snippet returns alternating
[Account: 2222 and 2222, Account: 1111 and 1111] or
[Account: 1111 and 1111, Account: 2222 and 2222]
if you run it multiple times within the Groovy Console ?
I thought the sort statement leads to an ALWAYS descending sort order of the list ???
class Account {
long number
String code
String toString() {return "Account: $number and $code"}
static mapping = {
sort number:"desc"
}
}
List items = []
items << new Account(number:1111,code:'1111')
items << new Account(number:2222,code:'2222')
println items.sort()
Thanks in advance
Dominik
You don't define an ordering among your Account instances. The mapping directive is only applicable to GORM mapped classes (IOW: "domain objects"), and will only be used when loading instances of your class from the database AFAIK.
However, you are appending the objects to a plain List, which does not know about GORM properties. In order to sort lists of Account instances reliably in such a context, you will have to specify an explicit ordering, for example:
class Account implements Comparable {
...
def int compareTo(rhs) {
long onum = rhs.number;
return (onum > number)? -1 : ((onum < number)? 1 : 0);
}
...
}
This article has more information about the topic. As to why Groovy sorts the list differently on multiple calls to list.sort: well, I have no idea...
Grails has two main default ways to sort:
Sort when you query:
def airports = Airport.list(sort:'name')
Put a default sorting method on that object:
class Airport {
…
static mapping = {
sort name:"desc"
}
}
The above is taken from the grails documentation.