3

The problem is to select several parent entities and link them with corresponding children. For such task parents should not be loaded at all (e.g. only collection of ids) or loaded with lazy fields.

  1. Is there API (workarounds) to link 2 entities by their ids? E.g. without loading parent and call setChild.
  2. Is there a way to load parent with all lazy fields even if some of them defined as eager? As I have searched that hibernate supports overriding for loading types. E.g. it is possible write Criteria which loaded only selected fields. But this is not comfortable way, because new entity classes can be added with eager type.
  3. Can I load a projection with same child, set children to it and then persist it like entity class?
Rish
  • 1,303
  • 9
  • 22
Cherry
  • 31,309
  • 66
  • 224
  • 364

2 Answers2

4

If you have a parent id and a child has an association with a parent by a foreign key you can use a fake parent

Parent parent = new Parent();
parent.setId(parentId);
child.setParent(parent);
// save child

If you want to load a parent with lazy fields you should make all parent's fields lazy and fetch them with join fetch or load by separate requests. If you will not use loaded parent to save data (for an example you can construct a new parent with the same id after user edit data) you can use partial object loading with a custom transformer as described here.

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • Earlier It was working, Just I changed the database. It's not. MySql 5.7 currently using – Faiz Akram Oct 28 '22 at 06:26
  • @FaizAkram It doesn't related to the database at all. Spring JPA uses `merge()` in place of `save()`, so such approach can lead to additional database query to load the parent. We can extend a repository to force to use `save()` of course. – v.ladynev Oct 28 '22 at 12:57
0

I hope this would be helpful for you.

If you have mapped your child and parent model classes and similarly in hibernate mapping xml file in such a way that without loading parent, child object can be saved, then definitely you can do your task.

Following is the sample code for you :-

Parent Class (Model)

 private Integer parentID;
 private String parentName;
 private Child childDetails;

..  getters and setters  ..

Child Class (Model)

private Integer childID;
private String childName;
private Parent parentDetails;

..  getters and setters  ..

Hibernate Mapping File

<class name="Model.Parent" table="Parent">
  <id name="parentID" type="integer">
      <generator class="increment"></generator>
  </id>
  <property column="parentName" name="parentName" type="string"/>
  <one-to-one class="Model.Child" name="childDetails" />
</class>

<class name="Model.Child" table="Child">
  <id name="childID" type="integer">
      <generator class="increment"></generator>
  </id>
  <property column="childName" name="childName" type="string"/>
  <many-to-one class="Model.Parent" column="parentID" name="parentDetails" unique="false" not-null="true" lazy="false"/>
</class>

how to save child without saving parent

SessionFactory sf = HibernateUtil.getSessionFactory();
org.hibernate.Session ss = sf.openSession();
Transaction tx = ss.beginTransaction();
Parent p = new Parent();
Child c = new Child();
c.setChildName("XYZ");
c.setParentDetails(p);
ss.save(c);
tx.commit();
ss.close();
IgniteCoders
  • 4,834
  • 3
  • 44
  • 62
Rish
  • 1,303
  • 9
  • 22
  • If you save the child without save the parent will throw a foreign key exception because child refer to a inexistente parent row. – IgniteCoders May 12 '16 at 16:39