Solve the problem that the conversion of sql server save object string into uniqueidentifier fails

1. Background introduction

The web application uses the ssh framework, and the database uses the sql server2014 version.

2. Problem:

According to customer requirements, the data type of the ID column must be uniqueidentifier. At the beginning, the ID of the entity class was designed to be java.lang.String; the growth method of the ID in the mapping file is uuid.hex

1
2
3
4
5
6
7
8
9
private java.lang.String id;

public java.lang.String getId(){
return id;
}

public void setId(java.lang.String id){
this.id=id;
}
1
2
3
<id name="id" column="ID" type="java.lang.String">
<generator class="uuid .hex"></generator>
</id> An
error was reported when saving: Failed to save the string uniqueidentifier.

3. Solution:

Modify the ID growth mode in the mapping file to "assigned" and assign values ​​in the program.

1
2
3
4
5
6
7
8
9
10
11
12
/ mapping file /

<id name="id" column="ID" type="java.lang.String">
<generator class="assigned"></generator>
</id>

/ Assignment code /

UUID uuid = java.util.UUID.randomUUID();
object.setId(uuid.toString);

this.save(object);
attached: statement to create a table

1
2
3
4
5
CREATE TABLE tableName
(
ID UNIQUEIDENTIFIER,
- omit other codes
);
Supplementary knowledge: understanding of uniqueidentifier data type in SQLServer

uniqueidentifier can be understood as a globally unique identifier (GUID), you can use the newid function to initialize the value, and convert the string constant into the following form ( https://blog.51cto.com/14830013/2549657, where each x is 0-9 Or a hexadecimal number in the range of af).

For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid uniqueidentifier value.

Comparison operators can be used with uniqueidentifier values. However, permutation is not achieved by comparing the bit patterns of two values. The only operations allowed on uniqueidentifier values ​​are comparisons (=, <>, <, >, <=, >=) and checking for NULL (IS NULL and IS NOT NULL).

No other arithmetic operators are allowed. All column constraints and attributes (except IDENTITY) are allowed to be used for the uniqueidentifier data type.

1
2
3
declare @myid uniqueidentifier
set @myid=newid()
print'Value of @myid is'+cast(@myid as varchar(255))
Each time you run the above program, it returns a different uniqueidentifier

The uniqueidentifier data type does not automatically generate a new ID for newly inserted rows like the IDENTITY property.

In order to get a new uniqueidentifier value, the table must have a DEFAULT clause that specifies the NEWID function, or an INSERT statement that uses the NEWID function:

1
2
3
4
5
6
7
CREATE TABLE MyUniqueTable
(UniqueColumn UNIQUEIDENTIFIER DEFAULT NEWID(),
Characters VARCHAR(10))
GO
INSERT INTO MyUniqueTable(Characters) VALUES ('abc')
INSERT INTO MyUniqueTable VALUES (NEWID(),'def')
GO The
above article to solve the problem of the failure of converting sql server saved object strings into uniqueidentifier is all the content shared by the editor. I hope to give you a reference, and I hope you can support the script house.

Guess you like

Origin blog.51cto.com/14308901/2550967