简体   繁体   English

休眠中的 UUID 映射

[英]UUID Mapping in hibernate

I have mapped a table to my table and trying to add some values in it.我已将一个表映射到我的表并尝试在其中添加一些值。 but I am getting errors as below但我收到如下错误

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create, delete, read, role_id, update, id) values (_binary'ØN_WlAs—\\niÊnÙ' at line 1检查与您的 MySQL 服务器版本相对应的手册,以获取在“创建、删除、读取、role_id、更新、id”附近使用的正确语法(第 1 行的 _binary'ØN_WlAs—\\niÊnÙ')

my entities are我的实体是

RoleSettings.java角色设置.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;
}

RoleMaster.java角色大师.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 RoleSettingsRepository.java

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

RoleSettingsService.java角色设置服务.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;
}
}

RoleSettingsController.java角色设置控制器.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;
}
}

my json object我的 json 对象

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

and my roleId is stored in database as type binary(16).我的 roleId 以 binary(16) 类型存储在数据库中。

Check in your database data type of the id column.检查id列的数据库数据类型。 It has to be BINARY(16) .它必须是BINARY(16) And annotate your entity field as:并将您的实体字段注释为:

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

Note that you nned to add a column definition in this case.请注意,在这种情况下,您需要添加列定义。

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

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