为了运行应用程序,我在war中使用了tomcat 8.5.50包。我使用的是Spring5.2版本。在我的代码中,我想像这样使用LocalDataTime:
@Entity
@Table(name="meals")
public class Meal {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "date_time")
@Convert(converter = MealConverter.class)
private LocalDateTime datetime;
@Column(name = "description")
private String description;
@Column(name = "calories")
private int calories;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public LocalDateTime getDatetime() {
return datetime;
}
public void setDatetime(LocalDateTime datetime) {
this.datetime = datetime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
this.calories = calories;
}
}我的转换器:
@Converter(autoApply = true)
public class MealConverter implements AttributeConverter<Meal, String> {
private static final String SEPARATOR = ", ";
@Override
public String convertToDatabaseColumn(Meal meal) {
StringBuilder sb = new StringBuilder();
sb.append(meal.getCalories()).append(SEPARATOR)
.append(meal.getDatetime())
.append(SEPARATOR)
.append(meal.getDescription());
return sb.toString();
}
@Override
public Meal convertToEntityAttribute(String dbData) {
String[] rgb = dbData.split(SEPARATOR);
Meal meal = new Meal(Integer.valueOf(rgb[0]),
LocalDateTime(valueOf(rgb[1]),
rgb[2],
rgb[3]);
return meal;
}
}我尝试在convertToEntityAttribute方法中使用转换器,但编译器不允许我这样做。我的转换器中需要修复的是什么?

发布于 2020-03-29 20:07:43
Meal meal = new Meal(Integer.valueOf(rgb[0]),
LocalDateTime(valueOf(rgb[1]),
rgb[2],
rgb[3]);Meal类似乎没有任何显式的构造函数,因此您不能将参数传递给new Meal()。您似乎正在尝试传递两个参数。您可能想要创建一个合适的构造函数,或者您可能想要在对象created.LocalDateTime是一个类之后使用setter将这两个值传递给Meal,但是您似乎试图将其作为一个带有三个参数的方法来调用。如果是java.time.LocalDateTime,您可能打算使用LocalDateTime.of(someArguemts),但是该类没有任何带有三个参数的of方法。如果你能更好地解释你所期望的结果,我们可以更好地指导你。LocalDateTime的第一个参数,你有一个似乎没有在你的类中声明的valueOf方法的调用。我不能肯定下面的是正确的,或者是你想让它做的事情,但这只是对你可能想要的东西的猜测。
@Override
public Meal convertToEntityAttribute(String dbData) {
String[] fields = dbData.split(SEPARATOR);
Meal meal = new Meal();
meal.setCalories(Integer.parseInt(fields[0]));
meal.setDateTime(LocalDateTime.parse(fields[1]));
meal.setDescription(fields[2]);
return meal;
}我正在尝试做与您的convertToDatabaseColumn方法相反的操作。我放弃了变量名rgb,因为我看不出它在这里为什么不会被误导。
https://stackoverflow.com/questions/60899851
复制相似问题