Code Monkey home page Code Monkey logo

Comments (4)

sedovalx avatar sedovalx commented on June 20, 2024

EntityId is a low level class. Why do you need to search by it? Usually, you have an identity field in a base class of your xd-hierarchy:

abstract class XdIdEntityType<XD : XdIdEntity> : XdNaturalEntityType<XD>(ThreadLocalStoreContainer) {
    fun findById(id: Long): XD? {
        return all().firstOrNull(XdIdEntity::id eq id)
    }
}

abstract class XdIdEntity(entity: Entity) : XdEntity(entity) {
    companion object : XdIdEntityType<XdIdEntity>()
    
    val id by xdRequiredLongProp(unique = true) { min(0) }
}

And there is a kind of agreement that all operations over xd-entities that involve data reads or writes should be done in a transactional block. So, typically, there is no need to check the current transaction in xd-methods.
Anyway, you are on a right way if you think you need to search by an EntityId. Here is how I would do it:

val store: TransientEntityStore = TODO()
val entityId: String = TODO()
        
store.transactional { txn ->
    txn.getEntity(txn.toEntityId(entityId)).toXd<XdMyEntity>()
}

from xodus-dnq.

jansorg avatar jansorg commented on June 20, 2024

Thanks @sedovalx ,
I couldn't find a way to have an automatically generated UUID in my entities, so I used the internal id (as suggest at https://github.com/JetBrains/xodus/wiki/Entity-Stores#entities at the end).

I'm now using a halfway automatic solution using UUIDs and a find operation like you suggested.

from xodus-dnq.

sedovalx avatar sedovalx commented on June 20, 2024

You can use UUID.randomUUID().toString() in a constructor of your xd-entities:

abstract class XdIdEntityType<XD : XdIdEntity> : XdNaturalEntityType<XD>(ThreadLocalStoreContainer) {
    fun findById(id: String): XD? {
        return all().firstOrNull(XdIdEntity::id eq id)
    }
}

abstract class XdIdEntity(entity: Entity) : XdEntity(entity) {
    companion object : XdIdEntityType<XdIdEntity>()

    var id by xdRequiredStringProp(unique = true, trimmed = true)

    override fun constructor() {
        this.id = UUID.randomUUID().toString()
    }

    override fun beforeFlush() {
        if (!isNew && hasChanges(XdIdEntity::id)) {
            throw DataConstraintsException("It is not allowed to change ID after construction")
        }
    }
}

class XdMyEntity(entity: Entity) : XdIdEntity(entity) {
    companion object : XdIdEntityType<XdMyEntity>() {
        fun new(value: Int) = new {
            this.value = value
        }
    }

    override fun constructor() {
        super.constructor()
        this.defaultProp = "default value"
    }

    var value by xdIntProp()
    var defaultProp by xdStringProp()
}

And then val xd = XdMyEntity.new(42).

from xodus-dnq.

lehvolk avatar lehvolk commented on June 20, 2024

@sedovalx EntityId is a reliable database identity which can be used as unique entity identity across single database. At the moment we have only one implementation of EntityId for which toString will return string identity like typeId-entityLocalId. Backward API is not supported in xodus-dnq at the moment thus you should make a low-level call tx.getEntity(tx.toEntityId(entityStringId)).toXd().

I think we should definitely support converting EntityId to String and back and type safe search by id across database.

from xodus-dnq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.