Jhipster ParentDTO does not have list of Child DTO - jhipster

am generating enties some thing like below, Person
entity Employee {
firstName String
lastName String
}
entity Role {
Name String
}
relationship OneToMany {
Employee{role} to Role{employee required}
}
The generated Employee does not include the RoleDTO. I would like it to return the following:
{
"id": 1,
"firstName": "John",
"lastName": "Doe",
"roles":
[
{
"id": 1
"name": "Admin"
},
{
"id": 2
"name": "Collector"
}
]
}
But the DTO generated are working other ways round.
Role is having List of EmployeeDTO's.
Manual Code change is one option,but in my project i have alsmot 40 entities and this mdoification has to be done for almost all entities. In that case Jhispter is generated code is not usefull any more.

Related

Can't get data for entity

I use jhipster to generate 2 entity Employee and Department
and relationship:
Employee.java
#ManyToOne
#JsonIgnoreProperties(value = "employees", allowSetters = true)
private Department department;
Department.java
#OneToMany(mappedBy = "department")
#Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<Employee> employees = new HashSet<>();
and when I called api/employees , I had:
{
"id": 1,
"code": "FU_EMP_DTP726",
"name": "Concrete system",
"birthDate": "2020-11-30",
"address": "Granite frame",
"phone": "0152104977",
"salary": 65309.0,
"department": {
"id": 1,
"code": "FU_DE_787778",
"name": "Tools"
}
}
but with api/departments , I had:
{
"id": 1,
"code": "FU_DE_787778",
"name": "Tools",
"employees": null
}
I didn't know why department's employees is null
In bidirectional one-to-many relationship case jhipster do not get all ‘child’ form “One” side,because “N+1” problem .
For your purpose you need to be modify the generated code。use EntityGraph
modify repository、add a “employees” Set into ” DepartmentDTO“ and Mapper
How to see both sides in one-to-many relationship generated by JHipsterHow to see both sides in one-to-many relationship generated by JHipster

How to joined 2 tables and output fields from the adjacent table?

everybody, could you please tell me
I use the objection.js (ORM) library for knex.js
joined Users and Roles tables, everything is great.
I use it:
const users = await User.query().eager().roles')
I'm getting it:
{"id":1, "email": "ann#mail.com", "password": "qwe", "role":1, "roles":{"id":1, "name": "admin"}}
But how can I get a flat structure? :
{"id":1, "email": "ann#mail.com", "password": "qwe", "role "admin"}
I would normally expect Objection to do this with a HasOneRelation relationship, so I'm going to assume that's what you have here:
{
"id": 1,
"email": "ann#mail.com",
"password": "qwe",
"role": 1,
"roles": {
"id":1,
"name": "admin"
}
}
If your users can only have one role at a time, that should be fine. To get a flat structure, you can either post-process it in JavaScript:
return {
...user,
role: roles.name
}
or create a virtual attribute:
export default class User extends Model {
static get virtualAttributes() {
return ['roleName'];
}
roleName() {
return this.roles.name;
}
// ...
}
This will not stop the roles object from being added, but it'll provide a first-level alias for the name in the JSON.

How to match and join results between two resolvers in one graphql query?

I have two resolver.
The one is Company resolve that return the company details like id, name and list of documents ids, like this example:
{
"data": {
"companyOne": {
"name": "twitter",
"documents": [
"5c6c0213f0fa854bd7d4a38c",
"5c6c02948e0001a16529a1a1",
"5c6c02ee7e76c12075850119",
"5c6ef2ddd16e19889ffaffd0",
"5c72fb723ebf7b2881679ced",
"5c753d1c2e080fa4a2f86c87",
...
]
}
}
}
And the another resolver gets me all the details of documents like this example:
{
"data": {
"documentsMany": [{
"name": "doc1",
"_id": 5c6c0213f0fa854bd7d4a38c,
}, {
"name": "doc2",
"_id": 5c6c02948e0001a16529a1a1,
},
...
]
}
}
How to match every data.companyOne.documents[id] to data.documentsMany[..]._id? in the query level? is it possible to do this graphql?
The expect results should be when I run the companyOne query (without change the code - just in the query level) it's should return with documents as object instead of array of string ids.
maybe something like?
query {
companyOne {
name,
documents on documentsMany where _id is ___???
}
}

JHipster - How to generate Entity with a field as List of String?

I'm using jhipster-generator 4.14.5 and im trying to generate an Entity with a field Persons. But Persons is a List of String List<String> Persons.
How can i achieve it in JHipster. I tried to generate a simple field as String, then i changed the POJO like this :
#ElementCollection
#Column(name="persons")
List<String> persons;
The domain.json containing the whole table remain not touched.
I tried to run the application, after running liquibase:diff, without success. How can i fix it?
Use the generator entity to create a relationship :
Create an entity Person (maybe with only the "name", but more fields will soon be needed. Like "active", some dates ...)
.jhipster/[YourEntity].json should contain :
"fields": [
{
"fieldName": "xxx",
"fieldType": "Integer"
}
],
"relationships": [
{
"relationshipName": "person",
"otherEntityName": "person",
"relationshipType": "one-to-many",
"relationshipValidateRules": [
"required"
],
"otherEntityField": "name"
}
],
don't forget to commit before using the generator. Maybe you will need multiple executions to get it right.

Elasticsearch term filter on inner object field not matching

I have just organized my document structure to have a more OO design (e.g. moved top level properties like venueId and venueName into a venue object with id and name fields).
However I can now not get a simple term filter working for fields on the child venue inner object.
Here is my mapping:
{
"deal": {
"properties": {
"textId": {"type":"string","name":"textId","index":"no"},
"displayId": {"type":"string","name":"displayId","index":"no"},
"active": {"name":"active","type":"boolean","index":"not_analyzed"},
"venue": {
"type":"object",
"path":"full",
"properties": {
"textId": {"type":"string","name":"textId","index":"not_analyzed"},
"regionId": {"type":"string","name":"regionId","index":"not_analyzed"},
"displayId": {"type":"string","name":"displayId","index":"not_analyzed"},
"name": {"type":"string","name":"name"},
"address": {"type":"string","name":"address"},
"area": {
"type":"multi_field",
"fields": {
"area": {"type":"string","index":"not_analyzed"},
"area_search": {"type":"string","index":"analyzed"}}},
"location": {"type":"geo_point","lat_lon":true}}},
"tags": {
"type":"multi_field",
"fields": {
"tags":{"type":"string","index":"not_analyzed"},
"tags_search":{"type":"string","index":"analyzed"}}},
"days": {
"type":"multi_field",
"fields": {
"days":{"type":"string","index":"not_analyzed"},
"days_search":{"type":"string","index":"analyzed"}}},
"value": {"type":"string","name":"value"},
"title": {"type":"string","name":"title"},
"subtitle": {"type":"string","name":"subtitle"},
"description": {"type":"string","name":"description"},
"time": {"type":"string","name":"time"},
"link": {"type":"string","name":"link","index":"no"},
"previewImage": {"type":"string","name":"previewImage","index":"no"},
"detailImage": {"type":"string","name":"detailImage","index":"no"}}}
}
Here is an example document:
GET /production/deals/wa-au-some-venue-weekends-some-deal
{
"_index":"some-index-v1",
"_type":"deals",
"_id":"wa-au-some-venue-weekends-some-deal",
"_version":1,
"exists":true,
"_source" : {
"id":"921d5fe0-8867-4d5c-81b4-7c1caf11325f",
"textId":"wa-au-some-venue-weekends-some-deal",
"displayId":"some-venue-weekends-some-deal",
"active":true,
"venue":{
"id":"46a7cb64-395c-4bc4-814a-a7735591f9de",
"textId":"wa-au-some-venue",
"regionId":"wa-au",
"displayId":"some-venue",
"name":"Some Venue",
"address":"sdgfdg",
"area":"Swan Valley & Surrounds"},
"tags":["Lunch"],
"days":["Saturday","Sunday"],
"value":"$1",
"title":"Some Deal",
"subtitle":"",
"description":"",
"time":"5pm - Late"
}
}
And here is an 'explain' test on that same document:
POST /production/deals/wa-au-some-venue-weekends-some-deal/_explain
{
"query": {
"filtered": {
"filter": {
"term": {
"venue.regionId": "wa-au"
}
}
}
}
}
{
"ok":true,
"_index":"some-index-v1",
"_type":"deals",
"_id":"wa-au-some-venue-weekends-some-deal",
"matched":false,
"explanation":{
"value":0.0,
"description":"ConstantScore(cache(venue.regionId:wa-au)) doesn't match id 0"
}
}
Is there any way to get more useful debugging info?
Is there something wrong with the explain result description? Simply saying "doesn't match id 0" does not really make sense to me... the field is called 'regionId' (not 'id') and the value is definitely not 0...???
That happens because the type you submitted the mapping for is called deal, while the type you indexed the document in is called deals.
If you look at the mapping for your type deals, you'll see that was automatically generated and the field venue.regionId is analyzed, thus you most likely have two tokens in your index: wa and au. Only searching for those tokens on that type you would get back that document.
Anything else looks just great! Only a small character is wrong ;)

Resources