简体   繁体   中英

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; 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

my entities are

RoleSettings.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

@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>{}

RoleSettingsService.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

@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

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

and my roleId is stored in database as type binary(16).

Check in your database data type of the id column. It has to be 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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