objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

String ID aliases

Open greenrobot opened this issue 8 years ago • 62 comments

Offer some convenience to use strings in an ID fashion.

greenrobot avatar Sep 04 '17 08:09 greenrobot

I really hope this can be supported soon, love to try it out but having to use long id is quite troublesome

thomasdao avatar Oct 20 '17 04:10 thomasdao

Would be really, really appreciated if this was added!

wirusw avatar Oct 28 '17 15:10 wirusw

Any ETA? Month or week? I need to know this before public app release ;)

Mistic92 avatar Nov 25 '17 16:11 Mistic92

Hi guys,

It will great to support the string ids like other ORMs.

In the current situation if we get entities from the server that already exist in the DB then we don't know about it until making the call to the DB and finding these long ids and then copy them... (not a nice code)

vadimh77 avatar Dec 20 '17 08:12 vadimh77

any news about when will this be implemented?

silviahisham avatar Jan 09 '18 09:01 silviahisham

Of course there are some disadvantage in performance when string is used for primary key. But out of doubt such possibility should be. It's personal responsibility for everyone to use string as id or not. It's good if we have a choice what is more important performance or implementation speed. I wasn't able to use Room from google because it hasn't relations between entities. They explained that all relations should be handled on server side. It's true and it's best practice. But what if I'm not able to change server side because it's third part service. I'm pretty sure that some amount of people can't use ObjectBox because it can't handle string as id. Looking forward!

motornyimaksym avatar Jan 18 '18 08:01 motornyimaksym

In the meantime, has anyone come up with a workaround for this? At the moment, all I can think of is to persist a separate id for MongoDB objects

EDIT Looks like this is the recommended workaround from the docs:

If your application requires other ID types (such as a string UID given by a server), you can model them as standard properties and use queries to look up entities by your application specific ID.

@Entity public class User { @Id private long id; @Index private String serverUid; }

obianachebe avatar Feb 25 '18 22:02 obianachebe

@obianachebe My workaround in kotlin: fun String.toId(): Long = this.hashCode().toLong()

motornyimaksym avatar Feb 27 '18 13:02 motornyimaksym

@Yazon2006 Dangerous thing to do. Hash collisions are quite likely.

greenrobot avatar Feb 27 '18 14:02 greenrobot

I was going to write the same. But for now, there is no other easy solution. Separate field for storing String id make a lot of mess and each time you need to check if it's not already in db.

Mistic92 avatar Feb 27 '18 14:02 Mistic92

@greenrobot Of course I'm familiar with hash collisions. But in my case it's not a problem 'cause I have exhaustive amount of data in my db and there are no hash collisions. And also I hope that the feature with string id in objectBox will be available until release of my application.

motornyimaksym avatar Feb 27 '18 14:02 motornyimaksym

+1 for String Ids

james-ff avatar Mar 10 '18 19:03 james-ff

expectation

longforus avatar Apr 10 '18 10:04 longforus

+1 And my current workaround: (Yeah, negative Id is still id, and work fine :) )

 fun longHash(string: String): Long {
        var h = 98764321261L
        val l = string.length
        val chars = string.toCharArray()

        for (i in 0 until l) {
            h = 31 * h + chars[i].toLong()
        }
        return h
    }

sandsaber avatar Apr 24 '18 08:04 sandsaber

Can you please provide any time estimation for the String type ids implementation?

vadimh77 avatar Jun 14 '18 12:06 vadimh77

Is there a development message about this feature?

a2open avatar Jun 23 '18 13:06 a2open

+1

ugurcany avatar Jul 13 '18 13:07 ugurcany

+1

zhenxun815 avatar Jul 23 '18 07:07 zhenxun815

+1

alijaza avatar Jul 24 '18 22:07 alijaza

Btw, a good in-between state is to use @Unique as introduced with ObjectBox 2.0.

greenrobot avatar Aug 01 '18 10:08 greenrobot

@greenrobot
That's so Good

linguokun1 avatar Aug 02 '18 03:08 linguokun1

I can see @Unique is useful but it's not a replacement for String id. Let say if we want to sync the objects with string id from server, we would first need to query for existing object in local db then save or update the object in local db. I don't think it's performant approach, unless we load all the objects in db into memory so we can avoid the query for existing object.

thomasdao avatar Aug 02 '18 04:08 thomasdao

@thomasdao +111111 Unfortunately we have to use server-Uniq-identifiers, this is the only way for us, Please pretty please release this feature immediately. God bless greenrobot :)

avcial avatar Aug 02 '18 11:08 avcial

@greenrobot when I want to update the same unique String vin property's entity, it throw : "Caused by: io.objectbox.exception.UniqueViolationException: Unique constraint for VehicleConfiguration.vin would be violated by putting entity with ID 2 because same property value already exists in entity with ID 1"

linguokun1 avatar Aug 03 '18 04:08 linguokun1

@greenrobot So I must remove the entity with unique vin property and put again rather than can cover update it?

linguokun1 avatar Aug 03 '18 04:08 linguokun1

@linguokun1 They should at least put the already exist entity into exception instance.

avcial avatar Aug 03 '18 06:08 avcial

@linguokun1 See https://github.com/objectbox/objectbox-java/issues/509 for current discussion.

greenrobot avatar Aug 07 '18 15:08 greenrobot

+1

mesterj avatar Aug 08 '18 13:08 mesterj

any news?

avcial avatar Aug 27 '18 14:08 avcial

still waiting for..

avcial avatar Sep 12 '18 08:09 avcial