I want to create a "friend" relationship between User using an entity "Friend".
I try this, but it isn't work.
entity Friend {
status Boolean,
modified LocalDate,
created LocalDate
}
relationship ManyToMany {
Friend{user(login)} to User,
User to Friend{user(login)}
}
How i can do that ?
Thanks
You cannot create relationship with User entity in JDL
A workaround is to create another entity and use a one-to-one relationship like this
entity Friend {
status Boolean,
modified LocalDate,
created LocalDate
}
entity UserExtended {
...
}
relationship OneToOne {
UserExtended to User
}
relationship ManyToMany {
Friend{userExtended(login)} to UserExtended,
UserExtended to Friend{userExtended(login)}
}
You may want to consider creating the relationship with the User directly in the generated code.
Found it :
entity UserExtra {
.....
}
entity Friend{
status Boolean,
modified LocalDate,
created LocalDate
}
relationship OneToOne {
UserExtended{user(login)} to User
}
relationship OneToMany {
UserExtended{friends} to Friend{user}
}
relationship ManyToOne {
UserExtended{friend} to UserExtended{users}
}
Related
how can I use a constructor on an Entity that has properties with #ManyToOne decorator and their types are from another Entity but only with the primary key.
For example:
#Entity()
class User {
constructor(
idUser: number,
idExtraData: number
){
this.id = idUser;
// this is going to give an error because it requires an instance of ExtraData.
// but I would like to pass only the id
// and have an instance similar when I use findOne without populating anything
this.extraData = idExtraData;
}
#PrimaryKey()
id: number;
#ManyToOne()
extraData: ExtraData;
}
The instance of the entity I want should be similar to the instance returned by findOne without populating anything.
You can use Reference.createNakedFromPK()
#Entity()
export class Book {
#ManyToOne(() => Author)
author: Author;
constructor(authorId: number) {
this.author = Reference.createNakedFromPK(Author, authorId);
}
}
This instance will be either replaced with the managed entity during flush if there is one, or it will be merged to the EM otherwise.
(the docs mention how to do this with reference wrapper only, but its the same approach)
https://mikro-orm.io/docs/entity-references#assigning-to-reference-properties
I have this UML:
entity Profile {
creationDate Instant required
bio String maxlength(7500)
}
entity Grupo {
creationDate Instant required
groupname String minlength(2) maxlength(100) required
image ImageBlob
isActive Boolean
}
// RELATIONSHIPS:
relationship OneToOne {
Profile{grupo} to Grupo{profile}
}
relationship OneToMany {
User{grupo} to Grupo{user(id) required}
}
// DTO for all
dto * with mapstruct
// Set pagination options
paginate all with pagination
// Set service options to all except few
service all with serviceImpl
and when I run it with yo jhipster:import-jdl it gives the following error:
IllegalAssociationException: Relationships from User entity is not supported in the declaration between User and Grupo.
Error jhipster:import-jdl ./src/main/scripts/raro.jh
Is it NOT allowed to have a OneToMany relationship with the User Entity? The thing is that OneToOne relationships works fine.
Because when I change the relationship to ManyToOne, it works
entity Profile {
creationDate Instant required
bio String maxlength(7500)
}
entity Groups {
creationDate Instant required
groupname String minlength(2) maxlength(100) required
image ImageBlob
isActive Boolean
}
// RELATIONSHIPS:
relationship OneToOne {
Profile{groups(groupname)} to Groups{profile}
}
// relationship OneToMany {
// User{groups} to Groups{user(id) required}
// }
relationship ManyToOne {
Groups{user(id) required} to User{groups}
}
// DTO for all
dto * with mapstruct
// Set pagination options
paginate all with pagination
// Set service options to all except few
service all with serviceImpl
Why? Am I doing something worng in the first example?
Thanks
JHipster does not allow OneToMany relationships to the user by default. If you need this, you will have to manually change the code.
Please note that the User entity, which is handled by JHipster, is specific. You can do:
many-to-one relationships to this entity (a Car can have a many-to-one relationship to a User)
many-to-many and one-to-one relationships to the User entity, but the other entity must be the owner of the relationship (a Team can have a many-to-many relationship to User, but only the team can add/remove users, and a user cannot add/remove a team)
https://www.jhipster.tech/managing-relationships/
Let's say you have a Jhipster app that has a Profile and needs to register which Profiles follows other profiles with 2 attributes: one for the user that is following (user Profile) and another for the followed user (followed Profile). Something like:
entity Profile {
creationDate Instant required
}
entity Follows {
creationDate Instant
}
relationship OneToMany {
Profile{follows(user)} to Follows{profile(id)}
Profile{follows(followed)} to Follows{profile(id)}
}
The problem with this is that the Follows.java has 2 identical atributes even when the names are different follows(user) & follows(followed):
#ManyToOne
private Profile profile;
... instead of...
#ManyToOne
private Profile user;
#ManyToOne
private Profile followed;
Thanks.
You are using the same relationship name for both relationships, but each relationship name needs to be different or else the fields conflict.
relationship (OneToMany | ManyToOne | OneToOne | ManyToMany) {
<from entity>[{<relationship name>[(<display field>)]}] to <to entity>[{<relationship name>[(<display field>)]}]
}
JDL Relationship Declaration Docs
In the case of your JDL sample, I changed the display field from user/followed to id as those fields do not exist on the entity. The important change is that the relationship names are unique.
relationship OneToMany {
Profile{followed(id)} to Follows{followed(id)}
Profile{following(id)} to Follows{following(id)}
}
entity Profile {
creationDate Instant required
}
entity Follows {
creationDate Instant
}
relationship ManyToOne{
Follows{user} to Profile
Follows{followed} to Profile
}
Supposing you want relation between Rooms and Patient
You can have these entities
entity NmsPatient {
photo ImageBlob
}
entity NmsRoom {
code String required,
name String required
}
entity NmsPatientRoom {
begin ZonedDateTime,
end ZonedDateTime
}
And then
relationship ManyToOne {
NmsPatientRoom{patient} to NmsPatient,
NmsPatientRoom{room} to NmsRoom
}
Consider we have a BankCard Entity that is a part of Client Aggregate. Client may want to cancel her BankCard
class CancellBankCardCommandHandler
{
public function Execute(CancelBankCardCommand $command)
{
$client = $this->_repository->get($command->clienId);
$bankCard = $client->getBankCard($command->bankCardId);
$bankCard->clientCancelsBankCard();
$this->_repository->add($client);
}
}
class BankCard implements Entity
{
// constructor and some other methods ...
public function clientCancelsBankCard()
{
$this->apply(new BankCardWasCancelled($this->id);
}
}
class Client implements AggregateRoot
{
protected $_bankCards;
public function getBankCard($bankCardId)
{
if (!array_key_exists($bankCardId, $this->_bankCards) {
throw new DomainException('Bank card is not found!');
}
return $this->_bankCard[$bankCardId]);
}
}
Finally we have some domain repository instance which is reponsible for storing Aggregates.
class ClientRepository implements DomainRepository
{
// methods omitted
public function add($clientAggregate)
{
// here we somehow need to store BankCardWasCancelled event
// which is a part of BankCard Entity
}
}
My question is whether AggregateRoot responsible for tracking its Entities' events or not. Is it possible to get events of an Entity which is a part of an Aggregate from within its Aggregate or not?
How to actually persist the Client with all changes made to the bank card saving its consistency?
I would say the aggregate as a whole is responsible for tracking the changes that happened to it. Mechanically, that could be "distributed" among the aggregate root entity and any other entities within the aggregate or the aggregate root entity as the sole recorder or some external unit of work. Your choice, really. Don't get too hung up on the mechanics. Different languages/paradigms, different ways of implementing all this. If something happens to a child entity, just consider it a change part of the aggregate and record accordingly.
I have the following tables
Entity
id,name,categoryid
21,"Blah",1
EntityCategory(Enum table)
id, name
1,"New Blahs"
I have a FK relationship between Entities->categoryid and EntityCategories->id
I have generated SubSonic classes for both as well a corresponding Model object for Entity
class Entity{ID,Name,CategoryName}
I am trying to return the Model.Entity type with category name filled in i.e.
public Entity GetEntityByName(string name){
return new
Select(
Entity.IdColumn,
Entity.NameColumn,
EntityCategory.NameColumn)
.From(Entity.Schema)
.InnerJoin(Tables.EntityCategory)
.Where(Entity.NameColumn).IsEqualTo(name)
.ExecuteSingle<Model.Entity>();
Needless to say this is not working. I actually get a Model.Entity with the Entity.Name set to the EntityCategoryName.
If you use SubSonic 3.0 you can do this with projection:
var result = from e in db.Entities
where e.ID=1
select new Entity{
ID=e.ID,
name=e.Name,
CategoryName=(CategoryName)e.CategoryID
}
With SubSonic 2.x, I'd say to make it easy on yourself and extend the partial class with a readonly enum:
public partial class Entity{
public CategoryName{
return (CategoryName)this.CategoryID;
}
}