NHibernate map class with pk also fk - nhibernate-mapping

I have two tables DATA_PERSONELL which has a primary key of PERSONNEL_ID, and SYS_USER_INFO which has a primary key of PERSONELL_ID which is also a foreign key to the DATA_PERSONELL table.
When attempting to map SYS_USER_INFO, the only mapping that I've tried which has worked is to map SYS_USER_INFO.PERSONELL_ID as an integer, which ultimately doesn't provide access to the data in DATA_PERSONELL.
Does anyone know how to do this the right way?

Use a one-to-one mapping.
More details here :
http://ayende.com/blog/3960/nhibernate-mapping-one-to-one
http://sdesmedt.wordpress.com/2006/07/24/nhibernate-part-3-mapping-techniques-for-aggregation-one-to-one-mapping/

Related

Can a parent child relationship be modeled in the same table using Foreign Keys?

I am designing a table that currently looks like this:
Container
ContaienrId
ParentContainerId
LotsOfOtherColumns
The idea is that a container can have a parent (which can in turn have a parent...). Also, several containers could have the same parent.
I can just stick the ID in there, but I would like the parent to be accessible via Entity Framework. For that to happen, I need a Foreign Key. (I also want the protection of a FK violation if an invalid number is entered for ParentContainerId.)
But when I try to put the Foreign key on these columns I get this error:
The columns in table 'Container (Container)' do not match an existing primary key or UNIQUE constraint.
I get what it is saying, I cant use ParentContainerId unless I put a UNIQUE constraint on it. But several containers can have the same parent, so that won't work.
Is there a way to use one table and have a Foreign Keyed parent child relationship?
You can do it like this if you want to model a one-to-many relationship:
create table container(
container_id int not null,
parent_container_id int,
more_data varchar,
primary key(container_id),
foreign key (parent_container_id) references container (container_id)
);
If you create an additional
unique(parent_container_id)
constraint, you will allow at most one child container in each parent container.
I just think you have to put a unique constraint on the ContainerId not the parentID

Does doctrine 2.x must have a primary key in db table?

I'm setting up a project with Doctrine 2.1.5. I have got few tables without primary keys. I added some primary keys temporarily to generate Entities and Repositories.
But I will delete the fake primary key columns and remove the reference from Doctrine Entity as well.
Also I will be defining few columns as composite primary key in those table Entities (but not in tables).
Will there be any consequences (eg: wrong result from query or failing query) when joining those entity classes Because of 1. not having PK in database 2.adding fake composite primary key in entity classes?
Lots of tutorial and blogs mentions that needs to have primary key. But, never anything mentioned (which I can find) that the above solution is not a problem. (or it is a problem).
An answer from real experience would be great. But everyone welcome.
Thanks in advance for help.
P

Is it possible to set constraints in DataContext generated from LINQ ORM?

I am working on a database whose tables won't have any foreign key constraints except composite primary keys for some of the tables. Is it possible to map the database using LINQ to SQL and then set the foreign key constraints in the DataContext being generated?
Thanks
Yes it is possible.
If they don't exist you just have to create them manually in the designer - use the Inheritance tool from the designer view and drag from the primary key on one table to the foreign key on the other.
Important notes:
1) both tables must have a primary key defined: see my blog entry on this point
2) the datatypes of the two columns must match - an integer cannot join to a date

How do I map an optional one-to-one relationship with Fluent NHibernate?

I've got two entities, one called Site and the other called Assignment. A Site may or may not have an associated Assignment. An Assignment is only ever associated with one Site. In terms of C#, Site has a property of type Assignment which could hold a null reference.
I have got two tables by the same names in the database. The Assignment table's PK is also its FK back to the Site table (rather than Site having a nullable FK pointing to Assignment). The SQL (with fields omitted for brevity) is as follows
CREATE TABLE Site(
SiteId INT NOT NULL CONSTRAINT PK_Site PRIMARY KEY)
CREATE TABLE Assignment(
AssignmentId INT NOT NULL CONSTRAINT PK_Assignment PRIMARY KEY,
CONSTRAINT FK_Assignment_Site FOREIGN KEY (AssignmentId) REFERENCES Site (SiteId))
I'm using Fluent NHibernate's auto persistence model, which I think I will have to add an override to in order to get this to work. My question is, how do I map this relationship? Is my schema even correct for this scenario? I can change the schema if needs be.
You need to read these:
http://ayende.com/Blog/archive/2009/04/19/nhibernate-mapping-ltone-to-onegt.aspx
http://gnschenker.blogspot.com/2007/06/one-to-one-mapping-and-lazy-loading.html
https://www.hibernate.org/162.html
it's not possible to have one-to-ones lazy loaded unless they are not-nullable, or you map them as a many-to-one with one item in it

Should I use a Foreign Key to Show Tree Relationship in SQL

I am trying to model a tree relationship in a table. For instance, there are "Categories" and categories can themselves be inside a Parent category.
My schema is:
id int PRIMARY KEY,
parent_id int,
name
My question is, should I label the parent_id column as a Foreign key? Foreign implies "outside" and not self-referencing. Is there a different type of key for this purpose?
My question is similar to:
Self-referencing constraint in MS SQL, but I'm asking a different question, cascading not being an issue.
Self-referencing foreign keys happen all the time. E.g. an employee might have another "employee" as his manager, so the manager_id will be a foreign key to the employee_id field in the same table.
Foreign keys are the natural candidate for representing the parent node in hierarchical data, although they're not exclusively used for that :)
If you have very deep levels of nesting it may not be easy to performantly select out all descendants of a particular node, since most DB's do not handle recursion very well. Another approach is to use what's called the "Nested Set Model" to represent the relationships. A great article is available here:
http://www.intelligententerprise.com/001020/celko.jhtml
A foreign key between two columns in the same table is often used when mapping tree structures to relational databases. However, it is not the only approach avaiable.
See this article for alternative reperesentations: Storing Hierarchical Data in a Database
I don't believe there is another type of key... a foreign key would be fine in this scenario.. it would enforce the constraint against the parent_id to ensure it references a valid id