【发布时间】:2020-10-24 02:43:45
【问题描述】:
我正在尝试使用 CrudRepository 实现 Spring data jpa。 下面是我的实体类
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id = null;
@OneToMany
@ElementCollection
@CollectionTable(name="photoUrls")
@Valid
private List<String> photoUrls = new ArrayList<String>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<String> getPhotoUrls() {
return photoUrls;
}
public void setPhotoUrls(List<String> photoUrls) {
this.photoUrls = photoUrls;
}
}
并且有一个如下的仓库,
public interface UserRepository extends CrudRepository<User, Long>{
}
并尝试使用 h2 数据库进行本地测试
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
并且从 CommandLineRunner 调用 UserRepository 的 save 方法,但得到以下错误,
原因:org.hibernate.AnnotationException:使用@OneToMany 或@ManyToMany 定位未映射的类:model.User.photoUrls[java.lang.String] 在 org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1191) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在 org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:794) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
【问题讨论】:
标签: java spring spring-boot hibernate hibernate-mapping