Hello, I have a JPA parent class that I use for all of my entities:@MappedSuperclass@Access(AccessType.FIELD)public abstract class AbstractEntity<T> {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Long id;@Column(nullable = false)private Boolean isDeleted;
...}I am also making an AbstractDao which will have fetchById() and fetchAll() operations. All other operations specific to the child classes will be in their specific DAO class. How would you go about specifying the queries? This is what I have so far.public abstract class AbstractDao<T extends AbstractEntity<T>> {@PersistenceContextprivate EntityManager entityManager;public T fetchById(Long id, boolean isDeletedEntitiesIncluded) {QAbstractEntity entity = QAbstractEntity.abstractEntity;JPAQuery query = new JPAQuery(getEntityManager());BooleanExpression whereClause = entity.id.eq(id);
// Determine if we should filter out logically deleted entities.if (!isDeletedEntitiesIncluded) {whereClause = whereClause.and(entity.isDeleted.eq(false));
}// What do I return here?
}public List<T> fetchAll(boolean isDeletedEntitiesIncluded) {QAbstractEntity entity = QAbstractEntity.abstractEntity;JPAQuery query = new JPAQuery(getEntityManager());query = query.from(entity);// Determine if we should filter out logically deleted entities.if (!isDeletedEntitiesIncluded) {query = query.where(entity.isDeleted.eq(false));
}// Run the query// What to return here?
}}Thanks!--
Hello, I have a JPA parent class that I use for all of my entities:@MappedSuperclass@Access(AccessType.FIELD)public abstract class AbstractEntity<T> {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Long id;@Column(nullable = false)private Boolean isDeleted;
...}I am also making an AbstractDao which will have fetchById() and fetchAll() operations. All other operations specific to the child classes will be in their specific DAO class. How would you go about specifying the queries? This is what I have so far.
public abstract class AbstractDao<T extends AbstractEntity<T>> {@PersistenceContextprivate EntityManager entityManager;public T fetchById(Long id, boolean isDeletedEntitiesIncluded) {QAbstractEntity entity = QAbstractEntity.abstractEntity;JPAQuery query = new JPAQuery(getEntityManager());BooleanExpression whereClause = entity.id.eq(id);
// Determine if we should filter out logically deleted entities.if (!isDeletedEntitiesIncluded) {whereClause = whereClause.and(entity.isDeleted.eq(false));}
// What do I return here?}
public List<T> fetchAll(boolean isDeletedEntitiesIncluded) {QAbstractEntity entity = QAbstractEntity.abstractEntity;JPAQuery query = new JPAQuery(getEntityManager());query = query.from(entity);// Determine if we should filter out logically deleted entities.if (!isDeletedEntitiesIncluded) {query = query.where(entity.isDeleted.eq(false));}
// Run the query// What to return here?}
}Thanks!
Hi.
Some comments inline
Thanks Timo,I think I replied to myself previously on the message thread...oops. So, I tried it with just casting, and it crashed on me. Here are my methods.public T fetchById(Long id, boolean isDeletedFetched) {QAbstractEntity entity = QAbstractEntity.abstractEntity;
Hi.