我有一個這樣的物體:
@Entity
@Data
public class Cat {
@Id
private String catId;
private String catName;
private List<String> favFoods;
}
當我啟動 Spring Boot 時,它顯示此錯誤:
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
在啟動應用程式之前,我將表 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 欄位,則一切正常。是否需要添加任何注釋來解決此問題?
謝謝
uj5u.com熱心網友回復:
你必須告訴 Hibernate如何通過注釋你的類@TypeDef(name = "list-array",typeClass = ListArrayType.class)并用@Type(type = "list-array")ie注釋串列來映射串列:
@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;
}
uj5u.com熱心網友回復:
您還可以使用以下代碼片段
@ElementCollection
@CollectionTable(name = "my_list", joinColumns = @JoinColumn(name =
"id"))
@Column(name = "list")
List<String> favFoods;
看這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/336546.html
標籤:爪哇 春天 PostgreSQL 弹簧靴 jpa
上一篇:SpringBoot Hibernate-插入查詢變慢
下一篇:JPA回滾嵌套事務
