【发布时间】:2018-11-15 07:44:25
【问题描述】:
我正在使用带有 hibernate 和 postgres 的 spring data jpa。我正在尝试将整数数组保存在一列中。我正在使用 vlad mihaceas 库将数组保存到 postgresql 中。实体如下:-
@Type(type = "int-array")
@Column(name = "location", columnDefinition = "integer[]")
private Integer[] locations;
对应的位置实体是
@Entity
@Table(name = "location_master")
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "location_name")
private String locationName;
}
位置 ID 正在保存在数据库中。但我无法在百里香中显示。
<tr>
<td>Selected Locations</td>
<td>[[${office.locations[0].locationName}]]</td>
</tr>
出现以下错误:-
org.springframework.expression.spel.SpelEvaluationException: EL1008E: 在类型的对象上找不到属性或字段“locationName” 'java.lang.Integer' - 可能不公开或无效?
【问题讨论】:
-
嗯,消息说明了一切。为什么您认为可以从 Integer 中获取 locationName?这是没有意义的。整数是整数。不是位置。如果您想要 Collection
,则使用 Collection ,并使其成为未知实体和 Location 实体之间的 OneToMany 关联。 -
@JB Nizet 我想将数据库中选定的位置 ID 保存为数组。我已经尝试过 List
,它给出了错误 Use of OneToMany or ManyToMany 针对未映射的类。由于 Hibernate 只映射原始类型,我使用的是 vlad mihalcea 的库 -
这不是正确的数据库设计。使用外键、连接列、连接表和实体之间的关联。
-
@JBNizet 数据库设计不在我的控制范围内。我必须使用给我的东西。
-
尝试使用
@ElementCollection和@CollectionTable注解
标签: java spring postgresql hibernate spring-data-jpa