Comments (4)
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.
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.
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.
@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)
- Dependencies not released HOT 3
- Conditionally required properties are not checked when they are not changed but their condition becomes true
- TransientChangesTrackerImpl#linksRemoved marks entity as modified even if there were actually no links removed
- Implement equals/hashCode for TransientEntityChange HOT 1
- Missing methods: XdQuery<T>.last(), XdQuery<T>.lastOrNull()
- Error when using XdEnumEntityType HOT 3
- TransientSession looses cancelling policy on upgrading initial read-only txn
- Cache entity wrappers inside XdModel.toXd() HOT 1
- One have to manually close Environment on closing TransientEntityStore
- Concurrent access to TransientEntityStoreImpl#listeners
- Make it possible to disable async listeners per enviroment
- Migration / versions of data definitions HOT 2
- DSL support for EntityIterable.findContaining(...)
- GraalVM/Spring Native Compatibility HOT 2
- UUID type support HOT 1
- xodus-dnq is incompatible with newer Java versions (tested on Java 16) HOT 2
- Make SingletonEntitiesCache store container aware
- Sample program fails with "java.lang.reflect.InaccessibleObjectException"
- java.lang.ClassCastException: class kotlinx.dnq.util.FakeEntity cannot be cast to class jetbrains.exodus.database.TransientEntity HOT 1
- Design of the storage engine
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xodus-dnq.