I am building a Single-Page-Webapp for testing-scenario and i am using spring-jpa. I want to use this JSON data-model for my post-request:
{
"id": 1,
"title": "test-title",
"releaseDate": "2021/12/15",
"rating" : {
"stars" : 5,
"comment" : "very exciting"
}
}
If i start my application, i get the following error: Caused by: org.hibernate.MappingException: Could not determine type for: de.demo.dto.Rating, at table: books, for columns: [org.hibernate.mapping.Column(rating)]
If i am declaring the class "rating" with @Entity and add the field "id", the application is starting without errors (if i am using an @OneToOne annotation). But for the class "Rating" i do not want to use an own data table with an "id". Can everyone help me with my issues? How do i fix this problem?
Class books:
@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;
}
class Rating
@Getter
@Setter
public class Rating {
int stars;
String comment;
}
Thanks!
CodePudding user response:
So seems you want one to one relationship but dont want it to be in another table, best thing comes to my mind is saving that rating as a json object String. So you might need to do cruds with some third party library like GSON:
Gson gson = new Gson(); // Or use new GsonBuilder().create();
MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2