How to create custom SpringBoot Crud Repository Methods to find specific details? - spring-boot

So i have 5 tables as Users,Orders,Products,Ingredients and nutrients,
Each is connected to adjacent table via #ManyToOne annotation.
What i am stuck at is how to find product details of a particular user for a particular order and similarly finding ingredients for a particular product for a particular order for a particular user and similarly for nutrients.
I know i have to declare custom crud methods according to a given syntax and i did so but still the method is not working.
Here is the link to the git repository
https://github.com/Guneet007/API.git

You can use Specification/Querydsl for defining custom criteria. Because we should use more than 3 parameters with AND suffix.
One more issue I have found in your code is that your property name not match with your method findByProductId() it should be findByProduct(Product product). Look same for others too.
https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

Related

How to add another table column in searchable array in nova?

i am getting data from different different tables and i displayed all those in one section that section contains search button also, i want to add another resource field as a searchable item in nova ,i am trying to add based on the relation of the particular table but it's throwing an error undefined column, How to add please help..
public static $searchable=['id','name','account.type'];
Basically, the search by relationship is not possible through Nova.
A package well made for your needs is this one: LaravelNovaSearch
This package includes both relationship search but also has other useful features making the search much more comprehensive.
Another package, which I have not personally tested, but which seems to be useful for your needs: NovaSearchRelations
My advice is to use LaravelNovaSearch, more maintained and more complete.

How does beforeSaveAddressInformation plugin

I was looking at this post: Magento 2 Add a custom input field on chackout forrm and save it
I have tried the same way but I am not sure how this part came
$customField = $shippingAddressExtensionAttributes->getFiscalCode();
$shippingAddress->setFiscalCode($customField);
Where is this getFiscalCode?
It's a Custom AttributeCode.
the getFiscalCode() method is an accessor method that is used to identity fields such as the one that is created with the extension attribute fiscal_code mentioned in the example.
It is not just extension attributes, but all collection models in magento use the following camel casing structure where for example the column entity_id in database table, can be accessed via either $modelCollection->getEntityId() or $modelCollection->setEntityId($valueOfEntityId)
This is a good read to understand the idea Magento 2: CRUD Models for Database Access

How to have a User Account for different entities in Spring JPA?

I'm making a system (using Spring + JPA with MySQL) that shows the best applicants for a certain job offer. The company and the applicants have their respective user account, and with that, they can fill in their personal/company information and their job profile/job offer conditions. With that, the system should match the job conditions (like 3+ years of experience in C) with the applicant's job profile.
My problem is that the User Account is created first, and should be independent, but these two different entities (Applicant and Company), with different attributes, are using it. So if I do something like create an applicant and company in the User Account, one of them will be always null.
How can I solve this? I guess the problem would be something like: how to implement a user account that can hold data from different entities that have different attributes (therefore, can't be grouped)? (In fact, I need one more entity, but I tried to simplify it to illustrate the problem more clearly).
I think, you should make marker interface, like public interface UserAccountable or smth. Implement this interface in your Applicant and Company classes. Then you can make a field in UserAccount class, like private UserAccountable someUser; and throught setters and getters you can assign and get this variable to Applicant or Company.
Hope this helps!
I found what I needed here: https://thoughts-on-java.org/complete-guide-inheritance-strategies-jpa-hibernate/
The problem was the mapping, not the class design per se. I could create interfaces and abstract classes to solve it in the Java world, but in the SQL world that's not possible, so the mapping is the key. In this case, I was looking for the Joined table mapping, but I realized I needed it just to not have null fields in my UserAccount, because I don't need a polymorphic query (e.g. give me the names of every 'user type' (Person, Company)), and it would be too costly performance wise to implement it that way, so I'll trade off space for performance, and I'll just reference all three user types in the User Account, leaving two of those three fields null forever.
PS: Single table mapping won't help because I do need to use not null conditions.

Spring JPA custom query with combination of parameters in WHERE condition?

How to write JPA query in Spring Data that uses at least one of three parameters?
I have these three parameters:
Id (PK)
Name
Surname
Client must supply at least one of these three parameters and I want to find user by these not-empty parameters.
Is it possible to create such custom query to my repository or do I have to create custom queries for all possible combination of WHERE conditions?
You can have your repository extend org.springframework.data.querydsl.QueryDslPredicateExecutor and use the inherited findAll(Predicate predicate) method to query using any combination of parameters.
You would not then have to write any query methods:
http://docs.spring.io/spring-data/jpa/docs/1.10.5.RELEASE/reference/html/#core.extensions.querydsl
You can also have the Predicate automatically bound in a Spring MVC Controller as detailed here:
https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling#querydsl-web-support
and here:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web.type-safe
So your controller can then automatically handle a search with 1,2 or all 3 parameters passed as request parameters without your having to write any code at all.
This is where you need the ability to create dynamic queries at runtime.
In your application code, you should have logic to build the predicate based on whether each of the above properties from the input DTO is empty or not.
One way to do it is to use QueryDSL.
To use QueryDSL, you should include the relevant dependency in your pom/gradle file and then your repository should extend the QueryDslPredicateExecutor interface. This will give you two additional generic finder methods.
T findAll(Predicate) or
Page<T> findAll(Predicate, Pageable)
One thing to keep in mind is that the above two methods are appropriate where this is no need to do a join
Your requirement here seems to be a single table query, so either one of the finder methods should suffice.
If you do need to do a join with other tables and need fine grained control over how the JOIN happens (INNER JOIN vs LEFT OUTER JOIN vs CROSS JOIN) etc, then you should consider creating a Custom repository that your repository can extend.
And then provide your own customImpl that you can now access from the repository.
thanks a lot for the reply ... as i read Spring is not easy flexible as other framework... for i.e i tried some node js api framework and it is easier and more flexible .. what do you think about this ?

How do I use Spring Roo for referenced data

I’m new to Spring Roo and looking to build an application. I need to have combo boxes to select values into string fields – a fairly common requirement. The Spring Roo Pizza example shows this using enumerated data types enum constant, but I require the source values to be editable by an admin (ie in a table) so we can change the values in the future. Further, my preference would be to use a single table to contain all these lists for easier maintenance.
I know the SQL I want to generate the list would be something like:
Select listvals FROM listTable WHERE listtype = “status”;
Then, my dropdown box would show something like: Active, Inactive. The user would select one, and the string “Active” would be stored in the target field.
And in a second example, we might use:
Select listvals FROM listTable WHERE listtype = “State”;
The second dropdown box would show something like: Alaska, California, Florida. The user would select one, and the string “Florida” would be stored in the target field.
My CORE question is how does one achieve this sort of function in Roo?
Using the Pizza Shop quick start as a sandbox I have tried defining the target fields such as:
I changed: field reference --fieldName base --type ~.domain.Base to:
field reference --fieldName base --type ~.domain.Base --referencedColumnName name
Which returned this error:
#JoinColumn name is required if specifying a referencedColumnName
The initial problem is that at this point roo has yet to create the row ID columns for the db, so I don’t know the name of the join column on the Base table. But, if I wait until after I run the script with a 1:M join, the column pizza.base will be defined as an integer, and not the string that I want.
So, I ran the vanilla pizza shop roo script and interrogated the vanilla db. (Does roo generate an SQL script for db creation that I could look at?)
As it turns out, roo names the row id column “id” as a BigInt. (I also note that it doesn't seem to make use of the SEQUENCE feature that postgres recommends for primary indices / row Ids.)
So now I run:
field reference --fieldName base --type ~.domain.Base --referencedColumnName name --joinColumnName id
Roo likes this!
Until I perform tests where it throws out a number of undecipherable errors in the Surefire reports.
I note that solving this problem is only step 1 to meeting my overall requrement described above. Step 2 will be to try to inject some sort of filter or where clause into the reference statement. I suspect that this has to do with the --fetch option (Roo support docs (http://docs.spring.io/autorepo/docs/spring-roo/1.2.5.RELEASE/reference/html/command-index.html#command-index-finder-commands - The fetch semantics at a JPA level; no default value)
But, I can’t find an example of this to see if I’m on the right track or to model my ‘fetch semantics’ – whatever those are.
Another possibility might be to use field list to define a class containing my list of dropdown values. This has a similar modifier --fetch, but again I can’t find any examples.
I’d really appreciate some help in answering my CORE question above.
THANKS!
Fetch parameter indicates whether the association should be lazily loaded or must be eagerly fetched (https://docs.oracle.com/javaee/6/api/javax/persistence/ManyToOne.html#fetch()).
Maybe the error of tests can be because you try to find, create, delete or update elements with related elements that not exists. Check this first of all.
You can see an example of application created with gvNIX (distribution of Spring Roo) that contains relationships between some entities on https://github.com/DISID/gvnix-samples/blob/master/quickstart-app/quickstart.roo

Resources