question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to implement hierarchical persistence as described in docs

See original GitHub issue

Simple Reference Project Documentation Version: 4.0 Corda Version: 4.0

In the docs a child token is tied to the parent token via:

 @Entity
 @CordaSerializable
 @Table(name = "child_data")
 class PersistentChildToken(
         @Id
         var Id: UUID = UUID.randomUUID(),

         // code for other columns

         @ManyToOne(targetEntity = PersistentParentToken::class)
         var persistentParentToken: TokenState

 ) : PersistentState()

but whenever i annotate a child with @Id i receive the following error:

net.corda.nodeapi.internal.persistence.HibernateConfigException: Could not create Hibernate configuration: net.corda.core.schemas.PersistentStateRef must not have @Id properties when used as an @EmbeddedId

i’ve looked through the source code and saw that a PersistentState is defined as the following:

@KeepForDJVM
@MappedSuperclass
@CordaSerializable
class PersistentState(@EmbeddedId override var stateRef: PersistentStateRef? = null) : DirectStatePersistable

so i guess i understand where the error is coming from, PersistentChildToken implements PersistentState which already has @EmbeddedId. but if i remove the @Id field i get the following error:

javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: null id generated for:class

it seems like i can’t use the @Id annotation because PersistentState is already annotated with EmbeddedId, but I also need the @Id annotation or else an id will not be properly generated.

there are further errors i have run into while trying to implement the hierarchal relationship which were eventually solved by adding a @OneToMany annotation above @JoinColumns. In the context of the docs it would be:

@OneToMany(
  cascade = [CascadeType.ALL],
  targetEntity = PersistentChildToken::class
)

the project i am attempting implement the hierarchical relationships is more complex than the simple reference project, but the core of the workflows, table creation, and relationship mapping is there.

any help implementing a one to many relationship would be greatly appreciated, thank you.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nargas-ritucommented, Feb 22, 2022

Internal ticket - CORDA-4212

1reaction
jrosmithcommented, Mar 10, 2021

@amolpednekar you are absolutely correct about the added overhead and i share your frustration with not being able to use Corda’s built in querying apis to handle the situation. unfortunately this approach was the best option available given the circumstances of our use case. if anyone else has found another solution to make this work using the querying api’s, i would be interested in hearing it

@SweeXordious some pseudo-code to get you started - note that this logic does not handle consumed/unconsumed states

 fun getParentAndChildren(parentLinearId: UUID)  {
   val nativeQuery = getNativeQuery(linearId)
   val session= serviceHub.jdbcSession()
   val rs = session.prepareStatement(nativeQuery).executeQuery()
   
   while (rs.next()) {
     // business logic
    }
 }
 
 fun getNativeQuery(linearId: UUID) : String {
    return """
      select 
        *
      from
        parent_table as p
      join
        child_table as c
      on
        c.parent_linear_id = '$linear_id'::uuid
      where
         p.linear_id = '$linear_id'::uuid
    """.trimIndent()
 }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to configure hierarchical multi-document management
The first part of implementing your own multi-document management implementation is creating a loading strategy. This consists of a number of callbacks that ......
Read more >
Using resource hierarchy for access control - IAM
Google Cloud resources are organized hierarchically, where the organization node is the root node in the hierarchy, the projects are the children of...
Read more >
Troubleshoot Google Docs, Sheets, Slides & Forms error ...
If you get an error message such as "Something went wrong. Reload" or "Unable to load file" preventing you from making edits on...
Read more >
Cloud Firestore Data model - Firebase
Documents; Collections; References; Hierarchical Data ... Every document in Cloud Firestore is uniquely identified by its location within the database.
Read more >
Entity Inheritance - The Java EE 6 Tutorial
You must use entity subclasses of the mapped superclass in EntityManager or Query ... the root class of the hierarchy with the annotation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found