我有一个这样的实体:
@Entity
@Data
public class Cat {
@Id
private String catId;
private String catName;
private List<String> favFoods;
}
当我启动我的Spring引导时,它显示这个错误:
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List
在启动应用程序之前,我在DB中删除表Cat我的yml设置是:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/localtest
username: catuser
password:
jpa:
show-sql: false
hibernate:
ddl-auto: create
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
如果我注释掉List的字段,一切都可以正常工作。我需要添加什么注释来解决这个问题吗?
谢谢
您还可以使用以下代码片段
@ElementCollection
@CollectionTable(name = "my_list", joinColumns = @JoinColumn(name =
"id"))
@Column(name = "list")
List<String> favFoods;
看到
你必须告诉Hibernate如何映射列表,用@TypeDef(name = "list-array",typeClass = ListArrayType.class)
注释你的类,用@Type(type = "list-array")
注释列表,即:
@Entity
@Data
@TypeDef(
name = "list-array",
typeClass = ListArrayType.class
)
public class Cat {
@Id
private String catId;
private String catName;
@Type(type = "list-array")
private List<String> favFoods;
}