Is it possible to get last elements grouped with JPA and Spring boot - spring

I have a data set that looks like this:
name | rang
boo | 4
boo | 2
boo | 1
foo | 3
foo | 1
zoo | 2
zoo | 1
I want to select through spring boot jpa:
name | rang
boo | 4
foo | 3
zoo | 2
I do ofcause have a larger and more complex dataset, so the example is simplify.
I was hoping to find something like this:
#Repository
public interface FooRepository extends CrudRepository<FooDb, String> {
Set< FooDb > findAllByNameAndMaxDate(String name);
}

I think you may be able to do this by using the #Query annotation.
In your case it might look something like:
#Query("SELECT t.name, MAX(t.rang) FROM table t WHERE t.name=?1 GROUP BY t.name")
Set<FooDb> findAllByNameAndMaxDate(String name);
Without knowing what your data set looks like it's impossible for me to create the query here, but I hope this helps.

Related

Aerospike + Spring: Why does PK type always change to varchar?

Entity (very simple, for the test):
#Document(collection = "user_repo")
public class User {
#Id
private int id;
private String name;
private int age;
}
Repository
public interface AerospikeUserReactiveRepository extends ReactiveAerospikeRepository<User, Integer> {
}
Requested data from the database:
aql> select * from test
+-------------+----------------------------------------+------------------+-----------+
| PK | name | #_class | age |
+-------------+----------------------------------------+------------------+-----------+
| "761637962" | "21b50081-c244-4e98-9e5d-7b346f549154" | "com.model.User" | 761637962 |
| "626513063" | "7a171a11-b275-488b-ac29-e92b3f6f8668" | "com.model.User" | 626513063 |
| "422312771" | "c51ea6c5-840b-40eb-8616-e3447b363097" | "com.model.User" | 422312771 |
+-------------+----------------------------------------+------------------+-----------+
3 rows in set (0.053 secs)
You are right,
In Spring Data Aerospike the PK is stored as a String, initially it was done because:
It has an advantage of being able to have any type of key/change the key on application layer without changing persistence layer.
It gives readable format in Aerospike when you need to see data directly in storage.
Spring Data Aerospike supports Composite Keys so for saving them as a readable string the most simple approach is using to string conversion.
Currently you can change it by using Custom Converters.
We do consider to support storing primitive types as it is out-of-the-box, it's in our backlog.

can I use laravel 4 eloquent model or do I need to use db:query

Hi I am trying my first attempt to use ORM with Laravel. I have a big table from Drupal that I want to grab some records of and I need to join those with another table in Drupal to get the records that I care about manipulating.
Like so...
Node
----------------------------------------------------------
| Nid | type | misc other stuff | N
==========================================================
| 1 | Programs | Test Service | 1 |
----------------------------------------------------------
| 2 | Programs | Example Service | 1 |
----------------------------------------------------------
| 3 | Something else | Another Service | 1 |
----------------------------------------------------------
Fields
----------------------------------------------------------
| id | title | NID | tag |
==========================================================
| 1 | Blog Title 1 | 1 | THER |
----------------------------------------------------------
| 2 | Blog Title 2 | 2 | TES |
----------------------------------------------------------
| 3 | Blog Title 3 | 3 | ANOTHER |
----------------------------------------------------------
I want to get all the Nodes where type='Programs' and inner join those with all fields where NIDs are the same. Do I do that with an Eloquent ORM in app/model/node.php? Or a query builder statement $model=DB:table? what is the code for this? Or do I just do it in PHP?
You could do this with the ORM, but would have to override everything that makes it convenient and elegant.
Because you say you're trying to "manipulate" data in the fields table, it sounds like you're trying to update Drupal tables using something other than the Drupal field system. I would generally not recommend doing this—the Drupal field system is big, complicated, and special. There's a whole CMS to go with it.
You should move the data out of the old Drupal database and into your new database using seeds (http://laravel.com/docs/4.2/migrations#database-seeding).
Define a "drupal" database connection in your app/config/database.php, in addition to whatever you're using as a "default" connection for a new application. You can seed Eloquent models from an alternative connection in this manner:
<?php
// $nodes is an array of node table records inner joined to fields
$nodes = DB::connection('drupal')
->table('node')
->join('fields', 'node.nid', '=', 'fields.nid')
->get();
Pull the data out and put it in proper tables using Laravel migrations into normalized, ActiveRecord-style tables (http://laravel.com/docs/4.2/migrations#creating-migrations).
I prefer query builder, it's more flexible
DB::table('Node')
->join('Fields', 'Fields.NID', '=', 'Node.Nid')
->where('type', 'Programs')
->get();
Create two models in app/model (node.php and field.php) like this:
class Node extends \Eloquent {
protected $fillable = [];
protected $table = 'Node';
public function fields()
{
return $this->hasMany('Field');
}
}
class Field extends \Eloquent {
protected $fillable = [];
public function node()
{
return $this->belongsTo('Node');
}
}
Than you could do something like this:
$nodes = Node::with('fields')->where('type', 'Programs')->get();
You will get all your nodes with their relation where type is Programs.

Laravel 4: one to many by on multiple columns?

I'm making a table that essentially maps rows in a table to rows in another table where the structures are as follows:
|--- Words --| |- Synonyms -|
|------------| |------------|
| id | | id |
| en | | word_id |
| ko | | synonym_id |
| created_at | | created_at |
| updated_at | | updated_at |
|------------| |------------|
Now then, I know I can essentially have the words model have many Synonyms through a function like:
public function synonyms()
{
return $this->hasMany('Synonym');
}
No problem, but this method always gets it by the the word_id, and I would like to get it from word_id OR synonym_id that way I don't have to make multiple entries in the DB.
Is there anyway I can do this?
Check laravel docs Eloquent relationships. It would only get word_id because that's the only foreign key I believe.
Also why do you have synonym_id in your Synonyms table?
I believe you are looking for polymorphic relationship.
http://laravel.com/docs/eloquent#polymorphic-relations
I think your best bet is to create a many-to-many relationship with words on itself using the synonyms table as your pivot table.
Add this to your Word model.
public function synonyms()
{
return $this->belongsToMany('Word', 'synonyms', 'user_id', 'synonym_id');
}
Using it:
$word = Word::where('en', '=', 'someword')->first();
foreach($word->synonyms as $synonym) {
// This method would probably return the same word as a synonym of itself so we can skip that iteration.
if($synonym->en == $word->en) {
continue;
}
// Echo the synonym.
echo $synonym->en;
}
I'm a bit confused on you wanting to be able to find synonyms by the word_id or synonym_id but I think if you are using the many-to-many, it won't matter because if you know the synonym, it's still technically just a word, and you'd do the exact same thing.

Propel ORM - One-to-many relationship with parent name

Is it possible to create a parent name field into one-to-many relationship in Propel ORM.
This type of relationship uses in CRM systems.
Just imagine that we have a Task List. So, we created a Task #1 and related it to a Project.
Task #2 is related to Account (e.g. create a contract).
Task #3 is related to Bug Tracker (e.g. fix a bug).
So, we have the following relationships:
task_name | parent_name | parent_id
--------------------------------------------------
Start a project | Project | <project_id>
Create a contract | Account | <account_id>
Fix a bug | Bug Tracker | <bug_id>
Is it possible to implement in Propel. If no, could you recommend me another ORM with this feature.
The main purpose is to get a list of records with all relationship values.
For my example, it should look like (in JSON):
{
"Task_0":{"Id":1,"Name":"Start a project","ParentId":1,"ParentName":"Project","Project":{"Id":1,"Name":"Project-1","Tasks":{"Task_0":"*RECURSION*"}}},
"Task_1":{"Id":1,"Name":"Create a contract","ParentId":1,"ParentName":"Account","Account":{"Id":1,"Name":"Account-1","Tasks":{"Task_0":"*RECURSION*"}}},
"Task_2":{"Id":1,"Name":"Fix a bug","ParentId":1,"ParentName":"Bug","Bug":{"Id":1,"Name":"Bug-1","Tasks":{"Task_0":"*RECURSION*"}}}
}
Does anyone help me?
The output you have shown looks as if the toArray function has been used on the Propel objects and then the json_encode function. This should work if you define foreign keys mutually in Propel's schema.xml.
Since project tasks, account tasks and bug tracker tasks all have something in common, they all are tasks :), I would organize them as sub classes of a more general task entity.
You will end up with a collection of tables like this:
Table "task"
id | name
------------------------
1 | Start a project
2 | Create a contract
3 | Fix a bug
4 | Start another project
5 | Fix another bug
---------------------------------------
Table "bugtrack_task"
id | id_task
---------------
1 | 3
2 | 5
---------------------------------------
Table "project_task"
id | id_task
---------------
1 | 1
2 | 4
---------------------------------------
Table "account_task"
id | id_task
---------------
1 | 2
In the end, you would define a view in the schema.xml. This could look something like this:
<table name="view_task" phpName="ViewTask" skipSql="true" readOnly="true" description="All my tasks together for display">...</table>
Note that the skipSql attribute has been set to true. This will skip this view table when generating the SQL code. Propel will generate the classes for you but won't touch your database. You can now manually define the view yourself putting into it whatever you desire.
Of course you'd have to put some effort into creating this view but it pays off as you will be able to use the Propel classes like so for instance:
$tasks = ViewTask::create()->find();
$result = array();
foreach($tasks as $task) {
$result[] = $task->toArray();
}
return json_encode($result);
This isn't a complete answer but I hope you see the idea! Good luck :-)

rails activerecord column query

I am new to Ruby on Rails.
I get a simple table whose name is Loadtest.
The table have two attributes which are: testname, exectime.
The table may looks like this :
\-------------------------
testname | exectime |
test A | xx-xx-xx |
test B | xx-xx-xx |
\--------------------------
I want to get all the entries under testname, such as ["test A", "test B"]. How could I achieve this?
I could use Loadtest.find(:all) to get all the entries in the table and write some codes to form the testname array. But is there any direct method like Loadtest.column[:testname]?
Thanks for your time!
Yes, there is pluck method.
Loadtest.pluck(:testname)

Resources