您好我正在关注CRUD tuts,我正在尝试在表中编写类。但是我被卡住了......
Classes.java
@Entity
@Table(name = "Classes")
public class Classes {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "class_id", unique = true, nullable = false)
private String classId;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "max_numb_students")
private int max_numb_students;
@Column(name = "min_numb_students")
private int min_numb_students;
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getMax_numb_students() {
return max_numb_students;
}
public void setMax_numb_students(int max_numb_students) {
this.max_numb_students = max_numb_students;
}
public int getMin_numb_students() {
return min_numb_students;
}
public void setMin_numb_students(int min_numb_students) {
this.min_numb_students = min_numb_students;
}
public Classes() {
}
public Classes(String classId, String name, String description, int max_numb_students, int min_numb_students) {
super();
this.classId = classId;
this.name = name;
this.description = description;
this.max_numb_students = max_numb_students;
this.min_numb_students = min_numb_students;
}
}
Controller.java
@Controller
@RequestMapping("/classesTable")
public class ClassesController {
@Autowired
private ClassesRepository classesRepository;
@RequestMapping(value="", method=RequestMethod.GET)
public String listClasses(Model model){
model.addAttribute("classes", classesRepository.findAll());
return "classesTable";
}
@RequestMapping(value = "/{classId}/delete", method = RequestMethod.GET)
public ModelAndView delete(@PathVariable String classId) {
System.out.println(classId);
classesRepository.delete(classId);
return new ModelAndView("redirect:/classesTable");
}
}
ClassesRepository.java
public interface ClassesRepository extends CrudRepository<Classes,String>{
}
我收到此错误
出现意外错误(type = Internal Server Error,status = 500)。 没有类id为{classId}的com.oggi.model.Classes实体!
答案 0 :(得分:0)
我创建了一个这样的项目并使用String id工作正常,唯一的区别是我使用JpaRepository而不是CrudRepo。使用long id是一个更好的解决方案,但是如果你需要String id,它也可以工作。
domain:DataCenters.java
a.click()
回购:DataCentersRepository
@Entity
public class DataCenters {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
private String name;
private String location;
public DataCenters() {
}
public DataCenters(String name, String location) {
this.name = name;
this.location = location;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "DataCenters{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", location='" + location + '\'' +
'}';
}
}
controller:DataCentersController
public interface DataCentersRepository extends JpaRepository<DataCenters,String> {
}
启动应用程序:MyJpademoApplication
@Controller
@RequestMapping("/datacenters")
public class DataCentersController {
private final DataCentersRepository dataCentersRepository;
@Autowired
public DataCentersController(DataCentersRepository dataCentersRepository){
this.dataCentersRepository=dataCentersRepository;
}
@RequestMapping(value="", method=RequestMethod.GET)
public String listClasses(Model model){
model.addAttribute("datacenters", dataCentersRepository.findAll());
return "datacenters";
}
@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
public ModelAndView delete(@PathVariable String id) {
System.out.println(id);
dataCentersRepository.delete(id);
return new ModelAndView("redirect:/datacenters");
}
}
application.properties
@SpringBootApplication
public class MyjpademoApplication {
private static final Logger logger = LoggerFactory.getLogger(MyjpademoApplication.class);
public static void main(String[] args) {
SpringApplication.run(MyjpademoApplication.class, args);
}
@Bean
public CommandLineRunner runner(ServerRepository serverRepository, DataCentersRepository dataCentersRepository){
return (args) -> {
logger.info("Creating Data Centers...");
dataCentersRepository.save(new DataCenters("StarFox","nowhere"));
dataCentersRepository.save(new DataCenters("Axelay","nowhere"));
dataCentersRepository.save(new DataCenters("NinjaGaiden","nowhere"));
dataCentersRepository.findAll().stream().forEach(System.out::println);
};}}
的pom.xml
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url:jdbc:h2:mem:~/test