English 中文(简体)
当父/子对象同时存在时, 如何在 Hibernate + Spring JPA 配置中填充外国关键值?
原标题:How to populate foreign key values in a Hibernate + Spring JPA configuration when parent/child objects are persisted at the same time?

我有两个对象,父母和孩子。孩子有一个外国密钥 返回 PARENT_ID 字段的 PARENT 表格( MySQL 数据库) 。

当父母和子女对象同时创建/组合并持续使用时,儿童 PARENT_ID 外国密钥不会自动被覆盖。

样本代码 :

@Entity
@Table(name = "PARENT")
public class Parent {
    @Column(name = "PARENT_ID")
    private Long parentId;

    @OneToMany(mappedBy = "parent")
    private List<Child> children;
}

@Entity
@Table(name = "CHILD")
public class Child {
    @Column(name = "CHILD_ID")
    private Long childId;

    @Column(name = "PARENT_ID")
    private Long parentId;

    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Parent parent;
}

我有两种持久性的样本:

1) 使用休眠实体管理器:

@PersistenceContext
EntityManager em;

...

em.persist(parent);

2) 使用 Spring Data JPA JpaRepository 实例:

public interface ParentRepository extends JpaRepository<Parent, Long> {

}

在我的父母服务问题中:

@Autowired
private ParentRepository parentRepository;

...

public Parent save(Parent parent) {
    return parentRepository.save(parent);
}

建议的外国密钥弹出策略是什么? 父密钥是否先是持续,然后是孩子反对在那个时候设定的外国密钥? 还是有办法同时自动设定外国密钥或参照该外国密钥?

编辑:增加了持久性样本。

Edit 2 (fixed! ) : 根据 Ryan Stewart 的第二点解决了问题 。 我从儿童对象中移除了长父字段, 并在获取器上放置了 parentId 的@ Column 引用 。 还要确保设置正确的引用( 主要是孩子引用回到父母) 。

最佳回答

该守则有两件事阻止其运作:

  1. Your entities have no @Id. You probably just left it off because this is an example, but it s worth pointing out.
  2. You ve double-mapped the column parent_id. Child has both a Long field and a Parent field mapped to the same column. Get rid of the Long. It s the Parent relationship you re after.

在此之后, 您拥有的是一个完全传统的双向单对一对一的组合列。 如果它不为您工作, 那么您就会在创建和保存它们的代码中做错事。 很可能, 您没有正确创建对象。 使用这些实体的正确代码将看起来类似 :

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Parent p = new Parent();
Child child = new Child();
child.parent = p;
Child child1 = new Child();
child1.parent = p;
p.children = Arrays.asList(child, child1);
session.save(p);
session.save(p.children.get(0));
session.save(p.children.get(1));
tx.commit();

特别要注意的是,孩子应该被安置在父母和孩子的父母身上。 不要建一个半碎的物体模型,期望休眠来清理你的烂摊子。 许多人忽视了这一看似显而易见的要求,并想知道休眠为什么看起来不稳定和/或不可靠。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...