【问题标题】:Error creating bean. Could not determine type for: java.util.List创建 bean 时出错。无法确定类型:java.util.List
【发布时间】:2021-09-06 05:33:40
【问题描述】:

在实体类中,我有一个字段为:@Column(name = "alias", length = 255) private List<String> akaList;

运行时出现错误:

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, at table: customer, for columns: [org.hibernate.mapping.Column(alias)]

可能是什么原因?

【问题讨论】:

  • 这是一个原始类型。因此,hibernate 无法计算出您想要的列表中的内容。添加类型。
  • 什么意思?)你能举个例子吗?
  • 我想将此列表 保存到数据库中。像这样:repository.saveAll()
  • 您的akaList 持有哪些对象?字符串,还是别的什么?

标签: java xml spring hibernate jpa


【解决方案1】:

@Column 之外添加以下注解:@ElementCollection(fetch = FetchType.EAGER)。通常您使用@OneToMany 关联来处理这种关系。元素集合似乎比具有@OneToMany 关联的实体更易于使用。但它有一个主要缺点:集合的元素没有 id,Hibernate 无法单独处理它们。

@ElementCollection(fetch = FetchType.EAGER)
@Column(name = "alias", length = 255) 
private List<String> akaList;

【讨论】:

  • 请编辑您的问题并发布错误的完整堆栈跟踪,而不是在 cmets 中。
  • 我加了,Hibernate,开始保存,但最后还是报错:无法执行语句; SQL [不适用];嵌套异常是 org.hibernate.exception.DataException: could not execute statement
  • 添加堆栈跟踪?
  • 是的,来自您的错误的日志,不要将它们发布在 cmets 中,而是编辑您的问题并将它们放在那里(格式化为代码)。它更具可读性。关于您的错误,您确实知道您的列只能包含 255 个字符,因此如果您的字符串列表总长度超过 255 个字符,您可能会收到您提到的错误。
  • 尝试将length = 255 更改为更大的值,例如length = 4096。请理解,您无法以这种方式保存非常大的列表,您迟早会达到长度限制
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多