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.  [ 2 posts ] 
Author Message
 Post subject: Joined-Subclass
PostPosted: Thu Oct 25, 2007 11:49 am 
Beginner
Beginner

Joined: Tue May 30, 2006 10:55 am
Posts: 21
I am using the joined-subclass mapping and all works fine with one exception. If I delete the subclass I want to hold the data from the basic class but this entry will be deleted too. Is there a possibility to prevent it? Here is what I use in code at the moment:
Code:
public class DetailInfo
{
  private Guid _id = Guid.Empty;
  private string _title = String.Empty;

  public virtual Guid Id
  {
    get { return _id; }
    set { _id = value; }
  }

  public virtual string Title
  {
    get { return _title; }
    set { _title = value; }
  }
}

public class MoreDetailInfo : DetailInfo
{
  private string _subtitle = String.Empty;

  public virtual string Subtitle
  {
    get { return _subtitle; }
    set { _subtitle = value; }
  }
}

The mapping file looks like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyNamespace" assembly="MyAssemblyName">

   <class name="DetailInfo" table="Details" >

      <id name="Id" column="id" type="System.Guid">
         <generator class="guid" />
      </id>

      <property name="Title" type="string" />
      
      <joined-subclass name="MoreDetailInfo" table="MoreDetails" lazy="false" proxy="MoreDetailInfo">
         <key column="detailId" />

         <property name="Subtitle" type="string" />
      </joined-subclass>

   </class>
   
</hibernate-mapping>

And inside of the database I have two tables named Details and MoreDetails.
Code:
CREATE TABLE [dbo].[Details](
   [id] [uniqueidentifier] NOT NULL,
   [title] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL,
CONSTRAINT [PK_Details] PRIMARY KEY CLUSTERED
(
   [id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


GO

CREATE TABLE [dbo].[MoreDetails](
   [detailId] [uniqueidentifier] NOT NULL,
   [subtitle] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL,
CONSTRAINT [PK_MoreDetails] PRIMARY KEY CLUSTERED
(
   [detailId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[MoreDetails]  WITH CHECK ADD  CONSTRAINT [FK_MoreDetails_Details1] FOREIGN KEY([detailId])
REFERENCES [dbo].[Details] ([id])
GO
ALTER TABLE [dbo].[ProofSelections] CHECK CONSTRAINT [FK_MoreDetails_Details1]


What I want is when I delete a MoreDetail object the entry from the MoreDetails table will be deleted but the entry in the Details table should be still stored. Is this possible? At the moment if I delete a MoreDetail object the entry from the MoreDetail table will deleted but from the Details table too.

Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 25, 2007 4:40 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
You might be better off modeling this as a one-to-one relationship rather than as a subclass. You can't normally convert a class from one type to another (although, sure, you can treat a subclass as it's base...but it's still an instance of the subclass!).

If you model as a one-to-one you can have an entity that shares the same key and is directly related but the extra info can be deleted at any time without affecting the main object.

Cheers,

Symon.


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

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.