简体   繁体   English

JPA:使用单向@OneToMany时,子实体中的引用列为null

[英]JPA: Reference column in the child entity is null when using unidirectional @OneToMany

I have two entity classes. 我有两个实体类。

Order.java Order.java

@Entity
@Table(name = "order_table")
public class Order implements Serializable {

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

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

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "order_id", referencedColumnName = "id", nullable = false, insertable=false, updatable=false)
    private Set<Item> items;

    // getters & setters & toString

Item.java Item.java

@Entity
@Table(name = "item")
public class Item implements Serializable {

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

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

    @Column(name = "order_id", nullable = false)
    private Long orderId;

    // getters & setters && toString

I created a test class like this: 我创建了这样的测试类:

@Test
public void createOrderWithItems() {

    Item item = new Item();
    item.setName("Iron Man");

    Order order = new Order();
    order.setName("Toy");
    order.getItems().add(item);

    Order created = service.createOrder(order);

    Order orderById = service.getOrderById(order.getId());
    System.out.println("Created Order: " + orderById);

    Item itemById = service.getItemById(item.getId());
    System.out.println("Created item: " + itemById);


    Assert.notNull(created.getId(), "Order ID is Null");
}

Test is green but if you check output, you'll see that orderId field in the Item class is null. 测试为绿色,但如果您检查输出,则会看到Item类中的orderId字段为null。

Created Order: Order{id=1, name='Toy', items=[Item{id=2, name='Iron Man', orderId=null}]}
Created item: Item{id=2, name='Iron Man', orderId=null}

Does JPA not update this column in the db automatically? JPA是否不会自动更新数据库中的此列? Is this column is redundant? 此列是否多余? If so, how can I retrieve this information from test code? 如果是这样,我如何从测试代码中检索此信息?

You need to set orderId explicitly. 您需要显式设置orderId。

item.setOrderId(order.getId());
order.getItems().add(item);

You can create a method addItem(Item item) in your Order class and hide this logic within it. 您可以在Order类中创建方法addItem(Item item)并在其中隐藏此逻辑。

Cascading will create an entry in db but it won't initialize field. 级联将在db中创建一个条目,但不会初始化字段。 JPA annotations just indicate to JPA provider how to perform mapping between entity and table. JPA批注仅向JPA提供程序指示如何在实体和表之间执行映射。

Moreover, check your annotations. 此外,请检查您的注释。 @JoinColumn should be used in the entity which owns the relationship (the corresponding table has column as a foreign key). @JoinColumn应该在拥有关系的实体中使用(相应的表将列作为外键)。 Check the top answer for this question for detailed explanations: What's the difference between @JoinColumn and mappedBy when using a JPA @OneToMany association 检查此问题的顶部答案以获取详细说明: 使用JPA @OneToMany关联时,@ JoinColumn和maptedBy有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使两个不同的父实体通过JPA中的@OneToMany引用子实体 - Make 2 different Parent Entities reference a Child Entity through @OneToMany in JPA JPA @OneToMany - Set vs List - 使用 Set 时无法从双向关联中删除一个子实体 - JPA @OneToMany - Set vs List - Not able to delete one child entity from bidirectional association when using Set 使用外键列的单向OneToMany - Unidirectional OneToMany using Foreign Key Column 在JPA中使用oneToMany关系时,Postgresql在列中抛出空值违反了非空约束 - Postgresql throw null value in column violates not-null constraint when using oneToMany relationship in JPA JPA / Hibernate单向OneToMany为“ mappedBy” - JPA/Hibernate Unidirectional OneToMany as 'mappedBy' 如何使用 JPA 和 Hibernate 为 2 个表建立单向 @oneToMany 关系 - How to make unidirectional @oneToMany relationship for 2 tables using JPA and Hibernate 无法添加子实体 - JPA OneToMany 关系 - Unable to add child entity - JPA OneToMany relationship JPA @OneToMany -&gt; 父 - 子参考(外键) - JPA @OneToMany -> Parent - Child Reference (Foreign Key) 在JoinTable中使用Criteria在休眠的单向OneToMany关系中获取子类 - Using Criteria get child class in unidirectional OneToMany relationship in hibernate with JoinTable JPA @OneToMany和@ManyToOne:后引用为空 - JPA @OneToMany and @ManyToOne: back reference is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM