【发布时间】:2015-07-30 13:40:37
【问题描述】:
我在使用 EntityManager 从数据库中获取数据时遇到问题 这是我的用户实体
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private Integer id;
@Column(name = "username", length = 20, nullable = false)
@JsonProperty
private String username;
@Column(name = "password", nullable = false, unique = true)
@JsonProperty
private String password;
@Column(name = "enabled", nullable = false)
@JsonProperty
private boolean enabled;
@Column(name = "email", nullable = false, unique = true)
@JsonProperty
private String email;
@OneToMany(mappedBy = "user", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private Set<UserRole> userRoles;
//getters and setters
我尝试使用用户名搜索用户:
public User findByUserName(String username){
return entityManager.find(User.class, username);
}
但是有错误
Provided id of the wrong type for class project.model.User. Expected: class java.lang.Integer, got class java.lang.String
什么是正确的使用方法? 以及如何检查表中的用户名 uniuqe?
【问题讨论】: