英文:
SpringBoot: Persist nested JSON [using spring-boot-starter-data-rest + sql]
问题
以下是您提供的内容的翻译部分:
我有以下的POST请求:
{
"name": "Peter",
"lastName": "Smith",
"contact": {
"phone": "12345679",
"email": "peter@smith.com"
}
}
我想要将其存储在SQL数据库中,如下所示:
| id (int) | name (varchar) | lastName (varchar) | contact (JSON) |
我正在使用spring-boot-starter-data-rest
,因此我只有UserRepository
和User
实体,其中有一个嵌入式属性contact
。
User.java
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String title;
@Column(name = "lastName")
private String lastName;
@Embedded
@Column(name = "contact")
private Contact contact;
}
Contact.java
@Embeddable
public class Contact {
private String phone;
private String email;
}
UserRepository.java
public interface UserRepository extends JpaRepository<User, Integer> {
//
}
如果我进行POST请求,会出现错误,因为(猜测)我没有将Contact
转换为JSON。
我已经尝试添加了@Convert(converter = HashMapConverter.class)
,但是出现了错误。
HashMapConverter
public class HashMapConverter implements AttributeConverter<Object, String> {
private static final ObjectMapper om = new ObjectMapper();
@Override
public String convertToDatabaseColumn(Object attribute) {
try {
return om.writeValueAsString(attribute);
} catch (JsonProcessingException ex) {
//log.error("Error while transforming Object to a text datatable column as json string", ex);
return null;
}
}
@Override
public Object convertToEntityAttribute(String dbData) {
try {
return om.readValue(dbData, Object.class);
} catch (IOException ex) {
//log.error("IO exception while transforming json text column in Object property", ex);
return null;
}
}
}
英文:
I have the following POST request:
{
"name": "Peter",
"lastName": "Smith",
"contact": {
"phone":"12345679",
"email": "peter@smith.com"
}
}
And I would like to store that in a SQL DB as follow:
| id (int) | name (varchar) | lastName (varchar) | contact (JSON) |
I'm using spring-boot-starter-data-rest
so I only have the UserRepository
and User
Entity, which has an Embedded property contact
User.java
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String title;
@Column(name = "lastName")
private String lastName;
@Embedded
@Column(name = "contact")
private Contact contact;
}
Contact.java
@Embeddable
public class Contact {
private String phone;
private String email;
}
UserRepository.java
public interface UserRepository extends JpaRepository<User, Integer> {
//
}
If I make a POST request I get an error, because (guess) I'm not converting Contact
to JSON.
I've already tried adding a @Convert(converter = HashMapConverter.class)
but I get an error.
HashMapConverter
public class HashMapConverter implements AttributeConverter<Object, String> {
private static final ObjectMapper om = new ObjectMapper();
@Override
public String convertToDatabaseColumn(Object attribute) {
try {
return om.writeValueAsString(attribute);
} catch (JsonProcessingException ex) {
//log.error("Error while transforming Object to a text datatable column as json string", ex);
return null;
}
}
@Override
public Object convertToEntityAttribute(String dbData) {
try {
return om.readValue(dbData, Object.class);
} catch (IOException ex) {
//log.error("IO exception while transforming json text column in Object property", ex);
return null;
}
}
}
答案1
得分: 0
你需要为Contact
创建一个实体,然后在两者之间创建一对一的关系。查看这个示例。
英文:
You need to create an entity for Contact
and then create a one to one relationship between the two. Check out this example.
答案2
得分: 0
我已经有一个用于存储 JSON 字段的相同情况,它完美地工作。请尝试:
在 pom.xml 中添加依赖:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.9.7</version>
</dependency>
在 User 类中进行编辑:
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
public class User {
// 其他字段在这里
@Type(type = "json")
@Column(columnDefinition = "json")
private Contact contact;
// getter 和 setter 方法
}
当然,您的数据库应支持 json 类型。例如,对于 MariaDB,您可以参考 https://mariadb.com/kb/en/json-data-type/
英文:
i've got the same case for storing json field which is working perfectly. Please try :
Add dependency to pom.xml :
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.9.7</version>
</dependency>
Edit User class within :
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
public class User {
// other field here
@Type(type = "json")
@Column(columnDefinition = "json")
private Contact contact;
// getters, setters
}
Of course your database should support json type. For MariaDB for example you can refer to https://mariadb.com/kb/en/json-data-type/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论