Why Employee table of NORTHWND database has a self reference? - linq-to-sql

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.

Related

How do I connect 4 tables in Laravel 7?

So basically I have 4 tables
The employees, payrolls and employees_payrolls(pivot table) table are already connected through a many-to-many relationship and is working fine. Is it possible to fetch the basic_pay(from positions table) column through positions_id at employees table(which is already connected through the many-to-many relationship) so that I can perform calculation on payroll table that will base on employees basic pay?
you can not use $payroll->employees->positions->basic_pay because $payroll->employees
this gives you a collection of employees, each employee has many positions, so if you perform $payroll->employees->positions->basic_pay it makes collections in a collection, like below :
Payroll
Employee-1
Position-1->basic_pay
Position-2->basic_pay
Employee-2
Position-1->basic_pay
Position-2->basic_pay
.
.
.
if you perform $payroll->employees->positions->basic_pay the structure does not make sense in Laravel collection so you first perform $payroll->employees then loop through each and perform second loop on $employee->positions and next you will get $position->basic_pay
You will need to add a belongsTo relationship in your Employee model:
/**
* Get the employee's position.
*/
public function position()
{
return $this->belongsTo(Position::class,'positions_id');
}
Now if you wish to access it you will need to do the folllowing:
//get a payroll from database
$payroll = Position::find(1);
//get your payroll employees then iterate through each one
foreach($payroll->employees as $employee) {
//access to this employee position attributes
$employee->position;
}

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

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

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.

Using LINQ and Entity how do I return values of a many-to-many relationship

I have two entities, Account and Subscription with a many-to-many association between them. I can't seem to find in the tutorials anywhere how to do the following:
I want to find all the Accounts with a Subscription of type x. If I wasn't using the Entity framework I could join to the AccountSubscription table, but that isn't accessible via Entity. Do I have to create a special entity if I need to query on a many-to-many relationship?
EF should create a navigation property for a many-to-many relationship. Then you should be able to do something like this:
var accounts = from a in Accounts
where a.Subscriptions.Any(s => s.SubscriptionType == "something")
select a;
For example, I have a simple db with a many to many relationship between Products and Groups:
And EF creates the association in the model:
So I can create a query like this (here including the Groups so I can see the Category):
What about something like this:
List<Accounts> myAccounts = //get accounts;
foreach(Accounts a in myAccounts)
{
foreach(Subscriptions s in a)
{
//add it to a global list?
}
}

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.