[英]Saving integer array using Spring Data JPA
I am using spring data jpa with hibernate and postgres. 我正在使用hibernate和postgres的spring数据jpa。 I am trying to save an integer array in a column.
我正在尝试在列中保存一个整数数组。 I am using vlad mihaceas library for persisting the array into postgresql.
我正在使用vlad mihaceas库将数组持久化到postgresql中。 The entity is as follows:-
实体如下:
@Type(type = "int-array")
@Column(name = "location", columnDefinition = "integer[]")
private Integer[] locations;
The corresponding location entity is 对应的位置实体是
@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;
}
The location ids are being saved in the db. 位置ID被保存在数据库中。 But I am not able to display the in thymeleaf.
但是我无法在百里香中展示。
<tr>
<td>Selected Locations</td>
<td>[[${office.locations[0].locationName}]]</td>
</tr>
The following error has come up:- 出现以下错误:
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'locationName' cannot be found on object of type 'java.lang.Integer' - maybe not public or not valid?
org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“ java.lang.Integer”类型的对象上找不到属性或字段“ locationName”-可能不是公共的或无效的?
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'locationName' cannot be found on object of type 'java.lang.Integer' - maybe not public or not valid?
org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“ java.lang.Integer”类型的对象上找不到属性或字段“ locationName”-可能不是公共的或无效的?
The error has nothing to do with the use of the int-array
of the hibernate-types project. 该错误与休眠类型项目的
int-array
无关。
The error message is about the Spring Expression Language you used for setting the locationName
which is a String
property. 该错误消息与您用于设置
locationName
的Spring Expression Language有关,该locationName
是String
属性。
[[${office.locations[0].locationName}]]
The locations
property is an Integer[]
, but you treated it as a Location
array. locations
属性是Integer[]
,但是您将其视为Location
数组。
What you want is a @OneToMany
List<Location>
instead: 您想要的是
@OneToMany
List<Location>
:
@OneToMany(mappedBy="office", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Location> locations = new ArrayList<>();
And a @ManyToOne
association in Location
: 在
Location
还有一个@ManyToOne
关联:
@ManyToOne(fetch = FetchType.LAZY)
private Office office;
Check out this article for more details about the best way to use a @OneToMany
association. 请查看本文 ,以
@OneToMany
有关使用@OneToMany
关联的最佳方法的更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.