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: Insert new row with relation to existing record (how?)
PostPosted: Mon May 18, 2009 8:41 am 
Newbie

Joined: Mon May 18, 2009 8:18 am
Posts: 2
Hi,

I'm quite new to NHibernate, so far I came to like it, but now I am stuck on something I suppose is quite easy. I have 2 tables: "request" and "request_status".

Code:
+----------+     +----------------+
| request  |     | request_status |
+----------+     +----------------+
| id       | +-> | id             |
| fk_status|-+   | code           |
+----------+     +----------------+


I have 2 record in "request_status" :
- id = 1 code = 'New'
- id = 2 code = 'Closed'

I want to add record in request and set fk_status to either 1 or 2.

Request.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Helpdesk.Models.Requests, Helpdesk" table="requests">
    <id name="Id" column="id" type="int" length="11">
      <generator class="identity" />
    </id>
    <many-to-one name="Status" class="Helpdesk.Models.RequestStatus, Helpdesk" column="fk_status" cascade="all" fetch="join" />
  </class>
</hibernate-mapping>


RequestStatus.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Helpdesk.Models.RequestStatus, Helpdesk" table="request_status">
    <id name="Id" column="id" type="int" length="11">
      <generator class="identity" />
    </id>
    <property name="Code" column="code" type="string" length="50" />
  </class>
</hibernate-mapping>


Request.cs
Code:
using System;

namespace Helpdesk.Models
{
    public class Requests
    {
        private int _id;
        private RequestStatus _status;

        public Requests()
        {
        }

        virtual public int Id
        {
            get { return this._id; }
            set { this._id = value; }
        }

        virtual public RequestStatus Status
        {
            get { return this._status; }
            set { this._status = value; }
        }
    }   
}



RequestStatus.cs
Code:
using System;

namespace Helpdesk.Models
{
    public class RequestStatus
    {
        private int id;
        private string code;

        public RequestStatus()
        {
        }

        virtual public int Id
        {
            get { return id; }
            set { id = value; }
        }

        virtual public string Code
        {
            get { return code; }
            set { code = value; }
        }
    }
}


Now, when I select a request, the mapping is done correctly, I mean, if I print the Request.Status field, I will see New or Closed. but when I want to add a new one programatically, I have to create a new instance of RequestStatus, something along the line of:
Code:
RequestStatus status = new RequestStatus();
            status.Id = 1;
            status.Code = "New";
            Request.Status. = status;


I would like to be able to only set the status to 1, or 2. Since I get the value from a dropdown. Anyone can help me with this? Many thanks in advance.


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.