You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a question about attaching entities to the persistence context when using projections. Below is an example of what I’m trying to achieve:
@AllArgsConstructor
@NoArgsConstructor
public abstract class AbstractBaseRepository<TEntity extends Product, TId> {
@Inject
BlazeCriteriaBuilderFactory cbf;
Class<TEntity> entityClass;
public TEntity attached(TId id) {
// This works as expected since the entity is attached to the persistence context.
TEntity product = cbf.create(entityClass)
.from(entityClass)
.where("id").eq(id)
.getSingleResult();
product.setName("changed name"); // Changes will be committed to the DB at the end of the transaction.
return product;
}
public TEntity notAttached(TId id) {
// Using an ObjectBuilder, the entity is not attached to the persistence context.
TEntity product = cbf.create(Tuple.class)
.from(entityClass)
.selectNew(new ObjectBuilder<TEntity>() {
@Override
public <X extends SelectBuilder<X>> void applySelects(X queryBuilder) {
queryBuilder
.select("id")
.select("name");
}
@Override
public TEntity build(Object[] tuple) {
Product prd = new Product();
prd.setId((String) tuple[0]);
prd.setName((String) tuple[1]);
return (TEntity) prd;
}
@Override
public List<TEntity> buildList(List<TEntity> list) {
return list;
}
})
.where("id").eq(id)
.getSingleResult();
product.setName("changed name"); // Changes will NOT be committed because the entity is detached.
return product;
}
}
The attached method works as expected because it directly fetches an entity attached to the persistence context. However, when using projections (e.g., in the notAttached method), the result is not attached since it is created using a custom ObjectBuilder.
What I Want to Achieve
I would like to return an entity when using projections while keeping it attached to the persistence context. Ideally, this entity should have null values for any fields that are not selected. Here's an example of what I need:
If not, is this limitation due to design? Am i right?
Blaze-Persistence can only do what Hibernate ORM allows, and partially loaded entities simply requires the use of bytecode enhancement with lazy groups, so there is nothing Blaze-Persistence can do.
Are there any best practices or workarounds to accomplish this use case?
Hi,
Thank you for this amazing library!
I have a question about attaching entities to the persistence context when using projections. Below is an example of what I’m trying to achieve:
The attached method works as expected because it directly fetches an entity attached to the persistence context. However, when using projections (e.g., in the notAttached method), the result is not attached since it is created using a custom ObjectBuilder.
What I Want to Achieve
I would like to return an entity when using projections while keeping it attached to the persistence context. Ideally, this entity should have null values for any fields that are not selected. Here's an example of what I need:
The response would look like this:
category
is ManyToOne relation in separate tableproduct_category_product
but not jsonB. ManyToMany also is used.Now it is failed because
Object[]
is returned but notTEntity
.My Questions
Any insights or suggestions would be greatly appreciated. Thanks in advance for your help!
FYI
I intentionally use
TEntity
to keepAbstractBaseRepository
generic and applicable to any entity type.The text was updated successfully, but these errors were encountered: