我是新来的springboot,当试图使用我设置的userresponsitory时,我得到了这个错误"java.lang.IllegalArgumentException: Not a managed type: class com.smart.entities.User"我已经尝试了以前提供的解决方案,但没有一个是有效的。
我的控制器
package com.smart.controller;
@Controller
public class HomeController {
@Autowired
private UserRepository userRepository;
// handler for registering user
@RequestMapping(value = "/do_register", method = RequestMethod.POST)
public String registerUser(@ModelAttribute("user") User user,
@RequestParam(value = "agreement", defaultValue = "false") boolean agreement, Model model,
HttpSession session) {
try {
if (!agreement) {
throw new Exception("You have not agreed terms and conditions");
}
user.setRole("ROLE_USER");
user.setEnabled(true);
user.setImageUrl("default.png");
this.userRepository.save(user);
model.addAttribute("user", new User());
session.setAttribute("message", new Message("Successfully Registered!! ", "alert - success"));
System.out.println("Agreement " + agreement);
System.out.println("User " + user);
return "signup";
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("user", user);
session.setAttribute("message", new Message("SomeThing went wrong " + e.getMessage(), "alert - danger"));
return "signup";
}
}
}
我的存储库
package com.smart.dao;
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
用户类
package com.smart.entities;
@Entity
@Table(name="USER")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@Column(unique = true)
private String email;
private String password;
private String role;
private Boolean enabled;
private String imageUrl;
@Column(length = 500)
private String about;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
private List<Contact> contacts = new ArrayList<>();
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
}
我试过像ComponentScan(base.package.*)这样的注释,
@EnableJpaRepositories
Spring Boot扫描包结构中具有@SpringBootApplication
注释的类所在的同一层及以下的所有类。
为了避免问题和减少配置,请确保这样做。
在您的情况下,@SpringBootApplication
注释类必须在包com.smart