Hello all,
I apologise for newbie question, but I am not able to find the solution. I want to create 1 : 0..1 association between persons (dba."Osoby") and their database logins (dba."DbLogin"). Only some persons can sign up into the database. After some impasses (<one-to-one>, <one-to-many> and <many-to-one> back) I wrote the code that works but it uses composite-id for single column (stupidity). Please can somebody correct this code (verify tags, attributes, ...) and at least get off the <composite-id> approach?
Thank you
Code:
CREATE TABLE dba."Osoby"
(
"IdOsoby" smallint NOT NULL DEFAULT nextval('dba."Osoby_IdOsoby_seq"'::regclass),
"Email" character varying(30),
...
CONSTRAINT "PK_OSOBY" PRIMARY KEY ("IdOsoby")
)
public class Osoba
{
public virtual short? IdOsoby { get; set; }
public virtual string Email { get; set; }
...
public virtual IList<DbLogin> DbLogin { get; set; }
}
<class name="Osoba" table="`Osoby`">
<id name="IdOsoby" column="`IdOsoby`" unsaved-value="null">
<generator class="sequence">
<param name="sequence">`Osoby_IdOsoby_seq`</param>
</generator>
</id>
<property name="Email" column="`Email`" />
...
<bag name="DbLogin" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="`IdOsoby`" />
<one-to-many class="DbLogin"/>
</bag>
</class>
CREATE TABLE dba."DbLogin"
(
"IdOsoby" smallint NOT NULL,
"UserName" character varying(16) NOT NULL,
"Pwd" character varying(16) NOT NULL,
CONSTRAINT "PK_DBLOGIN" PRIMARY KEY ("IdOsoby"),
CONSTRAINT "FK_DBLOGIN_OSOBY" FOREIGN KEY ("IdOsoby")
REFERENCES dba."Osoby" ("IdOsoby") MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT
)
public class DbLogin
{
public virtual string UserName { get; set; }
public virtual string Pwd { get; set; }
public virtual Osoba Osoba { get; set; }
// Equals() and GetHashCode() method implementations required by <composite-id> tag in mappings file
}
<class name="DbLogin" table="`DbLogin`">
<composite-id>
<key-many-to-one name ="Osoba">
<column name="`IdOsoby`" />
</key-many-to-one>
</composite-id>
<property name ="UserName" column="`UserName`" />
<property name="Pwd" column ="`Pwd`" />
</class>
At nhusers google forum I found notice:
Quote:
Strictly speaking, optional items are one-to-many associations. The semantic of an one-to-one association is 1:1 not 1:0,1. So I assume one-to-one’s are not advised because it’s better to put the data in one table to avoid the hassle with aligning id’s and the join in each access.
Therefore I want to avoid <one-to-one> association.
Thank for your help.