-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping a table without a primary key (id)
PostPosted: Sat Jun 02, 2007 9:08 am 
Newbie

Joined: Sat Jun 02, 2007 8:55 am
Posts: 7
Hi,

I've got a table that is used to store a relation between other 2 tables, e.g.:

Table: Report
report_id (PK)
report_name

Table: Section
section_id (PK)
section_name

Table: Report_Section
report_id
section_id

How can I create the mapping file for Report_Section table, if there is no ID as in the previous tables?

Thanks,
Jerry


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 02, 2007 9:28 am 
Regular
Regular

Joined: Sun Jan 21, 2007 4:33 pm
Posts: 65
You create Report Section Table to have a Primary key (id), and use those two as Foreign keys to their respective tables.

Or, you can concatenate those two ids to make a composite id for the Report/Section table.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 02, 2007 12:12 pm 
Newbie

Joined: Sat Jun 02, 2007 8:55 am
Posts: 7
This is what I have so far:

My DB table:
Code:
CREATE TABLE [dbo].[T_app_vuvh_605_xmlexp_sys_ui_report_vodovody] (
  [id_report] int,
  [id_vodovod] varchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS
)
ON [PRIMARY]
GO


NHibernate mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">
   <class name="ReportingBusiness.Sys_Ui_Report_Vodovody, ReportingBusiness" table="T_app_vuvh_605_xmlexp_sys_ui_report_vodovody">
      <composite-id>
         <key-property name="Id_Report" column="id_report"/>
         <key-property name="Id_Vodovod" column="id_vodovod"/>
      </composite-id>
   </class>
</hibernate-mapping>


C# Class "Sys_Ui_Report_Vodovody":
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
namespace ReportingBusiness
{
   public class Sys_Ui_Report_Vodovody : BusinessBase
   {
      private int id_report;
      private string id_vodovod;

      public Sys_Ui_Report_Vodovody()
      {
      }

      public int Id_Report
      {
         get { return id_report; }
         set { id_report = value; }
      }

      public string Id_Vodovod
      {
         get { return id_vodovod; }
         set { id_vodovod = value; }
      }
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
   }
}


When I try to save this object into the DB, everything seems to work fine. There are no errors, but nothing gets saved. The DB table is empty.

Here is the log from the Save operation:
Code:
2007-06-02 18:04:05,896 DEBUG 5732 NHibernate.Transaction.AdoTransaction - begin
2007-06-02 18:04:05,902 DEBUG 5732 NHibernate.Impl.SessionImpl - generated identifier: ReportingBusiness.Sys_Ui_Report_Vodovody
2007-06-02 18:04:05,906 DEBUG 5732 NHibernate.Impl.SessionImpl - saving [ReportingBusiness.Sys_Ui_Report_Vodovody#ReportingBusiness.Sys_Ui_Report_Vodovody]
2007-06-02 18:04:05,916 DEBUG 5732 NHibernate.Transaction.AdoTransaction - commit
2007-06-02 18:04:05,918 DEBUG 5732 NHibernate.Impl.SessionImpl - flushing session
2007-06-02 18:04:05,918 DEBUG 5732 NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
2007-06-02 18:04:05,919 DEBUG 5732 NHibernate.Impl.SessionImpl - Processing unreferenced collections
2007-06-02 18:04:05,919 DEBUG 5732 NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
2007-06-02 18:04:05,920 DEBUG 5732 NHibernate.Impl.SessionImpl - Flushed: 1 insertions, 0 updates, 0 deletions to 2 objects
2007-06-02 18:04:05,920 DEBUG 5732 NHibernate.Impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2007-06-02 18:04:05,920 DEBUG 5732 NHibernate.Impl.Printer - listing entities:
2007-06-02 18:04:05,921 DEBUG 5732 NHibernate.Impl.Printer - ReportingBusiness.Reporty{Kvartal=1, Nazov=NewReport, Rok=2000, Id=1018}
2007-06-02 18:04:05,921 DEBUG 5732 NHibernate.Impl.Printer - ReportingBusiness.Sys_Ui_Report_Vodovody{}
2007-06-02 18:04:05,922 DEBUG 5732 NHibernate.Impl.SessionImpl - executing flush
2007-06-02 18:04:05,925 DEBUG 5732 NHibernate.Persister.EntityPersister - Inserting entity: [ReportingBusiness.Sys_Ui_Report_Vodovody#ReportingBusiness.Sys_Ui_Report_Vodovody]
2007-06-02 18:04:05,929 DEBUG 5732 NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands :1
2007-06-02 18:04:05,930 DEBUG 5732 NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO T_app_vuvh_605_xmlexp_sys_ui_report_vodovody (id_report, id_vodovod) VALUES (:id_report, :id_vodovod)
2007-06-02 18:04:05,931 DEBUG 5732 NHibernate.Persister.EntityPersister - Dehydrating entity: [ReportingBusiness.Sys_Ui_Report_Vodovody#ReportingBusiness.Sys_Ui_Report_Vodovody]
2007-06-02 18:04:05,945 DEBUG 5732 NHibernate.Type.Int32Type - binding '1018' to parameter: 0
2007-06-02 18:04:05,946 DEBUG 5732 NHibernate.Type.StringType - binding 'T201' to parameter: 1
2007-06-02 18:04:05,948 DEBUG 5732 NHibernate.SQL - INSERT INTO T_app_vuvh_605_xmlexp_sys_ui_report_vodovody (id_report, id_vodovod) VALUES (@p0, @p1)
2007-06-02 18:04:05,948 DEBUG 5732 NHibernate.SQL - @p0 = '1018'
2007-06-02 18:04:05,949 DEBUG 5732 NHibernate.SQL - @p1 = 'T201'
2007-06-02 18:04:05,950 DEBUG 5732 NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands :0
2007-06-02 18:04:05,951 DEBUG 5732 NHibernate.Impl.SessionImpl - post flush
2007-06-02 18:04:05,953 DEBUG 5732 NHibernate.Impl.SessionImpl - transaction completion
2007-06-02 18:04:05,953 DEBUG 5732 NHibernate.Transaction.AdoTransaction - running AdoTransaction.Dispose()
2007-06-02 18:04:05,973 DEBUG 5732 NHibernate.Impl.SessionImpl - flushing session
2007-06-02 18:04:05,974 DEBUG 5732 NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
2007-06-02 18:04:05,974 DEBUG 5732 NHibernate.Impl.SessionImpl - Processing unreferenced collections
2007-06-02 18:04:05,974 DEBUG 5732 NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
2007-06-02 18:04:05,976 DEBUG 5732 NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
2007-06-02 18:04:05,976 DEBUG 5732 NHibernate.Impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2007-06-02 18:04:05,978 DEBUG 5732 NHibernate.Impl.Printer - listing entities:
2007-06-02 18:04:05,978 DEBUG 5732 NHibernate.Impl.Printer - ReportingBusiness.Reporty{Kvartal=1, Nazov=NewReport, Rok=2000, Id=1018}
2007-06-02 18:04:05,979 DEBUG 5732 NHibernate.Impl.Printer - ReportingBusiness.Sys_Ui_Report_Vodovody{}
2007-06-02 18:04:05,979 DEBUG 5732 NHibernate.Impl.SessionImpl - executing flush
2007-06-02 18:04:05,980 DEBUG 5732 NHibernate.Impl.SessionImpl - post flush
2007-06-02 18:04:05,980 DEBUG 5732 NHibernate.Impl.SessionImpl - closing session
2007-06-02 18:04:05,980 DEBUG 5732 NHibernate.Impl.SessionImpl - disconnecting session
2007-06-02 18:04:05,980 DEBUG 5732 NHibernate.Connection.ConnectionProvider - Closing connection
2007-06-02 18:04:05,981 DEBUG 5732 NHibernate.Impl.SessionImpl - transaction completion


Any ideas, why this isn't saved into the DB, even though the INSERT statement seems to be executed with correct values?

Thanks,
Jerry


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 02, 2007 1:11 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Can we see the code that is doing the work of saving the object to the session? It's a bit hard to tell what's going on. Oh, and are you committing your transaction?

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 03, 2007 5:51 am 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
You should correctly implement Equals and GetHashcode to match the database equality. Don't know if this might solve your problem though.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 6:12 am 
Newbie

Joined: Sat Jun 02, 2007 8:55 am
Posts: 7
The insert is working after all. There must have been a problem with my DB. However to get the insert to run I have to call Save() explicitly, SaveOrUpdate() doesn't work, because it's always trying to update, even though it is a new record. Would that be because the Equals() method isn't implemented properly? How should the Equals method look like?

Thanks :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.