【发布时间】:2016-09-10 23:18:51
【问题描述】:
我有两个 JPA 实体,一个带有 SDR 导出的存储库,另一个带有 Spring MVC 控制器和一个未导出的存储库。
MVC 公开实体具有对 SDR 托管实体的引用。请参阅下面的代码参考。
从UserController 检索User 时,问题就出现了。 SDR 托管实体不会序列化,似乎 Spring 可能会尝试在响应中使用 HATEOAS refs。
下面是 GET 与完全填充的 User 的样子:
{
"username": "foo@gmail.com",
"enabled": true,
"roles": [
{
"role": "ROLE_USER",
"content": [],
"links": [] // why the content and links?
}
// no places?
]
}
如何使用嵌入式 SDR 托管实体从我的控制器中明确返回 User 实体?
Spring MVC 托管
实体
@Entity
@Table(name = "users")
public class User implements Serializable {
// UID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private Long id;
@Column(unique = true)
@NotNull
private String username;
@Column(name = "password_hash")
@JsonIgnore
@NotNull
private String passwordHash;
@NotNull
private Boolean enabled;
// No Repository
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@NotEmpty
private Set<UserRole> roles = new HashSet<>();
// The SDR Managed Entity
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "user_place",
joinColumns = { @JoinColumn(name = "users_id") },
inverseJoinColumns = { @JoinColumn(name = "place_id")})
private Set<Place> places = new HashSet<>();
// getters and setters
}
回购
@RepositoryRestResource(exported = false)
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
// Query Methods
}
控制器
@RestController
public class UserController {
// backed by UserRepository
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping(path = "/users/{username}", method = RequestMethod.GET)
public User getUser(@PathVariable String username) {
return userService.getByUsername(username);
}
@RequestMapping(path = "/users", method = RequestMethod.POST)
public User createUser(@Valid @RequestBody UserCreateView user) {
return userService.create(user);
}
// Other MVC Methods
}
SDR 托管
实体
@Entity
public class Place implements Serializable {
// UID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank
private String name;
@Column(unique = true)
private String handle;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "address_id")
private Address address;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "contact_info_id")
private ContactInfo contactInfo;
// getters and setters
}
回购
public interface PlaceRepository extends PagingAndSortingRepository<Place, Long> {
// Query Methods
}
【问题讨论】:
-
我假设您确实有 PlaceRepository 的 @Repository 注释 - 只是没有在此处发布?您能否也添加异常的文本?
-
@lenach87 - SDR 不需要
@Repository注释,除非您需要进一步配置它。也没有Exception,只是缺少序列化。 -
您的 JPA 实现可能有问题?如果您使用的是 Hibernate,它可以只加载一个包。您可以创建一个自定义查询来强制它立即加载,或者在将其作为响应发送之前简单地调用属性的访问器(这将强制休眠加载属性包)。
-
也许检查一下,假设您使用杰克逊进行序列化:stackoverflow.com/questions/19580856/…
-
您可以添加您的存储库配置吗?是否有可能有两个 entityManager 在处理这些存储库,而一个看不到另一个中的实体?
标签: java spring-mvc spring-boot spring-data spring-data-rest