我需要设置孩子的父母吗?

时间:2018-05-24 02:01:17

标签: jpa java-ee eclipselink

我遇到域(孩子)未获得商家(父母)ID的问题。我创建了业务和域,但域没有business_id。以下是我最初做的事情。然后我创建了一个我调用的测试路径,除非我使用setBusiness的{​​{1}}方法,否则它会产生相同的行为。这是期望吗?我是否需要遍历从客户端传递的域并设置业务?

测试

domain

代码

public void test() {
    Business b = new Business();
    b.setName("Test Test");
    Domain d1 = new Domain();
    d1.setName("Domain 1");
    d1.setBusiness(b);
    em.persist(b);
    em.flush();
}

-

@Entity
@Table(name = "Business")
public class Business {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;


    @OneToMany(mappedBy="business", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Domain> domains = new ArrayList<>();
    //...getters and setters
}

我使用下面的JSON调用了一个休息服务,然后坚持业务。

@Entity
@Table(name = "Domain")
public class Domain {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "business_id", nullable = false)
    private Business business;
    //...getters and setters
}

-

{
    "name": "Business Test ",
    "domains": [{"name": "test domain"}]
}


@Path("business")
public class BusinesResouce {

    @EJB
    BusinessService service;

    @Path("create")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public void create(Business entity) {
        service.persist(entity);
    }
    //other paths
}

0 个答案:

没有答案