Hibernate fetch subset of entities
up vote
0
down vote
favorite
In a project I have 2 REST controllers, one intended to be used by third parties and one intended to be used by internal staff. Both of the controllers return the same entities, but the third parties get a filtered subset of what the internal staff see. The filtereing is done based on an entity-type field of the entity.
My current implementation is as follows
@Entity
@Table("TABLENAME")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("0")
@DiscriminatorValue("0")
public class Entity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EntitySeq")
@SequenceGenerator(name = "EntitySeq", sequenceName = "ENTITY_SEQ", allocationSize = 20)
private Long id;
//@Column("ENTITY_TYPE")
//private EntityType entityType; //EntityType is an enumerator
}
@Entity
@Table("TABLENAME")
@Where("ENTITY_TYPE in ('A', 'B', 'C')
@DiscriminatorValue("00") //Found at https://stackoverflow.com/questions/6572814/single-table-inheritance-without-discriminator-column
public class LimitedEntity extends Entity {}
The repository is written as followed:
public interface EntityRepository<T extends Entity> extends PagingAndSortingRepository<T, long> {
List<T> findAll();
List<T> findAll(Date from, Date to, Pageable pageable);
}
To test this all, I've written a test as follows:
@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles({"local", "h2db"})
public class EntityRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private EntityRepository<Entity> entityEntityRepository;
@Autowired
private EntityRepository<LimitedEntity> limitedEntityEntityRepository;
@Before
public void setup() {
//Insert a record in the database for each possible value of entityType
}
@Test
public void vacatureTypeFilterTest() {
Date from = new GregorianCalendar(2018, 11, 5).getTime();
Date to = new GregorianCalendar(2018, 11, 6).getTime();
Assertions.assertThat(entityEntityRepository.countAllVacatures(from , to)).isEqualTo(EntityType.values().length);
Assertions.assertThat(limitedEntityEntityRepository.countAllVacatures(from , to)).isEqualTo(3);
}
}
As written in the test, I was expecting that the results would be 9 and 3 respectively, but it turns out they're both 9.
Why is this not working as expected? What is the/a proper way to have global filtering?
hibernate jpa inheritance spring-data-jpa spring-data
|
show 1 more comment
up vote
0
down vote
favorite
In a project I have 2 REST controllers, one intended to be used by third parties and one intended to be used by internal staff. Both of the controllers return the same entities, but the third parties get a filtered subset of what the internal staff see. The filtereing is done based on an entity-type field of the entity.
My current implementation is as follows
@Entity
@Table("TABLENAME")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("0")
@DiscriminatorValue("0")
public class Entity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EntitySeq")
@SequenceGenerator(name = "EntitySeq", sequenceName = "ENTITY_SEQ", allocationSize = 20)
private Long id;
//@Column("ENTITY_TYPE")
//private EntityType entityType; //EntityType is an enumerator
}
@Entity
@Table("TABLENAME")
@Where("ENTITY_TYPE in ('A', 'B', 'C')
@DiscriminatorValue("00") //Found at https://stackoverflow.com/questions/6572814/single-table-inheritance-without-discriminator-column
public class LimitedEntity extends Entity {}
The repository is written as followed:
public interface EntityRepository<T extends Entity> extends PagingAndSortingRepository<T, long> {
List<T> findAll();
List<T> findAll(Date from, Date to, Pageable pageable);
}
To test this all, I've written a test as follows:
@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles({"local", "h2db"})
public class EntityRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private EntityRepository<Entity> entityEntityRepository;
@Autowired
private EntityRepository<LimitedEntity> limitedEntityEntityRepository;
@Before
public void setup() {
//Insert a record in the database for each possible value of entityType
}
@Test
public void vacatureTypeFilterTest() {
Date from = new GregorianCalendar(2018, 11, 5).getTime();
Date to = new GregorianCalendar(2018, 11, 6).getTime();
Assertions.assertThat(entityEntityRepository.countAllVacatures(from , to)).isEqualTo(EntityType.values().length);
Assertions.assertThat(limitedEntityEntityRepository.countAllVacatures(from , to)).isEqualTo(3);
}
}
As written in the test, I was expecting that the results would be 9 and 3 respectively, but it turns out they're both 9.
Why is this not working as expected? What is the/a proper way to have global filtering?
hibernate jpa inheritance spring-data-jpa spring-data
This doesn't make any sense. Wouldn't it be a lot cleaner just to have one entity and 1 query on your repository which returns the subset? You controller/service can then delegate to either the inherited findAll() method of the repository or the query method as required.
– Alan Hay
Nov 6 at 19:31
@AlanHay I don't think so. It's not limited to 1 single query. There are multiple possible querries: getBulk, getChanges(from, until, filter), getDetail, ... If it's done via querries, I'll need to write every querry twice, once with filter on entitytype and once without. In addition, if querries are added in the future, we'll have to keep that filter in mind. I'm looking for a solution that works for no matter how many querries.
– Michaël Benjamin Saerens
Nov 7 at 8:23
Okay, so filtering globally is the question you really want to ask. en.wikipedia.org/wiki/XY_problem
– Alan Hay
Nov 7 at 8:27
I don't think so. Global filtering is already enabled with the@Where
annotation, but the test doesn't seem to confirm this. Unless I completely missed the intention of that annotation.
– Michaël Benjamin Saerens
Nov 7 at 8:32
Apart from the fact it doesn't work, there will be better solutions than the one you are proposing so you should ask question about how best to solve the problem, non one about why your current solution does not work.
– Alan Hay
Nov 7 at 8:37
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In a project I have 2 REST controllers, one intended to be used by third parties and one intended to be used by internal staff. Both of the controllers return the same entities, but the third parties get a filtered subset of what the internal staff see. The filtereing is done based on an entity-type field of the entity.
My current implementation is as follows
@Entity
@Table("TABLENAME")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("0")
@DiscriminatorValue("0")
public class Entity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EntitySeq")
@SequenceGenerator(name = "EntitySeq", sequenceName = "ENTITY_SEQ", allocationSize = 20)
private Long id;
//@Column("ENTITY_TYPE")
//private EntityType entityType; //EntityType is an enumerator
}
@Entity
@Table("TABLENAME")
@Where("ENTITY_TYPE in ('A', 'B', 'C')
@DiscriminatorValue("00") //Found at https://stackoverflow.com/questions/6572814/single-table-inheritance-without-discriminator-column
public class LimitedEntity extends Entity {}
The repository is written as followed:
public interface EntityRepository<T extends Entity> extends PagingAndSortingRepository<T, long> {
List<T> findAll();
List<T> findAll(Date from, Date to, Pageable pageable);
}
To test this all, I've written a test as follows:
@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles({"local", "h2db"})
public class EntityRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private EntityRepository<Entity> entityEntityRepository;
@Autowired
private EntityRepository<LimitedEntity> limitedEntityEntityRepository;
@Before
public void setup() {
//Insert a record in the database for each possible value of entityType
}
@Test
public void vacatureTypeFilterTest() {
Date from = new GregorianCalendar(2018, 11, 5).getTime();
Date to = new GregorianCalendar(2018, 11, 6).getTime();
Assertions.assertThat(entityEntityRepository.countAllVacatures(from , to)).isEqualTo(EntityType.values().length);
Assertions.assertThat(limitedEntityEntityRepository.countAllVacatures(from , to)).isEqualTo(3);
}
}
As written in the test, I was expecting that the results would be 9 and 3 respectively, but it turns out they're both 9.
Why is this not working as expected? What is the/a proper way to have global filtering?
hibernate jpa inheritance spring-data-jpa spring-data
In a project I have 2 REST controllers, one intended to be used by third parties and one intended to be used by internal staff. Both of the controllers return the same entities, but the third parties get a filtered subset of what the internal staff see. The filtereing is done based on an entity-type field of the entity.
My current implementation is as follows
@Entity
@Table("TABLENAME")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("0")
@DiscriminatorValue("0")
public class Entity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EntitySeq")
@SequenceGenerator(name = "EntitySeq", sequenceName = "ENTITY_SEQ", allocationSize = 20)
private Long id;
//@Column("ENTITY_TYPE")
//private EntityType entityType; //EntityType is an enumerator
}
@Entity
@Table("TABLENAME")
@Where("ENTITY_TYPE in ('A', 'B', 'C')
@DiscriminatorValue("00") //Found at https://stackoverflow.com/questions/6572814/single-table-inheritance-without-discriminator-column
public class LimitedEntity extends Entity {}
The repository is written as followed:
public interface EntityRepository<T extends Entity> extends PagingAndSortingRepository<T, long> {
List<T> findAll();
List<T> findAll(Date from, Date to, Pageable pageable);
}
To test this all, I've written a test as follows:
@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles({"local", "h2db"})
public class EntityRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private EntityRepository<Entity> entityEntityRepository;
@Autowired
private EntityRepository<LimitedEntity> limitedEntityEntityRepository;
@Before
public void setup() {
//Insert a record in the database for each possible value of entityType
}
@Test
public void vacatureTypeFilterTest() {
Date from = new GregorianCalendar(2018, 11, 5).getTime();
Date to = new GregorianCalendar(2018, 11, 6).getTime();
Assertions.assertThat(entityEntityRepository.countAllVacatures(from , to)).isEqualTo(EntityType.values().length);
Assertions.assertThat(limitedEntityEntityRepository.countAllVacatures(from , to)).isEqualTo(3);
}
}
As written in the test, I was expecting that the results would be 9 and 3 respectively, but it turns out they're both 9.
Why is this not working as expected? What is the/a proper way to have global filtering?
hibernate jpa inheritance spring-data-jpa spring-data
hibernate jpa inheritance spring-data-jpa spring-data
edited Nov 7 at 9:09
asked Nov 6 at 18:15
Michaël Benjamin Saerens
655420
655420
This doesn't make any sense. Wouldn't it be a lot cleaner just to have one entity and 1 query on your repository which returns the subset? You controller/service can then delegate to either the inherited findAll() method of the repository or the query method as required.
– Alan Hay
Nov 6 at 19:31
@AlanHay I don't think so. It's not limited to 1 single query. There are multiple possible querries: getBulk, getChanges(from, until, filter), getDetail, ... If it's done via querries, I'll need to write every querry twice, once with filter on entitytype and once without. In addition, if querries are added in the future, we'll have to keep that filter in mind. I'm looking for a solution that works for no matter how many querries.
– Michaël Benjamin Saerens
Nov 7 at 8:23
Okay, so filtering globally is the question you really want to ask. en.wikipedia.org/wiki/XY_problem
– Alan Hay
Nov 7 at 8:27
I don't think so. Global filtering is already enabled with the@Where
annotation, but the test doesn't seem to confirm this. Unless I completely missed the intention of that annotation.
– Michaël Benjamin Saerens
Nov 7 at 8:32
Apart from the fact it doesn't work, there will be better solutions than the one you are proposing so you should ask question about how best to solve the problem, non one about why your current solution does not work.
– Alan Hay
Nov 7 at 8:37
|
show 1 more comment
This doesn't make any sense. Wouldn't it be a lot cleaner just to have one entity and 1 query on your repository which returns the subset? You controller/service can then delegate to either the inherited findAll() method of the repository or the query method as required.
– Alan Hay
Nov 6 at 19:31
@AlanHay I don't think so. It's not limited to 1 single query. There are multiple possible querries: getBulk, getChanges(from, until, filter), getDetail, ... If it's done via querries, I'll need to write every querry twice, once with filter on entitytype and once without. In addition, if querries are added in the future, we'll have to keep that filter in mind. I'm looking for a solution that works for no matter how many querries.
– Michaël Benjamin Saerens
Nov 7 at 8:23
Okay, so filtering globally is the question you really want to ask. en.wikipedia.org/wiki/XY_problem
– Alan Hay
Nov 7 at 8:27
I don't think so. Global filtering is already enabled with the@Where
annotation, but the test doesn't seem to confirm this. Unless I completely missed the intention of that annotation.
– Michaël Benjamin Saerens
Nov 7 at 8:32
Apart from the fact it doesn't work, there will be better solutions than the one you are proposing so you should ask question about how best to solve the problem, non one about why your current solution does not work.
– Alan Hay
Nov 7 at 8:37
This doesn't make any sense. Wouldn't it be a lot cleaner just to have one entity and 1 query on your repository which returns the subset? You controller/service can then delegate to either the inherited findAll() method of the repository or the query method as required.
– Alan Hay
Nov 6 at 19:31
This doesn't make any sense. Wouldn't it be a lot cleaner just to have one entity and 1 query on your repository which returns the subset? You controller/service can then delegate to either the inherited findAll() method of the repository or the query method as required.
– Alan Hay
Nov 6 at 19:31
@AlanHay I don't think so. It's not limited to 1 single query. There are multiple possible querries: getBulk, getChanges(from, until, filter), getDetail, ... If it's done via querries, I'll need to write every querry twice, once with filter on entitytype and once without. In addition, if querries are added in the future, we'll have to keep that filter in mind. I'm looking for a solution that works for no matter how many querries.
– Michaël Benjamin Saerens
Nov 7 at 8:23
@AlanHay I don't think so. It's not limited to 1 single query. There are multiple possible querries: getBulk, getChanges(from, until, filter), getDetail, ... If it's done via querries, I'll need to write every querry twice, once with filter on entitytype and once without. In addition, if querries are added in the future, we'll have to keep that filter in mind. I'm looking for a solution that works for no matter how many querries.
– Michaël Benjamin Saerens
Nov 7 at 8:23
Okay, so filtering globally is the question you really want to ask. en.wikipedia.org/wiki/XY_problem
– Alan Hay
Nov 7 at 8:27
Okay, so filtering globally is the question you really want to ask. en.wikipedia.org/wiki/XY_problem
– Alan Hay
Nov 7 at 8:27
I don't think so. Global filtering is already enabled with the
@Where
annotation, but the test doesn't seem to confirm this. Unless I completely missed the intention of that annotation.– Michaël Benjamin Saerens
Nov 7 at 8:32
I don't think so. Global filtering is already enabled with the
@Where
annotation, but the test doesn't seem to confirm this. Unless I completely missed the intention of that annotation.– Michaël Benjamin Saerens
Nov 7 at 8:32
Apart from the fact it doesn't work, there will be better solutions than the one you are proposing so you should ask question about how best to solve the problem, non one about why your current solution does not work.
– Alan Hay
Nov 7 at 8:37
Apart from the fact it doesn't work, there will be better solutions than the one you are proposing so you should ask question about how best to solve the problem, non one about why your current solution does not work.
– Alan Hay
Nov 7 at 8:37
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53177666%2fhibernate-fetch-subset-of-entities%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This doesn't make any sense. Wouldn't it be a lot cleaner just to have one entity and 1 query on your repository which returns the subset? You controller/service can then delegate to either the inherited findAll() method of the repository or the query method as required.
– Alan Hay
Nov 6 at 19:31
@AlanHay I don't think so. It's not limited to 1 single query. There are multiple possible querries: getBulk, getChanges(from, until, filter), getDetail, ... If it's done via querries, I'll need to write every querry twice, once with filter on entitytype and once without. In addition, if querries are added in the future, we'll have to keep that filter in mind. I'm looking for a solution that works for no matter how many querries.
– Michaël Benjamin Saerens
Nov 7 at 8:23
Okay, so filtering globally is the question you really want to ask. en.wikipedia.org/wiki/XY_problem
– Alan Hay
Nov 7 at 8:27
I don't think so. Global filtering is already enabled with the
@Where
annotation, but the test doesn't seem to confirm this. Unless I completely missed the intention of that annotation.– Michaël Benjamin Saerens
Nov 7 at 8:32
Apart from the fact it doesn't work, there will be better solutions than the one you are proposing so you should ask question about how best to solve the problem, non one about why your current solution does not work.
– Alan Hay
Nov 7 at 8:37