Answer the question
In order to leave comments, you need to log in
How to add an entity to a collection with a One-To-Many relationship without fetching the entire collection from the db?
Good afternoon. Started to deal with JPA-Hibernate. For one of my questions, I could not find an answer in the open spaces of the network. Actually, there are two questions. And so, in order.
There are two entities:
1)
@Entity
public class Category implements Serializable {
@Id
String categoryId;
String name;
@OneToMany
List<SubCategory> subCategories = new ArrayList<SubCategory>();
@Entity
public class SubCategory {
@Id
String subCategoryId;
String subCategoryName;
@Transactional
@Repository ("subCategoryDao")
public class JpaSubCategoryDaoImpl implements SubCategoryDao {
@PersistenceContext
EntityManager em;
public SubCategory createSubCategory(SubCategory subCategory, String categoryId) {
Category category = em.getReference(Category.class, categoryId);
category.getSubCategories().add(subCategory);
em.persist(subCategory);
return subCategory;
}
Hibernate:
select
category0_.categoryId as category1_0_0_,
category0_.name as name2_0_0_
from
Category category0_
where
category0_.categoryId=?
Hibernate:
select
subcategor0_.Category_categoryId as Category1_1_0_,
subcategor0_.subCategories_subCategoryId as subCateg2_1_0_,
subcategor1_.subCategoryId as subCateg1_3_1_,
subcategor1_.subCategoryName as subCateg2_3_1_
from
Category_SubCategory subcategor0_
inner join
SubCategory subcategor1_
on subcategor0_.subCategories_subCategoryId=subcategor1_.subCategoryId
where
subcategor0_.Category_categoryId=?
Hibernate:
insert
into
SubCategory
(subCategoryName, subCategoryId)
values
(?, ?)
Hibernate:
insert
into
Category_SubCategory
(Category_categoryId, subCategories_subCategoryId)
values
(?, ?)
Hibernate:
select
category0_.categoryId as category1_0_0_,
category0_.name as name2_0_0_
from
Category category0_
where
category0_.categoryId=?
Hibernate:
select
subcategor0_.Category_categoryId as Category1_1_0_,
subcategor0_.subCategories_subCategoryId as subCateg2_1_0_,
subcategor1_.subCategoryId as subCateg1_3_1_,
subcategor1_.subCategoryName as subCateg2_3_1_
from
Category_SubCategory subcategor0_
inner join
SubCategory subcategor1_
on subcategor0_.subCategories_subCategoryId=subcategor1_.subCategoryId
where
subcategor0_.Category_categoryId=?
Hibernate:
insert
into
SubCategory
(subCategoryName, subCategoryId)
values
(?, ?)
Hibernate:
delete
from
Category_SubCategory
where
Category_categoryId=?
Hibernate:
insert
into
Category_SubCategory
(Category_categoryId, subCategories_subCategoryId)
values
(?, ?)
Hibernate:
insert
into
Category_SubCategory
(Category_categoryId, subCategories_subCategoryId)
values
(?, ?)
Answer the question
In order to leave comments, you need to log in
After some amount of meditation:
The answer to my first question is no. The collection will always get as soon as you try to access it. The output is well described here .
Second question. If nevertheless to do through association One to Many. That needs to be done not through JoinTable
, which is the default, but through @JoinColumn
. Then the sql for any iteration of adding an object will look like this:
Hibernate:
insert
into
SubCategory
(subCategoryName, subCategoryId)
values
(?, ?)
Hibernate:
update
SubCategory
set
subCategories_categoryId=?
where
subCategoryId=?
I can’t say for sure, since the beginner himself did not ask this question. But there is a suggestion, try it? The SubCategory must have a field referring to the parent - Category categoryId with the annotation "@ManyToOne(orphanRemoval=true)". When creating an entity, you specify the parent and do the persistence of the entity without calling the parent in any way. Check it out yourself, it became interesting) True, there is no way to try it out yet
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question