Spring Boot Rest JPA更新数据错误

时间:2018-06-11 15:35:26

标签: hibernate spring-boot jpa spring-restcontroller

对不起,我知道我之前问过这个问题,但没有得到答案。
使用Spring rest JPA(PUT方法)更新数据库数据时遇到问题。

当我执行POST请求以插入数据时,它运行正常,我以正确的方式插入了所有数据,但是,当我尝试使用PUT更新数据时,Name是数据库中的更新事件,但不是GeometryBasic,我试图记录它的值它正确更改但不在数据库中,hibernate不会保留新的值数据。 我的课程如下:

  

位置

@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
}
  

GeometryBasic

@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
}
  

MultiBasic

@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>();

}
  

GeometryHelper

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));
    }
}
Object :
 "geometry":[[{"x":-170341.70902636572,"y":5977585.216564284,"longitude":-1.5302056074142456,"latitude":47.217116005214564}]]

0 个答案:

没有答案