Hibernate Spring JPA更新不适用于嵌套对象
Hibernate Spring JPA更新不适用于嵌套对象,hibernate,spring-boot,jpa,spring-data,spring-data-rest,Hibernate,Spring Boot,Jpa,Spring Data,Spring Data Rest,我在使用SpringRESTJPA(PUT方法)更新数据库数据时遇到问题 当我执行POST请求以插入数据时,它工作正常,我以正确的方式插入了所有数据,但是,当我尝试使用PUT更新数据时,名称是数据库中的更新事件,但不是GeometryBasic,我尝试记录其值它正确更改但不在数据库中,hibernate不会保留新值数据。 我的课程如下: 位置 多重 我也面临同样的问题。放置嵌套对象无效。然而,POST和补丁运行良好 只需将HTTP谓词从PUT替换为PATCH。修补资源的所有字段(请求正文将与使用
我在使用SpringRESTJPA(PUT方法)更新数据库数据时遇到问题 当我执行POST请求以插入数据时,它工作正常,我以正确的方式插入了所有数据,但是,当我尝试使用PUT更新数据时,名称是数据库中的更新事件,但不是GeometryBasic,我尝试记录其值它正确更改但不在数据库中,hibernate不会保留新值数据。 我的课程如下: 位置 多重
我也面临同样的问题。放置嵌套对象无效。然而,POST和补丁运行良好 只需将HTTP谓词从PUT替换为PATCH。修补资源的所有字段(请求正文将与使用PUT时相同)将与放置资源时相同 也许这是Spring数据Rest中的一个bug
可能相关:在您的
MultiBasic.geometry
属性上有一个@JsonIgnore
。这意味着对于PUT
请求,它也将被忽略。我删除了@JsonIgnore,但没有任何更改,在我的日志控制台上看不到更新查询。我忘记告诉我正在使用这个对象“几何体”:[[{“x”:-170341.70902636572,“y”:5977585.216564284,“经度”:-1.5302056074142456,“纬度”:47.217160052145664}]仍然没有答案:/
@Data
@Entity
@Table(name="sw_locations")
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Location_Generator")
@SequenceGenerator(name="Location_Generator", sequenceName = "Location_Sequence")
private Long id;
@Column(name = "latitude")
private Double latitude;
@Column(name = "longitude")
private Double longitude;
@Column(name = "x")
private Double x;
@Column(name = "y")
private Double y;
@ManyToOne @JoinColumn(name="multi_id") @JsonIgnore
private MultiBasic multi;
public boolean isEmpty() {
boolean hasNoPointCoord = (this.getLatitude() ==null && this.getLongitude() == null);
return hasNoPointCoord;
}
@Override
public String toString() {
return "Location [point="+this.getX()+" : "+this.getY()+" : "+this.getLatitude() +" : "+ this.getLongitude() + "]";
}
}
public class Multi extends ArrayList<Location>{
//Some methods
public class Geometry extends ArrayList<Multi>{
//Some Methods
}
@Data
@Entity
@Table(name="sw_geometries")
public class GeometryBasic{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Geometry_Generator")
@SequenceGenerator(name="Geometry_Generator", sequenceName = "Geometry_Sequence")
private long id;
@OneToMany(mappedBy="geometry",
cascade= CascadeType.ALL, fetch=FetchType.LAZY)
private List<MultiBasic> multies = new ArrayList<MultiBasic>();
//some other methods
}
@Data
@Entity
@Table(name="sw_multis")
public class MultiBasic{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Multi_Generator")
@SequenceGenerator(name="Multi_Generator", sequenceName = "Multi_Sequence")
private long id;
@ManyToOne @JoinColumn(name="geometry_id") @JsonIgnore
private GeometryBasic geometry;
@OneToMany(mappedBy="multi",
cascade= CascadeType.ALL,fetch=FetchType.LAZY)
private List<Location> locations = new ArrayList<Location>();
}
public class GeometryHelper {
public static Geometry map(GeometryBasic geom) {
//Check if geometry object is defined and contains multies
if(geom == null || geom.isEmpty())
return null;
Geometry geometry = new Geometry();
//Map nested collections into 2D Array.
geom.getMulties().forEach(m->{
Multi multi = new Multi();
multi.addAll(m.getLocations());
geometry.add(multi);
});
return geometry;
}
public static GeometryBasic mapToBasic (Geometry geom) {
//Check if geometry object is defined and contains multies
if(geom == null)
return null;
GeometryBasic geometry = new GeometryBasic();
List<MultiBasic> multis = new ArrayList<MultiBasic>();
//Iterate over multis to add link to geometry
geom.forEach(m ->{
MultiBasic multi = new MultiBasic();
List<Location> locations = new ArrayList<Location>();
//iterate over locations to add link to Multi
m.forEach( l -> {
l.setMulti(multi);
locations.add(l);
});
multi.setLocations(locations);
multi.setGeometry(geometry);
multis.add(multi);
});
geometry.setMulties(multis);
return geometry;
}
}
@MappedSuperclass
public abstract class AbstractEntityWithNameTitleLocation extends AbstractEntityWithNameTitle {
@Column(name="entity_name")
private String name;
@OneToOne(fetch = FetchType.EAGER, cascade= CascadeType.ALL) @JoinColumn(name = "id_geometry") @JsonIgnore
private GeometryBasic geometryBasic;
public GeometryBasic getGeometryBasic() {
return this.geometryBasic;
}
public void setBasicGeometry(GeometryBasic geometry) {
this.geometryBasic = geometry;
}
@Transient
Geometry geometry;
//Return the transformed object basic geometry
public Geometry getGeometry() {
return GeometryHelper.map(this.geometryBasic);
}
//Return the transformed object basic geometry
public void setGeometry(Geometry geom) {
this.setBasicGeometry(GeometryHelper.mapToBasic(geom));
}
}