veaceslav.kunitki
veaceslav.kunitki

Reputation: 326

JHipster relation between predefined table USER and my own table

I need to define new table related to predefined 'User' table. Please, help me to write a correct JDL code for this entity diagram

I tried to write file "mytable.jh" and import :>jhipster import-jdl mytable.jh

entity MyTable{
    userid Long, //relation to table jhi_user
}

relationship OneToMany {
  User{id} to Mytable{userid}
}

and got

{ name: 'IllegalAssociationException',
  message: 'Relationships from User entity is not supported in the declaration between User and Mytable.',
  prototype:
   Error

Upvotes: 3

Views: 3779

Answers (2)

Rousseau Alban
Rousseau Alban

Reputation: 104

I think this tip in the jhipster doc might help you, I never looked at it, but a mate did this during my project I'm crrently working on. Hope this helps. You can always ask Paul-Etienne for further information, he will gladly help.

Upvotes: 0

allquixotic
allquixotic

Reputation: 1559

Basically you can't add new fields to the jhi_user table, and JDL won't let you do anything that would cause that to happen.

You can add a User object reference to another entity with something like

relationship ManyToOne {
  Mytable{userid} to User
}

Note there that I am not putting {anything} after User -- this means the User has no idea which Mytable it's associated with. If you want to "back up" from an instance of Mytable to an instance of User, you have to search from the Mytable side; the User won't have any fields or getters/setters related to an instance of Mytable.

Also note that the userid field is not necessarily strictly for relating the two entities. The JDL and code generation actually takes care of creating the relationships and primary/foreign keys for you. So

entity Car {
}

relationship ManyToOne {
  Car{user} to User
}

means for every User, there are many Cars, or another way of saying it is for every Car there is one user whose field name within the Car object will be user. So you'll have Car.getUser() method, etc.

When you define a field within an entity block, you're defining a property of that entity, not a relation -- that property is independent of all other objects in your model.

You still won't have a User.getCars() method though. This is not possible with current jHipster design.

Upvotes: 12

Related Questions