Reputation: 21
I'm pretty new to programming, so please have mercy :)
I have to work on a project with Spring and I currently have some struggles on how to use the findBy Method.
I want to create a folder-system, where you can see every folder other users shared by setting the private boolean shared = true;
Next thing I did was to edit the service class of "folder" and I want to write something like this:
public List<Folder> getsharedFolder() {
return folderRepository.findBy___();
}
How can I exactly get folders where the boolean "shared" was set to true?
Upvotes: 1
Views: 4819
Reputation: 416
From what I know of the "findBy" method the function will be able to work if you set a boolean as your criterion for search (refer to this website for documentation: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/FindBy.html).
However, you could create an attribute that is the String variation of the boolean value (as in keep a string value for true/false). The findBy method should work fine in that instance.
If you don't wish to add an extra component you could try inheriting the class and then overriding the method (create a superclass) that will also check for the boolean component (maybe extend the functionality?). However, I'm not sure how you would implement that and it's more an abstract idea than a real suggestion
Upvotes: 0
Reputation: 1673
JPARepository
instead of CRUDRepository
@Entity
public class Folder {
@Id
@GeneratedValue
private Integer FolderID;
@Column(unique = true)
private String Foldername;
private boolean geteilt = false;
public Integer getFolderID() {
return FolderID;
}
public void setFolderID(Integer folderID) {
FolderID = folderID;
}
public String getFoldername() {
return Foldername;
}
public void setFoldername(String foldername) {
Foldername = foldername;
}
public boolean isGeteilt() {
return geteilt;
}
public void setGeteilt(boolean geteilt) {
this.geteilt = geteilt;
}
}
public interface FolderRepository extends JpaRepository<Folder, Integer>{
List<Folder> findByGeteilt(boolean shared);
}
@RunWith(SpringRunner.class)
@DataJpaTest
public class FolderTest{
@Autowired
private TestEntityManager entityManager;
@Autowired
private FolderRepository folderrepo;
@Test
public void whenFindByshared() {
// given
Folder folder = new Folder();
folder.setFolderID(1);
folder.setFoldername("folder1");
folder.setGeteilt(true);
Folder folder1 = new Folder();
folder1.setFolderID(2);
folder1.setFoldername("folder2");
folder1.setGeteilt(true);
Folder folder2 = new Folder();
folder2.setFolderID(1);
folder2.setFoldername("folder3");
folder2.setGeteilt(false);
folderrepo.saveAndFlush(folder);
folderrepo.saveAndFlush(folder1);
folderrepo.saveAndFlush(folder2);
// when
List<Folder> sharedFolders =new ArrayList<>();
sharedFolders = folderrepo.findByGeteilt(true);
assertThat(sharedFolders.get(0).getFoldername())
.isEqualTo(folder1.getFoldername());
}
}
Upvotes: 0
Reputation: 19120
You can add this method:
List<Folder> findBySharedTrue();
On your FolderRepository
class:
public interface FolderRepository extends CrudRepository<Folder, Integer> {
List<Folder> findBySharedTrue();
}
It will use the field shared
from the entity Folder
and return only the folders with shared
field equals to true
.
Upvotes: 3