java - 将 UUID 与 Spring 引导数据一起使用
问题描述
我在我的应用程序中使用 UUID 作为主键当我通过 id 查找数据时遇到问题给我异常错误。 这是笔记类
package com.example.demo.model; import java.util.Date; import java.util.UUID; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import org.hibernate.annotations.GenericGenerator; import org.springframework.boot.autoconfigure.AutoConfigurationPackage; @AutoConfigurationPackage @Entity public class Note { @Id @GeneratedValue( strategy = GenerationType.AUTO,generator = "pg-uuid") @GenericGenerator(name = "pg-uuid",strategy = "uuid2") private UUID id; private String title; private String text; @ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.MERGE) private Notebook notebook; private Date lastModifiedOn; public UUID getId() { return id; } public void setId(UUID id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Notebook getNotebook() { return notebook; } public void setNotebook(Notebook notebook) { this.notebook = notebook; } public Date getLastModifiedOn() { return lastModifiedOn; } public void setLastModifiedOn(Date lastModifiedOn) { this.lastModifiedOn = lastModifiedOn; }
这是 NoteController
package com.example.demo.controller;
import java.text.ParseException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.MyMapper;
import com.example.demo.Repository.NoteBookRepository;
import com.example.demo.Repository.NoteRepository;
import com.example.demo.model.Note;
import com.example.demo.model.Notebook;
import com.example.demo.view.NoteViewModle;
@RestController
@RequestMapping("/api/note")
@CrossOrigin
public class NoteController {
@Autowired
NoteRepository noteRepository;
@Autowired
NoteBookRepository notebookRepository;
@Autowired
MyMapper maper;
@GetMapping("/all")
public List<NoteViewModle> getAllNote(){
List<Note> list = this.noteRepository.findAll();
List<NoteViewModle> noteViewModles = (List<NoteViewModle>) list.stream().map(note ->maper.convertToNodeViewModel(note)).collect(Collectors.toList());
return noteViewModles;
}
@GetMapping("/byId/{id}")
public NoteViewModle getNotreById(@PathVariable("id") UUID id) {
System.out.println(id);
Note note = this.noteRepository.findById(id).orElse(null);
if(note == null)
{
System.out.println("In If");
throw new EntityNotFoundException();
}
NoteViewModle modle = maper.convertToNodeViewModel(note);
return modle;
}
@GetMapping("/byNoteBook/{notebookId}")
public List<Note> getNotesByNoteBook(@PathVariable("notebookId") String id)
{
return this.noteRepository.findAllByNotebook(this.notebookRepository.findById(UUID.fromString(id)).orElse(new Notebook()));
}
@PostMapping("/add")
public Note save(@RequestBody NoteViewModle modle)
{
Note note = maper.convertToNodeEntity(modle);
return this.noteRepository.save(note);
}
@DeleteMapping("/deltebyId/{id}")
public void deleteNoteById(@PathVariable("id") String id) {
this.noteRepository.deleteById(UUID.fromString(id));
}
}
这是映射 json 数据的 MyMapper
package com.example.demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.demo.Repository.NoteBookRepository;
import com.example.demo.model.Note;
import com.example.demo.model.Notebook;
import com.example.demo.view.NoteViewModle;
import com.example.demo.view.NotebookViewModel;
@Component
public class MyMapper {
@Autowired
NoteBookRepository bookRepository;
public NoteViewModle convertToNodeViewModel(Note entity) {
NoteViewModle modle = new NoteViewModle();
modle.setId(entity.getId().toString());
modle.setTitle(entity.getTitle());
modle.setText(entity.getText());
modle.setNoteBookId(entity.getNotebook().getId().toString());
modle.setData(entity.getLastModifiedOn().toString());
return modle;
}
public Note convertToNodeEntity(NoteViewModle viewModle) {
Note note = new Note();
note.setTitle(viewModle.getTitle());
note.setText(viewModle.getText());
//note.setLastModifiedOn(new SimpleDateFormat("dd/MM/yyyy").parse(viewModle.getData()));
//Notebook notebook = bookRepository.findById(UUID.fromString(viewModle.getId())).orElse(new Notebook());
return note;
}
public NotebookViewModel convertToNotebookViewModel(Notebook entity)
{
NotebookViewModel viewModel = new NotebookViewModel();
viewModel.setId(entity.getId().toString());
viewModel.setName(entity.getName());
viewModel.setNumOfNote(entity.getNotes().size());
return viewModel;
}
public Notebook convertToNotebookEntity(NotebookViewModel model) {
Notebook notebook = new Notebook();
notebook.setId(UUID.fromString(model.getId()));
notebook.setName(model.getName());
return notebook;
}
}
这是存储库类
package com.example.demo.Repository;
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.model.Note;
import com.example.demo.model.Notebook;
@Repository
public interface NoteRepository extends JpaRepository<Note, UUID> {
List<Note> findAllByNotebook(Notebook notebook);
}
这是错误出现org.springframework.dao.EmptyResultDataAccessException: No class com.example.demo.model.Notebook entity with id 7e83b195-eb81-4752-a6fa-a5206c9eb1c6 存在!
解决方案
推荐阅读
- r - 如何在闪亮的 r 中的 bs4 仪表板中从一个选项卡面板转到另一个选项卡面板
- python - 在 pandas 中查找包含正确设置值的列的行
- reportportal - gradle 多项目设置中的 Reportportal 属性文件
- android - java.lang.RuntimeException:清单合并失败,出现多个错误,
- go - 在 Go 中为 ID 使用新类型的包装器是惯用的吗?
- java - 从单个 Java 流中提取多个值
- python - 如何在 Windows 中将 python 3.7 更改为 3.6
- python - 通过 pymongo 从 mongodb 获取范围内的数据条目
- css - 计算 div 的高度并将该数字放在 ::after 伪元素内容中
- python - 将双引号写入 Csv Python CSV 模块时出错