我正在尝试构建一个GWT应用程序,该应用程序使用JPA,Hibernate和MySQL与使用Spring Boot构建的现有REST服务进行通信。其中我有两个实体:开发人员和团队。和他们之间的双向OneToMany关系。现在我想创建一个开发人员并将他/她分配给现有的团队。所以我通过GET请求拉出一个团队。它将通过设置的所有参数进行检索。我将它分配给一个新的Developer对象。它仍保留所有参数。但是当我在POST请求中发送它时,团队的ID会丢失,因此Hibernate会创建一个Team实体的新实例。我在哪里做错了?
我看过这篇文章Child's id not present after cascade persisting the parent,但我看不出我怎么能做同样的事......
GWT实体:
开发
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fab.hide();
Snackbar.make(view, "blah", Snackbar.LENGTH_SHORT).show();
new Timer().schedule(new TimerTask() {
@Override
public void run() { fab.show(); }
}, 3000);
}
});
}
团队
import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Developer {
private final Integer id;
private final String name;
private final Team team;
@JsonCreator
public Developer(@JsonProperty("id")Integer id, @JsonProperty("name")String name, @JsonProperty("team")Team team){
this.id=id;
this.name=name;
this.team = team;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Team getTeam() {
return team;
}
}
服务:
TeamService
import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Team {
private final Integer id;
private final String name;
private final List<Developer> developers;
private final int velocity;
@JsonCreator
public Team(@JsonProperty("id")Integer id, @JsonProperty("name")String name, @JsonProperty("velocity")int velocity, @JsonProperty("developers")List<Developer> developers){
this.id = id;
this.name=name;
this.developers = developers;
this.velocity = velocity;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public List<Developer> getDevelopers() {
return developers;
}
public int getVelocity() {
return velocity;
}
@Override
public String toString() {
return "Team "+this.getId()+": name="+this.getName()+"; velocity="+this.getVelocity();
}
}
DeveloperService:
import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.RestService;
import gli.shared.Team;
import java.util.List;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
/**
* The client side stub for the RPC service.
*/
@Path("http://localhost:8080/team")
public interface TeamService extends RestService {
@GET
@Path("/{id}")
void getTeam(@PathParam("id") String id, MethodCallback<Team> team);
@GET
void getAllTeams(MethodCallback<List<Team>> teams);
@GET
@Path("/random")
void getRandomTeam(MethodCallback<Team> team);
@PUT
@Path("/{id}")
void updateTeam(@PathParam("id") String id, MethodCallback<String> result);
@POST
void addTeam(Team team, MethodCallback<String> result);
@DELETE
@Path("/{id}")
void deleteTeam(@PathParam("id") String id, MethodCallback<String> result);
}
最后是EntryPoint代码:
import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.RestService;
import gli.shared.Developer;
import java.util.List;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
/**
* The client side stub for the RPC service.
*/
@Path("http://localhost:8080/developer")
public interface DeveloperService extends RestService {
@GET
@Path("/{id}")
void getDeveloper(@PathParam("id") String id, MethodCallback<Developer> developer);
@GET
void getAllDevelopers(MethodCallback<List<Developer>> developers);
@GET
@Path("/team/{id}")
void getDevelopersByTeam( @PathParam("id") int id, MethodCallback<List<Developer>> developers );
@PUT
@Path("/{id}")
void updateDeveloper(@PathParam("id") String id, MethodCallback<String> result);
@POST
void addDeveloper(Developer developer, MethodCallback<String> result);
@DELETE
@Path("/{id}")
void deleteDeveloper(@PathParam("id") String id, MethodCallback<String> result);
}