imr*_*nk1 8 mysql grails groovy
我想使用UUID作为我的域对象的id.想法是uuid可以由客户端提供,如果不是,则将生成UUID.我有这样的定义:
class Person {
static mapping = {
id generator:'assigned'
}
String id
def getUUID ={
return java.util.UUID.randomUUID().toString();
}
transient beforeInsert = {
if ( id == null || id.equals(""))
id = getUUID();
}
}
Run Code Online (Sandbox Code Playgroud)
现在假设我删除了包含在java UUID或客户端提供的UUID中的破折号,我希望将其存储在MySQL数据库的二进制字段中.而且检索也有正确的格式发回.
我怎么能做到这一点?想更好的方法来做到这一点?
Grails 和 hibernate 通常以字符串形式处理 UUID。只需多做一点工作就可以使用二进制 UUID。声明id为 typeUUID并提供 hibernate 用户类型以将其序列化为字节数组。您还需要告诉 grails UUID 使用什么 SQL 类型。例如:
class Person {
static mapping = {
id generator:'assigned', type: UUIDUserType, sqlType: 'varbinary(16)'
}
UUID id
def beforeInsert = {
if (!id) {
id = UUID.randomUUID()
}
}
}
Run Code Online (Sandbox Code Playgroud)
UUID 的用户类型是:
import java.nio.ByteBuffer
import java.nio.LongBuffer
import java.sql.ResultSet
import java.sql.PreparedStatement
import java.sql.Types
import org.hibernate.usertype.UserType
public class UUIDUserType implements UserType {
int[] sqlTypes() { [Types.VARBINARY] as int [] }
Class returnedClass() { UUID }
Object nullSafeGet(ResultSet resultSet, String[] names, owner) {
byte[] value = resultSet.getBytes(names[0])
return value ? bytesToUuid(value) : null
}
void nullSafeSet(PreparedStatement statement, value, int index) {
if (value == null) {
statement.setNull(index, Types.VARBINARY)
} else {
statement.setBytes(index, uuidToBytes(value))
}
}
boolean equals(x, y) { x == y }
int hashCode(x) { x.hashCode() }
Object deepCopy(value) { value }
boolean isMutable() { false }
Serializable disassemble(value) { value }
Object assemble(Serializable cached, owner) { cached }
def replace(original, target, owner) { original }
static byte[] uuidToBytes(uuid) {
def bytes = new byte[16];
ByteBuffer.wrap(bytes).asLongBuffer().with {
put(0, uuid.mostSignificantBits)
put(1, uuid.leastSignificantBits)
}
bytes
}
static UUID bytesToUuid(bytes) {
ByteBuffer.wrap(bytes).asLongBuffer().with {
new UUID(get(0), get(1))
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3772 次 |
| 最近记录: |