Jhipster: How to create a relationship using a field not the id - mysql

I have 2 entities Country abd competions and i want to build a relationship betwenn them using the isoCode of the country, but when i generate the database(mySql) i find that the relation is based on the generated country_id.
What i'm missing?
Can i specify my own id and remove the generated id from the jdl file?
entity Country {
isoCode String required
.
.
}
entity Competition {
priority Integer,
code String,
name String
}
relationship OneToMany {
Country to Competition{country(isoCode)},
}
Thanks

JHipster only supports technical ids for relationships, if you want to use business ids you must code them manually, maybe have a look at Hibernate #NaturalId

Related

Why Employee table of NORTHWND database has a self reference?

I generated an object model of the Employee table of the NORTHWND database, using the object relational designer.
Why does the Employee class has a self reference?:
This is the relevant part of the generated class:
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Employees")]
public partial class Employee : INotifyPropertyChanging, INotifyPropertyChanged
{
// ... other properties ...
private EntitySet<Employee> _Employees;
private EntityRef<Employee> _Employee1;
}
I assume you refer to this NORTHWIND database.
The Employee entity has a self-reference because it contains a foreign key ReportsTo, which is probably meant to reflect a hierarchy of employees in imaginary Northwind company.
This foreign key also shows that the relationship is defined as 1 to * (many). Hence, there can be only 1 supervisor to whom the employee reports to, but a supervisor can have many employees reporting to him or her.

Recognize laravel's relationships

I got 3 linked tables and i am quite lost with eloquent relationship.
I need help to recognize my relationship type. I use Laravel 4.1
acquisitions table :
#id
date
sensors table :
#id
name
acquisition_sensor table:
#id
acquisition_id
sensor_id
depth
value
Indeed 1 acquisition may have many depth, and different values.
I search a way to link my tables and use sensor model likee:
Sensor::find(1)->acquisitions->count();
and
Sensor::find(1)->sensor_acquisition->get();
I actually do it with the Query Builder but i think there is a way to use it more efficiently with eloquent !
You are going to want to set up a many-to-many relationship in your Eloquent models.
Sensor.php (model)
public function acquisitions()
{
return $this->belongsToMany('Acquisition', 'acquisition_sensor', 'acquisition_id', 'sensor_id');
}
Acquisition.php (modal)
public function sensors()
{
return $this->belongsToMany('Sensor', 'acquisition_sensor', 'acquisition_id', 'sensor_id');
}
You can read more about many-to-many Eloquent relationships here, http://laravel.com/docs/eloquent#many-to-many
If you want to run the eloquent query you described in your question, then you can do it like so:
Sensor::find(1)->acquisitions()->count();
If you are chaining, then make sure to add the () to acquisitions.

MDS business rule

I am new with MDS, and I have a question about one to many relation mapping in MDS
I have a product, contains descriptions in multiple languages. I have created two entities with derived hierarchy structure: product (P_ID, P_name)and Addtional description(P_ID, P_Name_in_German, P_name_in_English).
Additonal description is a drop down from product table from MDS UI, but I only want to populate info that releated with its same P_ID. How can I achieve that? Can I use business rules here and how it should look like?
(2012 Master data service' web interface)
I have also faced a similar problem. I have decided to add multiple fields for languages and apply business rules for them. Let's see how it would work for your entity:
Product entity
{
Code
Name
Name_in_English (text)
Name_in_German (text)
Default_language (domain based, points to Languages entity)
}
Languages entity
{
Code
Name
}
Add values to the Languages entity:
Code = "EN", Mane = "EN"
Code = "DE", name = "DE"
Now we need to add the following business rules to the Product entity:
BR1:
{
IF Condition - Equals: Default_language equals "EN"
THEN Action - Change value: Name = Name_in_English
}
BR2:
{
IF Condition - Equals: Default_language equals "DE"
THEN Action - Change value: Name = Name_in_German
}
After that You will see the product names in your product entity only in proper language which is chosen by drop-down field Default_language.
The second option:
If You want user to see only the Name field and don't want him to see additional fields,
You can hide those fields (Name_in_English and Name_in_German) by setting their width to zero.
Moreover, You can use attribute groups to separate two modes of view:
first mode (for the regular user) - You see only Code and Name
second mode (for the administrator) - You see fields: Name_in_English, Name_in_German, Default_language.
For it to work You need to create two attribute groups:
1) attribute group "EN" (add attributes Name and Code to the group)
2) attribute group "DE" (add attributes Name_in_English, Name_in_German, Default_language to the group)
Hope something of that is helpful!
You're missing something. You said you have a derived hierarchy structure but you don't show a domain based attribute on the Additional description entity with a pointer back to the Product entity. Perhaps you were thinking that the P_ID for Additional Description is the pointer back but it isn't (based on your explanation). It is the entity Code identifier for the Additional Description entity and not the key you need that points back to Product. Perhaps you meant One to One. One to Many implies you have a separate Parent_P_ID back to the Product entity.
Add the Domain Based Parent_P_ID attribute to the Additional Description entity and then reconstruct your derived hierarchy structure. This may not be that helpful because I think you have left something out of the explanation of what you are trying to do.
Hi Cocunuts,
We can not assign derived hierarchy to entities in MDS 2012,but we can achieve this in MDS 2016 , Your Domain based attribute(Drop down) cannot be further filtered in MDS 2012.

To fetch column name

I am using struts2 , hibernate and MySql for my project.
I have table name TimeTable having 42 columns (all long datatype) containing course codes.
I want to search "column names" having particular course code from a particular row.
Help me please.
If you have mapped the entity in a "proper" way in hibernate, the answer is obvious:
You will have an entity called TimeTable, which have 42 relationships to Course (I bet the attribute name will be course1, course2.... course42).
The resulting HQL is simply a bunch of OR
from TimeTable t
where t.course1.code = :something
OR t.course2.code = :something .....
However, it is obviously a bad model design. You should make Timetable and Course a Many-To-Many relationship, and have another table storing the relationship. So, in the entity, you will see something like
class TimeTable {
#ManyToMany
private List<Course> courses;
}
Your life will be much easier with such design.

LINQ to SQL

While trying to use LINQ to SQL I encountered several problems.
I have table persons:
int ID
string firstName
string lastName
And table notes that has:
int ID
string noteText
string createdBy
datetime creationDate
int PersonID
PersonID is a foreign key and the relationship is 1:n
I tried to use LINQ to SQL to create a person and some notes for each person.
Person person = new person();
Person.firstName = "me";
Person.note = new note();
Person.note.noteText = "some text…";
_DataContext.Persons.InsertOnSubmit(person);
_DataContext.SubmitChanges();
The problem is that the person object doesn't yet exist in the DB so it doesn't have an ID yet. So the note.personID filed has a 0 value… (the ID field is an identity field in the sql server)
The only solution for this that I found is to create a person , submitchanges and then create a note and submitchanges again.
Am I missing something here or maybe that’s the way one should work with LINQ to SQL?
How can I add multiple notes per person with LTS? I have a 1:n relationship and I don't see it with LTS.
If a person has 10000 notes, I don't want the person object constructor to load all the notes he has. I want to load them only when I refer to them. How can I config LTS to load the notes on demand?
If you aren't using the LinqToSql class designer, you should think about using it. The classes it generates will support the insertion scenario you outlined.
I can tell you aren't using the designer because it would give you a Notes (plural) property on the Person... as Person is 1 to Many with Notes.
How can I config LTS to load the notes on demand?
The designer will generate a property of type EntitySet(Note), which will load Notes on demand.