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?










share|improve this question
























  • 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

















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?










share|improve this question
























  • 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















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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()