经过大量工作后,我(有点)想出了如何在两个spring实体之间建立关系,现在我可以在我的连接表中创建一个记录。目前我将一个电影对象从我的Angular框架发送到我的Spring API。
@RequestMapping(value = "/", method = RequestMethod.POST)
public Movie createMovie(@RequestBody Movie movie){
return movieService.createMovie(movie);
}
我的MovieController.java:
@Override
public Movie createMovie(Movie movie) {
Date date = new Date();
movieRepository.save(movie);
userRepository.save(new HashSet<User>(){{
add(new User("zxc@cvb.nl", "asd", "zxc", true, "123qwe", date, "cxv", new HashSet<Movie>(){{
add(movie);
}}));
}});
return movieRepository.save(movie);
}
我的MovieService.java:
@Repository
public interface MovieRepository extends CrudRepository<Movie, Serializable> {}
和MovieRepository:
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="api.taxjar.com" port="443" basePath="v2/taxes" doc:name="HTTP Request Configuration"/>
<flow name="postTaxCollectionFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/taxcollection" allowedMethods="POST" doc:name="HTTP"/>
<dw:transform-message doc:name="Transform Message" metadata:id="2789cbd2-5ca6-46c2-856f-67ba2bdfa6dd">
<dw:input-payload mimeType="application/json"/>
<dw:set-payload>
<![CDATA[%dw 1.0
%output application/json
---
payload
]]></dw:set-payload>
</dw:transform-message>
<http:request config-ref="HTTP_Request_Configuration" path="https://api.taxjar.com/v2/taxes" method="POST" doc:name="Web Service">
<http:request-builder>
<http:query-param paramName="Authorization" value="Token token="8dbc821e651fe0672c4032e65209b37c""/>
<http:query-param paramName="Content-Type" value="application/json"/>
</http:request-builder>
</http:request>
<byte-array-to-object-transformer doc:name="Byte Array to Object"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
此代码的主要问题是我在添加影片时实例化了一个新的User对象。这会导致以下错误:
重复录入&#39; cxv&#39;关键&#39; UK_sb8bbouer5wak8vyiiy4pf2bx&#39;
如何重构代码,以便我不必实例化新用户,但我可以使用我当前的用户?