当我试图坚持它时,我收到一条错误,说我的实体已经分离了。调试时我可以看到我尝试保存的对象有一个ID。我猜这可能与我在JPA中的注释有关但我无法弄清楚这个问题的原因是什么。
带有导致错误的方法的类(doStuff()的最后一行负责):
@RestController
@RequestMapping("/test")
public class ConcreteMyController implements MyController {
private final CategoryService categoryService;
private final OrmFactory ormFactory;
private final SongService songService;
private final CategoryForSongService categoryForSongService;
@Autowired
public ConcreteMyController(CategoryService categoryService, OrmFactory ormFactory, SongService songService, CategoryForSongService categoryForSongService) {
this.categoryService = categoryService;
this.ormFactory = ormFactory;
this.songService = songService;
this.categoryForSongService = categoryForSongService;
}
@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
@Override
public void doStuff() {
String title = "BestSongInTheWorld";
String popCategoryName = "pop";
String rockCategoryName = "rock";
String jazzCategoryName = "jazz";
Song song = this.ormFactory.createSong(title, "3:14");
this.songService.save(song);
Set<Category> categories = Sets.newHashSet(new Category(popCategoryName), new Category(rockCategoryName), new Category(jazzCategoryName));
this.categoryService.save(categories);
Song retrievedSong = songService.get(title);
Set<Category> retrievedCategories = categoryService.get(Sets.newHashSet(popCategoryName, rockCategoryName));
CategoryForSong categoryForSong = new CategoryForSong(retrievedSong.getTitle(), retrievedCategories);
this.categoryForSongService.save(categoryForSong);
}
}
实体:
@Entity
@Table
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "name")
private String name;
@Id
@GeneratedValue
private Long id;
}
@Entity
@Table
public class Song implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "duration")
private String duration;
@Column(name = "title")
private String title;
@Id
@GeneratedValue
private Long id;
}
@Entity
@Table
public class CategoryForSong implements Serializable {
private static final long serialVersionUID = 1L;
@JoinColumn(name = "categories")
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Category> categories;
@Column(name = "songTitle")
private String songTitle;
@Id
@GeneratedValue
private Long id;
}
Getters,setters,equals等已被省略。 CategoryForSong包含带有songTitle而不是实际Song的字符串的原因只是我试图隔离问题。这似乎没有帮助。
我的服务不是很有趣,所以我会在这里添加一首歌来展示结构:
@Service
public class ConcreteSongService implements SongService {
@Autowired
private SongRepository songRepository;
@Transactional
@Override
public void save(Song song) {
songRepository.save(song);
}
@Transactional
@Override
public Song get(String title) {
return songRepository.getByTitle(Sets.newHashSet(title)).stream().findFirst().get();
}
}
存储库就像这样简单:
public interface SongRepository extends CrudRepository<Song, Long> {
Set<Song> getByTitle(Set<String> titles);
}
例外情况如下:
org.hibernate.PersistentObjectException: detached entity passed to persist: proofofconcept.springmvc.model.orm.Category
非常感谢帮助。