繁体   English   中英

休眠中的 UUID 映射

[英]UUID Mapping in hibernate

我已将一个表映射到我的表并尝试在其中添加一些值。 但我收到如下错误

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你的SQL语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在“创建、删除、读取、role_id、更新、id”附近使用的正确语法(第 1 行的 _binary'ØN_WlAs—\\niÊnÙ')

我的实体是

角色设置.java

@Entity @Table(name = "role_settings")
@Getter @Setter @Data
public class RoleSettings implements Serializable {

private static final long serialVersionUID = 8862104773442047690L;

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

@ManyToOne
@JoinColumn(name = "role_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "role_settings_iam_role_FK"))
private RoleMaster roleId;
}

角色大师.java

@Entity @Table(name = "role")
@Getter @Setter @Data
public class RoleMaster implements Serializable {

private static final long serialVersionUID = 1792968151371176640L;

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

@Column(name = "name", nullable = false, length = 255)
private String name;
}

RoleSettingsRepository.java

public interface RoleSettingsRepository extends JpaRepository<RoleSettings, UUID>{}

角色设置服务.java

@Service
Class RoleSettingsService {
@Autowired
private RoleSettingsRepository roleSettingsRepository;
public BaseDTO create(RoleSettings roleSettings) {
    BaseDTO response = new BaseDTO();
    RoleSettings newRoleSettings = new RoleSettings();

    try {
        newRoleSettings.setRoleId(roleSettings.getRoleId());
        newRoleSettings.setAppAccessId(roleSettings.getAppAccessId());
        newRoleSettings.setCreate(roleSettings.getCreate());
        newRoleSettings.setUpdate(roleSettings.getUpdate());
        newRoleSettings.setRead(roleSettings.getRead());
        newRoleSettings.setDelete(roleSettings.getDelete());
        roleSettingsRepository.save(newRoleSettings);
        response.setStatusCode(200);
    } catch (Exception e) {
    }
    return response;
}
}

角色设置控制器.java

@RestController
@RequestMapping("/v1/rolesettings")
public class RoleSettingsController {

@Autowired
private RoleSettingsService roleSettingsService;

@PostMapping("/post")
public BaseDTO create(@RequestBody RoleSettings roleSettings) {
    BaseDTO response = roleSettingsService.create(roleSettings);
    return response;
}
}

我的 json 对象

{ "roleId" :{"id":  "b2e64c82-ab75-41d3-bb10-e9150f314807"} }

我的 roleId 以 binary(16) 类型存储在数据库中。

检查id列的数据库数据类型。 它必须是BINARY(16) 并将您的实体字段注释为:

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
@Column(columnDefinition = "BINARY(16)")
private UUID id;

请注意,在这种情况下,您需要添加列定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM