Hidden and static filter on all repository methods for an entity - spring

Disclaimer: Please do not suggest database-model redesigns / database model concept flaws. This question is about the case described, there is no way to change that case apprently.
Assuming i hava a table fruits, with name, color and type as fields
This means, all apples have the type=apple, all oranges have type=orange and so on.
Now creating a JPA entity/model called Apple with those 3 fields mapped and the Table=fruits, i want to create a AppleRepository where the argument type is statically set to apple and not visible to the outer consumers.
So i do not want to offer
AppleRepository.findByTypeAndNameContains with requiring the caller to now to set type to apple
but rather just
AppleRepository.findByNameContains while type is statically set to apple.
(With the same method i would then chose to Orange and the OrangeRepository using type=orange)
I understand i could use #Query to write a custom query where type is just statically included and only mapping the dynamic parameters... but
Question:
Is there a elegant way to implement that without using #Query?

What you have is the principle behind the "singe table" inheritance strategy. It consists in storing all the entities in a hierarchy inside a single table, and distinguishing them using a discriminator column (i.e. your type column).
See the documentation for details

Related

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

Handling Relational Data Input

When creating an MVC application with a "Create" view for a particular entity and I want to relate it to another entity I could use a dynamic drop down menu.
However when the possible items is larger than 10 (for example) the drop down does not seem to offer the best user experience.
What is the recommended way to handle the input of a relationship between entities? A textbox that validates against the possible entities?
A textbox that validates against the possible entities?
That is pretty much the answer. The general idea would be to have a controller method that takes a query string and checks against the list of valid entities and returns the entities that match the query. The user can then choose from that filtered list.
You don't have to build it from scratch if you don't want to. Take a look at something like https://github.com/twitter/typeahead.js. There is also https://select2.github.io. However, there are probably lots of choices for that type of control.

Hbase Schema Nested Entity

Does anyone have an example on how to create an Hbase table with a nested entity?
Example
UserName (string)
SSN (string)
+ Books (collection)
The books collection would look like this for example
Books
isbn
title
etc...
I cannot find a single example are how to create a table like this. I see many people talk about it, and how it is a best practice in certain scenarios, but I cannot find an example on how to do it anywhere.
Thanks...
Nested entities isn't an official feature of HBase; it's just a way some people talk about one usage pattern. In this pattern, you use the fact that "columns" in HBase are really just a big map (a bunch of key/value pairs) to let you to model a dimension of cardinality inside the row by adding one column per "row" of the nested entity.
Schema-wise, you don't need to do much on the table itself; when you create a table in HBase, you just specify the name & column family (and associated properties), like so (in hbase shell):
hbase:001:0> create 'UserWithBooks', 'cf1'
Then, it's up to you what you put in it, column wise. You could insert values like:
hbase:002:0> put 'UsersWithBooks', 'userid1234', 'cf1:username', 'my username'
hbase:003:0> put 'UsersWithBooks', 'userid1234', 'cf1:ssn', 'my ssn'
hbase:004:0> put 'UsersWithBooks', 'userid1234', 'cf1:book_id_12345', '<isbn>12345</isbn><title>mary had a little lamb</title>'
hbase:005:0> put 'UsersWithBooks', 'userid1234', 'cf1:book_id_67890', '<isbn>67890</isbn><title>the importance of being earnest</title>'
The column names are totally up to you, and there's no limit to how many you can have (within reason: see the HBase Reference Guide for more on this). Of course, doing this, you have to do your own legwork re: putting in and getting out values (and you'd probably do it with the java client in a more sophisticated way than I'm doing with these shell commands, they're just for explanatory purposes). And while you can efficiently scan just a portion of the columns in a table by key (using a column pagination filter), you can't do much with the contents of the cells other than pull them and parse them elsewhere.
Why would you do this? Probably just if you wanted atomicity around all the nested rows for one parent row. It's not very common, your best bet is probably to start by modeling them as separate tables, and only move to this approach if you really understand the tradeoffs.
There are some limitations to this. First, this technique only works to
one level deep: your nested entities can’t themselves have nested entities. You can still
have multiple different nested child entities in a single parent, and the column qualifier is their identifying attributes.
Second, it’s not as efficient to access an individual value stored as a nested column
qualifier inside a row, as compared to accessing a row in another table, as you learned
earlier in the chapter.
Still, there are compelling cases where this kind of schema design is appropriate. If
the only way you get at the child entities is via the parent entity, and you’d like to have transactional protection around all children of a parent, this can be the right way to go.

Changing the model's attributes - adding or removing attributes

I am working on a MVC3 code first web application and after I showed the first version to my bosses, they suggested they will need a 'spare' (spare like in something that's not yet defined and we will use it just in case we will need it) attribute in the Employee model.
My intention is to find a way to give them the ability to add as many attributes to the models as they will need. Obviously I don't want them to get their hands on the code and modify it, then deploy it again (I know I didn't mention about the database, that will be another problem). I want a solution that has the ability to add new attributes 'on the fly'.
Do any of you had similar requests and if you had what solution did you find/implement?
I haven't had such a request, but I can imagine a way to get what you want.
I assume you use the Entity Framework, because of your tag.
Let's say we have a class Employee that we want to be extendable. We can give this class a dictionary of strings where the key-type is string, too. Then you can easily add more properties to every employee.
For saving this structure to the database you would need two tables. One that holds the employees and one that holds the properties. Where the properties-table has a foreign-key targeting the employee-table.
Or as suggested in this Q&A (EF Code First - Map Dictionary or custom type as an nvarchar): you can save the contents of the dictionary as XML in one column of the employee table.
This is only one suggestion and it would be nice to know how you solved this.

Default Sort Column with Linq to SQL

I am in the process building myself a simple Linq to SQL repository pattern.
What I wanted to know is, is it possible to set a default sort column so I don't have to call orderby.
From what I have read I don't think it is and if this is the case what would recommend for a solution to this problem.
Would the best idea be to use an attribute on a partial class on my model?
the default order is the clustered index on the table you are pulling from.
What are you wanting to sort on (without sorting on) ?
If you needed something other than having it sorted by the primary key, you could look at supplying a select statement for the table instead of using the runtime generated statement. Look at the properties on the table in the designer -- you should be able to override the runtime generated select, delete, and update statements. I don't personally recommend this, though, since I'm not sure how it will interact with other orderings. I think the intent is more along the lines of allowing you to use stored procedures if you want.
Another alternative would be to create a table-valued function or stored procedure that does the ordering the way you want and has the same schema as the table. If, in the designer, you drag this onto the table, you get a strongly typed method on the data context that you can use to obtain those entities according to the definition of the function/procedure instead of the standard select. Personally I think this introduces fewer maintenance headaches because it makes it more visible, but you do have to remember to use the method instead of the Table property for that entity.

Resources