Spring Boot中@OneToMany关联未映射类触发AnnotationException的解决
Spring Boot 2.7.15 @OneToMany关联问题排查与解决
问题背景
我正在学习Java Spring Boot 2.7.15,开发一个学习管理系统练手项目,需要为讲师(Instructor)与社交链接(InstructorSocial)建立@OneToMany关联,过程中遇到问题,尝试多种方案后仍回到同一错误。
初始错误代码
InstructorEntity.java
@Entity @Table(name = "instructor", uniqueConstraints = @UniqueConstraint(columnNames = {"email"})) public class InstructorEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String uuid; private String firstName; private String lastName; private String profession; @Column(columnDefinition = "longtext", length = 65555) private String bio; @Column(unique = true) private String email; private String phone; private String website; @OneToMany( cascade = CascadeType.ALL, orphanRemoval = true ) @JoinTable( name = "instructor_social", joinColumns = {@JoinColumn(name = "instructor_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "instructor_social_id", referencedColumnName = "id")} ) private List<InstructorSocial> socials = new ArrayList<>(); public InstructorEntity() { } public InstructorEntity(long id, String uuid, String firstName, String lastName, String profession, String bio, String email, String phone, String website) { this.id = id; this.uuid = uuid; this.firstName = firstName; this.lastName = lastName; this.profession = profession; this.bio = bio; this.email = email; this.phone = phone; this.website = website; } // Getters and Setters }
InstructorSocialEntity.java
@Entity @Table(name = "instructor_social") public class InstructorSocialEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String uuid; private String name; private String image; public InstructorSocialEntity() { } public InstructorSocialEntity(long id, String uuid, String name, String image) { this.id = id; this.uuid = uuid; this.name = name; this.image = image; } // Getters and Setters }
修正后可运行代码
Instructor.java
@Entity @Table(name = "instructor", uniqueConstraints = @UniqueConstraint(columnNames = {"email"})) public class Instructor { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String uuid; private String firstName; private String lastName; private String profession; @Column(columnDefinition = "longtext", length = 65555) private String bio; @Column(unique = true) private String email; private String phone; private String website; @OneToMany( mappedBy = "instructor", cascade = CascadeType.ALL, orphanRemoval = true ) private List<InstructorSocial> socials = new ArrayList<>(); public Instructor() { } public Instructor(long id, String uuid, String firstName, String lastName, String profession, String bio, String email, String phone, String website) { this.id = id; this.uuid = uuid; this.firstName = firstName; this.lastName = lastName; this.profession = profession; this.bio = bio; this.email = email; this.phone = phone; this.website = website; } }
InstructorSocial.java
@Entity @Table(name = "instructor_social") public class InstructorSocial { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String uuid; private String name; private String image; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "instructor_id") private Instructor instructor; public InstructorSocial() { } public InstructorSocial(long id, String uuid, String name, String image) { this.id = id; this.uuid = uuid; this.name = name; this.image = image; } }
补充问题
修正关联配置后应用可正常运行,但出现新问题:将社交链接添加到讲师的socials列表时,数据库中已新增记录,但Instructor.socials列表未更新。相关Service代码如下:
InstructorService.java
InstructorSocial social = new InstructorSocial(); social.setUuid(); social.setName(instructorSocial.getName()); social.setImage(instructorSocial.getImage()); foundInstructor.getSocials().add(social); instructorRepository.save(foundInstructor);
补充问题解决方案
排查发现问题出在Service层的getAllInstructors()方法实现上,未正确映射关联的社交链接列表。修正后的代码如下:
service/InstructorServiceImpl.java
@Override public List<Instructor> getAllInstructors() { return instructorRepository .findAll().stream() .map(instructorEntity -> new Instructor( instructorEntity.getId(), instructorEntity.getUuid(), instructorEntity.getFirstName(), instructorEntity.getLastName(), instructorEntity.getProfession(), instructorEntity.getBio(), instructorEntity.getEmail(), instructorEntity.getPhone(), instructorEntity.getWebsite(), instructorEntity.getSocials() .stream() .map(instructorSocialEntity -> new InstructorSocial( instructorSocialEntity.getId(), instructorSocialEntity.getUuid(), instructorSocialEntity.getName(), instructorSocialEntity.getImage() )) .collect(Collectors.toList()) )) .collect(Collectors.toList()); }
内容的提问来源于stack exchange,提问作者hawkinside_out
相关产品推荐
相关产品推荐

