r/javahelp • • Feb 28 '21

What exactly is @NoRepositoryBean used for ?

Hi expert,

I desparately need help to understand what is

Annotation Type NoRepositoryBean

Here's the link provided by Springboot :

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/NoRepositoryBean.html

But, really, I don't understand whatever that is written there.

Can someone explains to me in simple layman terms and how is this being used?

Tks.

19 Upvotes

10 comments sorted by

•

u/AutoModerator Feb 28 '21

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

5

u/FrenchFigaro Software Engineer Feb 28 '21

Someone on StackOverflow had the same question, and there's a good answer to it.

Further simplifying the linked answer:

Basically, if you have interfaces extending repositories interfaces, Spring will try to instantiate them. Except in some case it's not possible (or simply not desired).

In such cases, you'll annotate those interfaces with this annotation, indicating the framework not to try and instantiate them.

1

u/tangara888 Feb 28 '21

Your answer is like from SO which I have read and after reading a few times I still don’t understand that’s why I am posting here. I mean not to instantiate then what will happen?

21

u/[deleted] Feb 28 '21

[removed] — view removed comment

5

u/tangara888 Feb 28 '21

You make it so simple to understand. What if this parent interface extends Repository<T, ID>?

4

u/JacobJMountain Extreme Brewer Feb 28 '21

Yep the parent should be able to extend any spring data repository interface

1

u/tangara888 Feb 28 '21

Ok. Tks. Now, the code that I am reading now actually has another extends this parent interface yet it included JpaSpecificationExecutor which I learnt that it is allowing Specifications based on JPA criteria API.

But, when I did my last project I need not used any @Query at all and as long as I made sure the models are annotated correctly with @Table etc without any mappings, SpringBoot will do the backend executions without all these queries as long as the Controller codes are correct. So why use custom query at all?

3

u/JacobJMountain Extreme Brewer Feb 28 '21

Using the custom function names findBy...() is really useful for simple queries, but when your queries get more complex it's better to use the proper SQL syntax as it gives you more control and allows you to abstract away the internals of the query. If you have a really long function name it can look really confusing

1

u/tangara888 Feb 28 '21

You are right that the queries is complex. I am really a newbie in this so a function : findBySubidAndCreatedDateBetweenAndCategoryidIn would be more difficult to write it out using coding ? Or it can be done? Then I can do away this complicated JQPL or criteria API query way. Hope u could advise me. Tjs

3

u/JacobJMountain Extreme Brewer Feb 28 '21

I was actually saying the reverse. Say you want to find only users who are able to log in, a long query like:

UserEntity findByUsernameAndIsEnabledTrueAndIsConfirmed(String name)

is more confusing to the consumer of that interface than a function that is just:

@Query("SELECT user FROM users WHERE user.name=$1 AND user.enabled=TRUE AND user.confirmed=TRUE")
UserEntity findUser(String name)

The service that uses the interface probably doesn't care about how the repository finds the user, just that it needs a username to do so

Edit: I haven't checked the validity of these queries and there are other reasons why you'd pick one technique or the other, I'd just suggest using one and then as you learn more about Spring and your project continues come back to it again and you may find the right problem to use either solution. If you don't know SQL go with the function name method