I'm experimenting with jHipster.
I want to create a one-to-one relationship from my entity X to the User entity.
This is definition:
"relationships": [
{
"relationshipId": 1,
"relationshipName": "user",
"otherEntityName": "user",
"relationshipType": "one-to-one",
"otherEntityField": "id",
"ownerSide": true,
"otherEntityRelationshipName": "x"
}
]
Unfortunately, as you can see, it also expects changes in the user entity which is actually managed by jHipster. I don't want to change the generated User code.
I have experimented with many-to-one relationships to User. They don't cause problems because there's no otherEntityRelationshipName field.
{
"relationshipId": 1,
"relationshipName": "user",
"otherEntityName": "user",
"relationshipType": "many-to-one",
"otherEntityField": "id"
}
Suggestions are most welcome.
Thanks, Andy
This should work. I just tested it with the latest version 2.22.0 of JHipster.
Are you using the latest version of JHipster? What error do you get?
The "otherEntityRelationshipName": "x"property is actually ignored by the generator when the other relationship entity is User. Though it is still asked by the generator and this is a bug. I have created an issue in github for this, see https://github.com/jhipster/generator-jhipster/issues/2121
Related
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.
[JHipster Generator 4.14.4]
i generate an "article" with some relationships with JDL Studio. Relationships in my article.json look like this :
"relationships": [
{
"relationshipType": "one-to-one",
"relationshipName": "adress",
"otherEntityName": "adressDomain",
"otherEntityField": "id",
"ownerSide": true,
"otherEntityRelationshipName": "article"
},
How can i add the required rules to my relationship?
i tried "relationshipValidateRules": "required" but it doesnt works.
My Answer is not to change in the JSON File but to regenerate the *.jh file on JDL-Studio.
Here is how to add the required:
relationship OneToOne {
Article{adress required} to AdressDomain
}
I want to reference a different model(as discribed here: https://loopback.io/doc/en/lb2/Embedded-models-and-relations.html) but the by a nested id:
{
"name" : "person",
...
"relations": {
"cars": {
"type": "referencesMany",
"model": "car",
"foreignKey": "cars.id"
}
}
Person json will actually be something like:
{
...
cars: [{"id": 1, "name": "car1"}, ...]
}
And car model will be the full car details
Do I have to write my own remote method to do this?
Yosh DaafVader,
I've came accross this issue also and took time to find a solution ^^ but actually you just have to play with the parameter options inside your target relation property. The documentation states how the relation should be defined (sure the loopback cli does not include in version 3.x yet the way to use embeds nor references).
In your person model you have to change the foreignKey and to add the following options to be able to only use id to reference cars.
{
"name" : "person",
...
"relations": {
"cars": {
"type": "referencesMany",
"model": "car",
"foreignKey": "",
"options": {
"validate": true,
"forceId": true
}
}
}
Now you will be able to see in the explorer the new routes to add, remove and see the cars that belongs to the target person.
[Edit]
the foreignKey shall be blank, in order to be able to add items properly in the list of cars, or you can test and give some feedbacks about it
The validate option ensures the id exists in your database
forceId option will ensure it accepts only ids as a parameter
[/Edit]
Hope it will help :)
Cheers
I'm using the latest AMS v0.10.0.rc3 with the JSON API adapter.
So far is working great and is adding some useful conventions that i would like to change.
For example, lets suppose that i have a Post serializer and a Comment serializer like this:
class Post < ActiveModel::Serializer
attributes :id, :title
has_many :comments
end
class Comment < ActiveModel::Serializer
attributes :id, :comment
belongs_to :post
end
Then if i request /posts/1 for example i get the following
{
"data": {
"id": "1",
"type": "posts",
"attributes": {
"title": "My awesome title",
},
"relationships": {
"comments": {
"data": [
{
"id": "1",
"type": "comments"
},
{
"id": "2",
"type": "comments"
}
]
}
}
}
}
Notice how the relationships member appears and according with the spec is marked as an optional member with MAY.
It is a nice convention that i need to override some times.
So my question is:
How i remove the relationship member at the serializer or controller level ?
(If i miss some detail please comment and i will update the question.)
Currently, your best option is to define two serializers, one with the associations, one without, and specify in your render call which to use (render json: posts, each_serializer: PostWithoutAssociationsSerializer).
There is an ongoing discussion about adding an include_if option to associations and attributes, which you could potentially leverage if it gets merged.
Hello I'm generating some Entity with the jhipster generator.
When I generate an entity relationship, I'd like to make the value in that field mandatory but unfortunately, the generator doesn't provide that option.
Is there anyway to inform the generator of this necessity?
(editing the .jhipster/entity_name.json), for example?
Since this commit JHipster supports the required validation on relationships too.
This is an example from a JDL file:
relationship ManyToOne { Certificate{ca(name) required} to CertificateAuthority }
('Certificate' and 'CertificateAuthority' are entities and 'ca' is the field name)
The relevant part from the .jhipster/Certificate.json:
"relationships": [
{
"relationshipType": "many-to-one",
"relationshipValidateRules": "required",
"relationshipName": "ca",
"otherEntityName": "certificateAuthority",
"otherEntityField": "name"
},
The generated Certificate.ca field:
#ManyToOne
#NotNull
private CertificateAuthority ca;
And the generated form has a 'This field is required.' warning at the Ca filed.
No, it would be a new feature.
Feel free to create an issue in our github project and pull requests are always welcome :)
Just for others, here is the issue you opened