保存后刷新并获取实体(JPA/Spring Data/Hibernate)
我有这两个简单的实体和.该实体与 具有多对一关系,因此当我创建新行时,我会分配一个现有的 .Something
Property
Something
Property
Something
Property
东西:
@Entity
@Table(name = "something")
public class Something implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "owner")
private String owner;
@ManyToOne
private Property property;
// getters and setters
@Override
public String toString() {
return "Something{" +
"id=" + getId() +
", name='" + getName() + "'" +
", owner='" + getOwner() + "'" +
", property=" + getProperty() +
"}";
}
财产:
@Entity
@Table(name = "property")
public class Property implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "shape")
private String shape;
@Column(name = "color")
private String color;
@Column(name = "dimension")
private Integer dimension;
// getters and setters
@Override
public String toString() {
return "Property{" +
"id=" + getId() +
", shape='" + getShape() + "'" +
", color='" + getColor() + "'" +
", dimension='" + getDimension() + "'" +
"}";
}
}
这是(春天):SomethingRepository
@SuppressWarnings("unused")
@Repository
public interface SomethingRepository extends JpaRepository<Something,Long> {
}
通过REST控制器和JSON,我想创建一个新的:Something
@RestController
@RequestMapping("/api")
public class SomethingResource {
private final SomethingRepository somethingRepository;
public SomethingResource(SomethingRepository somethingRepository) {
this.somethingRepository = somethingRepository;
}
@PostMapping("/somethings")
public Something createSomething(@RequestBody Something something) throws URISyntaxException {
Something result = somethingRepository.save(something);
return result;
}
}
这是输入中的 JSON(1 是数据库中的现有行):property
id
{
"name": "MyName",
"owner": "MySelf",
"property": {
"id": 1
}
}
问题是:在方法之后,变量包含持久化实体,但没有字段的字段,已验证(它们是):.save(something)
result
property
null
输出 JSON:
{
"id": 1,
"name": "MyName",
"owner": "MySelf",
"property": {
"id": 1,
"shape": null,
"color": null,
"dimension": null
}
}
我希望它们在保存操作后被验证/返回。
为了解决这个问题,我必须在REST控制器中注入/声明,并调用该方法(或者我必须调用一个方法才能获得完整的持久化实体):EntityManager
EntityManager.refresh(something)
.findOne(something.getId())
@RestController
@RequestMapping("/api")
@Transactional
public class SomethingResource {
private final SomethingRepository somethingRepository;
private final EntityManager em;
public SomethingResource(SomethingRepository somethingRepository, EntityManager em) {
this.somethingRepository = somethingRepository;
this.em = em;
}
@PostMapping("/somethings")
public Something createSomething(@RequestBody Something something) throws URISyntaxException {
Something result = somethingRepository.save(something);
em.refresh(result);
return result;
}
}
有了这个解决方法,我得到了预期的保存(使用正确的JSON):
{
"id": 4,
"name": "MyName",
"owner": "MySelf",
"property": {
"id": 1,
"shape": "Rectangle",
"color": "Red",
"dimension": 50
}
}
有没有一个自动的方法/注释,JPA或Spring或Hibernate,以便拥有“完整”的持久化实体?
我希望避免在每个 REST 或 Service 类中声明 the,或者我想避免每次需要新的刷新持久化实体时调用该方法。EntityManager
.findOne(Long)