【问题标题】:Spring MappingException: Could not determine typeSpring MappingException:无法确定类型
【发布时间】:2022-01-18 21:21:01
【问题描述】:

我正在构建一个用于测试场景的单页 Web 应用程序,并且我正在使用 spring-jpa。我想将此 JSON 数据模型用于我的后期请求:

{
  "id": 1,
  "title": "test-title",
  "releaseDate": "2021/12/15",
  "rating" : {
      "stars" : 5,
      "comment" : "very exciting"
   }
}

如果我启动我的应用程序,我会收到以下错误: 原因:org.hibernate.MappingException:无法确定类型:de.demo.dto.Rating,表:书籍,列:[org.hibernate.mapping.Column(rating)]

如果我使用@Entity 声明类“评级”并添加字段“id”,则应用程序启动时没有错误(如果我使用@OneToOne 注释)。但是对于“评级”类,我不想使用带有“id”的自己的数据表。每个人都可以帮助我解决我的问题吗?我该如何解决这个问题?

课本:

@Getter
@Setter
@Entity
public class Books {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(unique = true, nullable = false)
    private int id;
    private String title;
    private String releaseDate;
    private Rating rating;
}

班级评分

@Getter
@Setter
public class Rating {
    int stars;
    String comment;
}

谢谢!

【问题讨论】:

  • 我找到了解决方案。我可以使用 Embedded-and Embeddable-Annotation

标签: java spring-boot hibernate spring-data-jpa


【解决方案1】:

所以看起来你想要一对一的关系但不希望它在另一个表中,我想到的最好的事情是将评级保存为 json 对象字符串。因此,您可能需要使用 GSON 等第三方库进行 cruds:

Gson gson = new Gson(); // Or use new GsonBuilder().create();
MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2

【讨论】:

  • 谢谢!我找到了解决方案。我可以使用 Embedded-and Embeddable-Annotation
猜你喜欢
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 2018-07-12
  • 2014-01-10
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多