How to create a UUID from a value - java

In my database, I have a uuid 67b26616-943e-49e0-a70c-8e9ddcba1baa. In my code, I want to query the database and want to pass the above uuid. How could I create a type UUID from a value? I saw the following function but I dont know which bits of my value goes where.
public UUID(long mostSigBits,
long leastSigBits)

You can create UUID this way:-
UUID uid = UUID.fromString("67b26616-943e-49e0-a70c-8e9ddcba1baa");
System.out.println("UUID value is: "+uid);

Related

String having xml kind of value, how can I fetch key to update missing value?

I have a long string value. Here some values are missing as you see verifyoName. I wanted to fetch this key and add value for this key and finally update the payload with values. Could you please give
me some idea how can I do it.
String msg=<aleri:MESSAGE
xmlns:aleri="http://www.aleri.com/2005/JS/aleri"><HEADER>
<OPCODE>Capture</OPCODE></HEADER><DATA><Operators Branch="21"
Profile="ABC" VerifyDateTime="" VerifyoName="" /></DATA></aleri:MESSAGE>

How to get an entity-related object correctly

I have approximately the following entity:
public class Article {
private String name;
private Long fileId;
}
As you can see, it has a field fileld that contains the id of the associated file, which is also an entity. However, the file does not know anything about the Article, so the only thing that connects them is the fileId field in the Article. Therefore, they must be explicitly linked so as not to get lost. Now to get a linked file, I have to make a separate query to the database for each Article. That is, if I want to get a list of 10 Articles, I need to make a request to the database 10 times and get the file by its id. This looks very inefficient. How can this be done better? I use jooq, so I can't use JPA, so I can't substitute a file object instead of the fileId field. Any ideas?
I'm going to make an assumption that your underlying tables are something like this:
create table file (
id bigint primary key
content blob
);
create table article (
name text,
file_id bigint references file
);
In case of which you can fetch all 10 files into memory using a single query like this:
Result<?> result =
ctx.select()
.from(ARTICLE)
.join(FILE).on(ARTICLE.FILE_ID.eq(FILE.ID))
.fetch();

create UUID from MostSignificantBits and LeastSignificantBits

As we know that we can get MostSignificantBits of UUID using method getMostSignificantBits() and LeastSignificantBits of UUID using method getLeastSignificantBits(). But , How to get the Original UUID(reverse process) if MostSignificantBits and LeastSignificantBits are known ?
you can use the UUID Constructor
UUID(long mostSigBits, long leastSigBits)
which constructs a new UUID using the specified data.

Generate Unique Employee code in String

I am implementing Employee Management System in Java. I want employee code to be auto generated on new employee registration and I should be in predefined format. I don't want to use auto incremented integer key as a employee ID. I am using MySQL Database and JDBC API to connect to MySQL. Please suggest me a solution.
If the length of the ID does not matter just use java.util.UUID to generate a universally unique identifier.
There can be many approaches to generate an employee Id.I am recommending following most commonly used methods.
1) Timestamp Based :- Use UUID from java.util.UUID to generate random ID's based on host and current time.
UUID uuid = UUID.randomUUID();
2) Custom Information Based
If you want an employeeId to be generated using Company Initials/Employee's Personal Information,
Use employee's username and date of birth
Take a namespace identifier(can be your organisation's name)
With the hash of unique identifier and username of employee,
String source = namespace + username + dateofbirth;
byte[] bytes = source.getBytes("UTF-8");
UUID uuid = UUID.nameUUIDFromBytes(bytes);
3) Use Company Initials :
Lets say your company initials are XYZ.Use a random number generator function to generate any 5 or 6 digit number.
Eg. XYZ47899

How to generate a random string for ID/Name of an Entity instead of a number?

I'm working with the low-level datastore API. I've created an entity like this:
Entity entity = new Entity("Error");
entity.setProperty("description", "foo");
In the datastore viewer, I ses this:
Key Write Ops ID/Name description
----------------------------------------------
ahN0c... 4 259 foo
So the ID/Name field will be generated for me automatically since I'm not supplying anything in the Entity constructor. It generates an "ID" instead of a "Name", which is a number rather than an opaque string (like the "Key" value).
Is there a way to have the datastore generate a random "Name" instead of an "ID" for the Entity's "ID/Name" field?
I ask because if I share this ID with third parties, they could start to figure out roughly how many Error instances I have in my system. I'd rather give them an opaque string for the lookup ID, similar to what's in the auto-generated "Key" field. But I don't see a way to do this.
Thanks
For a similar task I used UUID to create a random string.
String uuid = UUID.randomUUID().toString();
You can use com.google.appengine.api.datastore.KeyFactory, combining the answer from #Devolus, it would look like
final Key key = KeyFactory.createKey("Error", UUID.randomUUID().toString());
final Entity e = new Entity(key);
You could even pass around the String representation of your Entitie's key via KeyFactory.keyToString(key) , may be after an encrypting depending on your security needs.

Categories

Resources