我正在开发一个Spring Boot项目,并且正在使用jpa进行数据持久化。现在,我有两个相互关联的表:用户和项目。一个用户可以拥有任意数量的项目,而一个项目只能由一个用户拥有。
这是我的pojos:
用户数
@Entity
@Table(name="users", uniqueConstraints = {
@UniqueConstraint(columnNames = {
"email"
})
})
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@NaturalId
@Column(unique = true)
private String email;
@NotBlank
@JsonIgnore
private String password;
@NotBlank
private String first_name;
@NotBlank
private String last_name;
@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true)
@JsonIgnore
private Set<Item> items;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updatedAt;
物品
@Entity
@Table(name="items")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@NotNull
private String name;
private String description;
@NotNull
private Date purchase_date;
private double price;
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "owner", nullable = true)
private User owner;
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updatedAt;
现在,我想以RESTfull JSON形式获取所有项目。我需要为用户添加项目,以便获得以下JSON:
{
"item_id":1,
"name":"item1",
"price":120.00,
etc .....
"owner_id":1,
"owner_name":"Jon"
etc ....
}
所以我正在使用自定义的本机查询
SELECT i.id, i.name, i.description ...... u.id, u.name ..... from items i , users u where i.owner = u.id
然后我返回
query.getResultList()
,但是返回的是字符串数组,而不是像这样的json[
[ 1 , "item1" , 120.00 , ..... , 1 , "Jon" , ....]
[ 2 , "item2" , 420.00 .... ]
etc...
]
如何将返回的对象直接转换为JSON或映射到将列名映射到值的映射列表,然后将其转换为JSON?
最佳答案
您可以使用构造函数表达式创建一个包含所需数据的DTO(数据传输对象)。
package dto;
public class ItemDTO {
private final Long item_id;
private final String name;
private final Long owner_id;
private final String owner_name;
public ItemDTO(Long item_id, String name, Long owner_id, String owner_name) {
// set the fields
}
// getters
}
然后在构造函数表达式查询中使用此DTO(重要说明:这仅适用于JPQL查询而不适用于本机查询)
SELECT new dto.ItemDTO(i.id, i.name, i.owner.id, i.owner.name) from Item i where i.owner.id = u.id
此DTO可用于序列化为JSON。
在此处阅读有关构造函数表达式的更多信息:
https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/
关于java - 将JPA(Hibernate)中的 native (联接)查询转换为JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51854878/