I am trying to create a user model with a CrudRepository:
我正在尝试使用CrudRepository创建用户模型:
@Entity
public class User {
@Id
@GeneratedValue
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
public interface UserRepository extends CrudRepository<User, String> {
}
However I got an 500 error every time I call findOne():
但是每次调用findOne()时我都会收到500错误:
@Controller
public class UserController {
@Autowired
private UserRepository users;
@Override
@RequestMapping(value="/register", method=RequestMethod.POST)
public @ResponseBody User register(@RequestBody User userToRegister) {
String username = userToRegister.getUsername();
User user = users.findOne(id);
if (user != null) {
return null;
}
User registeredUser = users.save(userToRegister);
return registeredUser;
}
}
However if I just switch to an long type id instead of username itself then everything works. I think it's common to use string as id. So how to make this work?
但是,如果我只是切换到长类型ID而不是用户名本身,那么一切正常。我认为使用string作为id是很常见的。那么如何使这项工作?
I use the embedded hsql database. I didn't wrote any sql code.
我使用嵌入式hsql数据库。我没有写任何sql代码。
1 个解决方案
#1
The problem is that String username;
is annotated with both @Id
and @GeneratedValue
. @Id
means that is should be a primary key, fine it can be a String. But @GeneratedValue
means that you want the system to automatically generate a new key when you create a new record. That's easy when the primary key is integer, all databases have a notion of sequence (even if the syntax is not always the same). But if you want String automatically generated keys, you will have do define your own custom generator.
问题是String username;用@Id和@GeneratedValue注释。 @Id表示应该是主键,可以是String。但是@GeneratedValue意味着您希望系统在创建新记录时自动生成新密钥。当主键是整数时,这很容易,所有数据库都有一个序列概念(即使语法并不总是相同)。但是如果你想要String自动生成的键,你将定义自己的自定义生成器。
Or if you have no reason for the @GeneratedValue
annotation, simply remove it as suggested by Bohuslav Burghardt
或者,如果您没有@GeneratedValue注释的原因,只需按照Bohuslav Burghardt的建议将其删除
#1
The problem is that String username;
is annotated with both @Id
and @GeneratedValue
. @Id
means that is should be a primary key, fine it can be a String. But @GeneratedValue
means that you want the system to automatically generate a new key when you create a new record. That's easy when the primary key is integer, all databases have a notion of sequence (even if the syntax is not always the same). But if you want String automatically generated keys, you will have do define your own custom generator.
问题是String username;用@Id和@GeneratedValue注释。 @Id表示应该是主键,可以是String。但是@GeneratedValue意味着您希望系统在创建新记录时自动生成新密钥。当主键是整数时,这很容易,所有数据库都有一个序列概念(即使语法并不总是相同)。但是如果你想要String自动生成的键,你将定义自己的自定义生成器。
Or if you have no reason for the @GeneratedValue
annotation, simply remove it as suggested by Bohuslav Burghardt
或者,如果您没有@GeneratedValue注释的原因,只需按照Bohuslav Burghardt的建议将其删除