-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Association 1 : 0..1 mappings
PostPosted: Thu Jun 02, 2011 1:01 am 
Newbie

Joined: Thu Jun 02, 2011 12:49 am
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.