简体   繁体   English

如何将曲线存储到数据库

[英]How to store curve to database

I would like to ask how to store curve to DB.我想问一下如何将曲线存储到DB。 Curve object is represented by an array of points. Curve 对象由一组点表示。

I have two options:我有两个选择:

1) create two tables one for curve one for points 1)为点创建两个表,一个用于曲线,一个用于点

  • it would look something like this: CURVE[id] , POINT[id, x, y, order, curve_id]它看起来像这样: CURVE[id] , POINT[id, x, y, order, curve_id]
  • I am not sure if this is optimal, to get one curve from database I would need to join to tables and in the POINT table there would be a lot of points我不确定这是否是最佳的,要从数据库中获得一条曲线,我需要加入表,并且在 POINT 表中会有很多点

2) one curve = one row in one table 2) 一条曲线 = 一张表中的一行

  • I would store all data in one table for example like this: CURVE[id, data] where data could be in formate "[[1,2],[2,2],[3,4] ...]" (string) or as blob or something like that.我会将所有数据存储在一个表中,例如: CURVE[id, data] 其中数据可以是格式“[[1,2],[2,2],[3,4] ...]”( string) 或 blob 或类似的东西。

What do you think?你怎么认为? Is there any other option?还有其他选择吗?

I would suggest to store your data points in separated row, you need only an additional index to order them.我建议将您的数据点存储在单独的行中,您只需要一个额外的索引来对它们进行排序。
You can recreate the string you proposed with the use of GROUP_CONCAT() function.您可以使用GROUP_CONCAT()函数重新创建您建议的字符串。
Your table would have the following structure :您的将具有以下结构

  • curve_id曲线ID
  • point_id点 ID
  • x_coord x_坐标
  • y_coord y_坐标

The querying would look like:查询看起来像:

--- {[1,2], [2,3] ...}
SELECT GROUP_CONCAT(CONCAT_WS("[", x_coord, ",", y_coord, "]"))
FROM table
WHERE curve_id ORDER BY point_id

您可以使用 JSON 数据类型将曲线点存储在单个属性中。

What you can do is to use javax.persistence.AttributeConverter and create curve converter like this:你可以做的是使用 javax.persistence.AttributeConverter 并像这样创建曲线转换器:

public class CurveConverter implements AttributeConverter<Curve, String> {
private static final String SEPARATOR1 = ",";
private static final String SEPARATOR2 = ";";


@Override
public String convertToDatabaseColumn(Curve curve) {
    if (curve == null) {
        return null;
    }

    StringBuilder sb = new StringBuilder();
    for(CurvePoint curvePoint:curve.getPoints()){
        sb.append(curvePoint.getTorque());
        sb.append(SEPARATOR1);
        sb.append(curvePoint.getSpeed());
        sb.append(SEPARATOR2);
    }
    sb.setLength(sb.length() - SEPARATOR2.length());

    return sb.toString();
}

@Override
public Curve convertToEntityAttribute(String dbData) {
    if (dbData == null || dbData.isEmpty()) {
        return null;
    }

    String[] pieces = dbData.split(SEPARATOR2);

    if (pieces == null || pieces.length == 0) {
        return null;
    }

    List<CurvePoint> curvePoints = new ArrayList<>();
    for(String piece:pieces){
        String[] numbers = piece.split(SEPARATOR1);
        curvePoints.add(new CurvePoint(Float.parseFloat(numbers[0]), Float.parseFloat(numbers[1])));
    }

    return Curve.builder().points(curvePoints).build();
}
}

which will store your curve as string (CSV - "1.1,2.2;3.3,4.4 ...") to table as one attribute.它将您的曲线作为字符串(CSV - “1.1,2.2;3.3,4.4 ...”)存储到表格中作为一个属性。 You will get rid of joints and it will be faster.你会摆脱关节,它会更快。 This is only good when the curve is immutable (all points are changing at once)这仅在曲线不可变时才有用(所有点都同时发生变化)

Then you just add annotation on field in entity where the curve is used like this:然后,您只需在实体中的字段上添加注释,其中曲线的使用方式如下:

@Column(name = "curve", length = 10000)
@Basic(fetch = FetchType.LAZY)
@Convert(converter = CurveConverter.class)
Curve curve;

Important here is to set correct length of the string.这里重要的是设置正确的字符串长度。 Also check that @Basic(fetch = FetchType.LAZY) is used to load the curve data lazy (only when we need them).还要检查@Basic(fetch = FetchType.LAZY)是否用于惰性加载曲线数据(仅在我们需要它们时)。 The annotation @Convert(converter = CurveConverter.class) then says to hibernate that before saving this field to database it will be converted using our newly created curve CurveConvertor (same when we will get the data from DB).注释@Convert(converter = CurveConverter.class)然后说要休眠,在将此字段保存到数据库之前,它将使用我们新创建的曲线 CurveConvertor 进行转换(与我们从 DB 获取数据时相同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM