如何保存一对多的权利

时间:2017-10-30 23:05:54

标签: java spring hibernate spring-data-jpa one-to-many

我无法保存以下实体。我想在尝试保存服务器时选择实验室。

@Entity
@Getter
@Setter
public class Lab {

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @NotNull
    @Column(name = "LAB_NAME")
    private String labName;

    @NotNull
    @Column(name = "LAB_PRIME")
    private String labPrime;

    @NotNull
    @Column(name = "LAB_SERVICE_IP", nullable = false)
    private String serviceIp;

    @Column(name = "LAB_OWNER", nullable = false)
    @Enumerated(EnumType.STRING)
    private LabOwner labOwner;

    @Column(name = "LAB_RELEASE")
    @Enumerated(EnumType.STRING)
    private LabRelease labRelease;

    @JsonIgnore
    @OneToMany(fetch = FetchType.EAGER)
    private Set<Server> servers;


    public Lab() {
    }

    public Lab(String labName, String labPrime, String serviceIp, LabOwner labOwner, LabRelease labRelease, Set<Server> servers) {
        this.labName = labName;
        this.labPrime = labPrime;
        this.serviceIp = serviceIp;
        this.labOwner = labOwner;
        this.labRelease = labRelease;
        this.servers = servers;
    }
}

存储库:

public interface LabRepository extends JpaRepository<Lab, Long> {

}
public interface ServerRepository extends JpaRepository<Server, Long> {

}

服务器授权;

@Entity
@Getter
@Setter
public class Server {

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @NotNull
    @Column(name = "LOGICAL_IP")
    private String logicalIp;

    @NotNull
    @Column(name = "INSTANCE_TYPE")
    private String instanceType;

    @NotNull
    @Column(name = "HOST_NAME", nullable = false)
    private String hostName;

    @NotNull
    @Column(name = "HDWR_TYPE", nullable = false)
    private String hardwareType;

    @NotNull
    @Column(name = "A2_TYPE", nullable = false)
    private String a2Type;

    @ManyToOne(fetch = FetchType.LAZY)
    private Lab lab;

    public Server() {
    }

    public Server(String logicalIp, String instanceType, String hostName, String hardwareType, String a2Type, Lab lab) {
        this.logicalIp = logicalIp;
        this.instanceType = instanceType;
        this.hostName = hostName;
        this.hardwareType = hardwareType;
        this.a2Type = a2Type;
        this.lab = lab;
    }
}

控制器:

@RestController
@RequestMapping(value = "services/")
public class GenericController  {

    @Autowired
    LabRepository labRepository;

    @Autowired
    LabRepository serverRepository;

    @RequestMapping(value = "server", method = RequestMethod.POST)
    public Server create(@RequestBody Server server) {
        return serverRepository.saveAndFlush(server);
    }
}

我不能使用serverRepository.saveAndFlush(服务器)。它说S不在其范围内,应扩展Lab。

但是,当我扩展Lab权限时,我的表被合并了。我想要2个分开的桌子。

1 个答案:

答案 0 :(得分:0)

在您的控制器中,您使用的是LabRepository而不是ServerRepository。它应该是:

@RestController
@RequestMapping(value = "services/")
public class GenericController  {

    @Autowired
    LabRepository labRepository;

    @Autowired
    ServerRepository serverRepository;

    @RequestMapping(value = "server", method = RequestMethod.POST)
    public Server create(@RequestBody Server server) {
        return serverRepository.saveAndFlush(server);
    }
}