• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I include multiple UUIDs in a single variable?  RSS feed

 
jason thompson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My application depends on eight default user roles (or account types). They can be renamed or disabled, but should not be deleted. I want to protect them from deletion using their uuid value from the db. How can I pass their uuids into a single variable so I can use that single variable in my conditional statement? If I should use an array, can you give me an example of how I might do this?


Thanks!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A variable can hold one thing, but that one thing can be pretty complicated. You may want to create your own class that holds the data, and that has methods that do what you need. you could then do something like:




so the "someMethod" would clearly return a boolean, based on whatever your needs are - which we don't know.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jason thompson wrote:How can I pass their uuids into a single variable so I can use that single variable in my conditional statement?

1. Why do you think you need a single variable?
2. Have you heard of enums? Or indeed HashSets? It seems to me that either one (or maybe even a combination of the two) would probably serve your purposes just fine.

It sounds to me like you're leaping to an implementation before you've worked out WHAT it is you want to do - and that's not generally a good way to go.

I'm also a bit worried that you're talking about UUIDs, because that doesn't really jive with a database ID.

HIH

Winston
 
jason thompson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am using uuid values. So a better question is should I used uuids to protect default user roles that the application depends on or should I add another attribute such as isDefault = t or f?

The default user roles (and their uuids) are statically entered on build. New user accounts with one of the 8 roles get their uuids generated. Hard coding uuids in the application doesn't seem like the right thing to do, but not sure adding another attribute is the best way to prevent the default roles from being deleted.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jason thompson wrote:Yes, I am using uuid values.

Really? They're enormous (128 bits, as I recall), so I would suggest that
(a) They're not going to dovetail well with a true database ID, which means that you may lose some of the things that they might provide (such as sequencing).
(b) The only time I've ever used them, there was only one way of "generating" a UUID automatically, and that was a time-based one, which provides no guarantees of uniqueness across JVM sessions (although the likelihood of a collision is extremely low).
Are you sure you didn't mean a UID?

So a better question is should I used uuids to protect default user roles that the application depends on or should I add another attribute such as isDefault = t or f?

Simple answer: Dunno.

To be honest, I'm not quite sure what a "role" has to do with a User, unless you're using the same routine to generate ALL your IDs. To me, it's a "HAS-A" relationship, not an "IS-A" one, and if I was designing a database for it, the two things would be completely separate.

However, I have no idea whether you're dealing with a legacy design or not. If so, and it equates "users" with "roles", then I hate to say, but I pity you.

Winston
 
jason thompson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The uuid's are not too long actually, for example '733217e3-850f-4f2d-9c0d-d33eceb7c86d' is an id generated from postgres uuid-ossp. I was originally going to use these values in a conditional statement, for example:



A user role is like a system account (i.e. user, power user, read only user, sys admin, etc.).

And yes, I inherited this application so I have to work within the already existing design.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jason thompson wrote:And yes, I inherited this application so I have to work within the already existing design.

Ah. I thought that might be it.

OK, so you have a database that stores roles as "users" and uses UUIDs as keys (which presumably you can read into your Java programs as a UUID object).
<horrible-retching-sound/>

Then I go back to my original statement: If you want a Java solution, you have the two I suggested earlier:
  • A HashSet - actually a HashSet<UUID>.
  • An enum that associates a named "role" with a UUID.

  • The advantage of the first on it's own is that if you're not interested in naming roles for your Java program, you could simply store the UUIDs that mustn't be updated in a config file somewhere, and then load them into a Set at start-up; an enum would require you to recompile for any update.

    The advantage of an enum (or a HashMap<String, UUID>, which could also be populated via a properties file) is that it's likely to make your "role" code more readable, viz (for a HashMap):which you'd then call with:but I'm afraid there's no "magic bullet" for this sort of stuff. It'll come down to what feels best for you.

    HIH

    Winston
     
    Junilu Lacar
    Sheriff
    Posts: 11479
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    IMO this requirement to not be able to delete one of those default roles from the database is a data integrity concern and thus, should be handled at the database level with a table trigger/constraint. Any attempt to delete the specific rows would fail, regardless of how that attempt is made. If you control that in your Java code then that leaves open a way to circumvent the rule. And I would use that isDefault column in the DB to avoid hard coding specific values in the db constraint or the Java code.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!