NHibernate One-To-One - nhibernate

I've an issue using one-to-one mapping. I've searched internet and found many solutions but none was satisfying. Most of the examples carry overhead of storing parent instance in child class.
I want to use only parent Id in child class having foreign key constraint relationship but dont want to keep any parent instance in child.
When I try to load the records from database it throws exception "No row with the given identifier exists [AssemblyName.]". But, the record exists in Table "B" properly.
Any solutions for this issue?
The class structure:
class A {
public virtual string Id {get;set;}
public virtual B B {get;set;} // properties...... }
class B { public virtual string Id {get;set;} // properties......
public virtual string ParentId { get;set;} // class A Id }
The database structure:
CREATE TABLE [A](
[Id] [nvarchar](45) PRIMARY KEY
) ON [PRIMARY]
CREATE TABLE [B](
[Id] [nvarchar](45) PRIMARY KEY,
[ParentId] [nvarchar](45) NOT NULL
) ON [PRIMARY]
The mapping:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="A,AssemblyName" table="A" lazy="true">
<id name="Id" column="Id" type="string">
<generator class="assigned"/>
</id>
<one-to-one name="_B" cascade="all" fetch="join" foreign-key="None" constrained="true" class="B"/>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="B,AssemblyName" table="B" lazy="true">
<id name="Id" column="Id" type="string"> <generator class="assigned"/> </id>
<property name="_Name" column="Name"/> </class>
</hibernate-mapping>

One-to-one associations are always bidirectional in NHibernate. Mapping of class B is incomplete:
<class name="B,AssemblyName" table="B" lazy="true">
<id name="Id" column="Id" type="string">
<generator class="assigned"/>
</id>
<many-to-one name="A" unique="true" column="A" />
<property name="_Name" column="Name"/>
</class>
This is foreign key association. For more information see this detailed Ayende's blog post or NHibernate documentation.

Read this. A "real" one-to-one needs the same primary key in both tables (and no ParentId column).

Related

Hibernate: join with 2 NOT primary key

i want to do a left outer join from 2 table, but with 2 key that are not primary key.
This is the native sql query used:
Maybe there is a better way to do it?
SQLQuery query = session.createSQLQuery("
select {A.*},{B.*} from A_TABLE A left outer join B_TABLE B on A.QUOTE_ID = B.QUOTE_NUM ")
.addEntity("A", A_Class.class)
.addJoin("B", "A.bDocs");
for(Object result : query.list())
{
....
}
The mapping A file:
<class name="A_Class" table="A_TABLE" schema="S">
<id name="rowId" type="string">
<column name="ROW_ID" length="60" />
<generator class="assigned" />
</id>
<set name="BDocs" inverse="true" fetch="select" lazy="false">
<key>
<column name="QUOTE_NUM" length="60" not-null="true" />
</key>
<one-to-many class="B_Class" />
</set>
A_Class.java
public class A_Class implements java.io.Serializable
{
private String rowId;
private String QuoteId;
private Set BDocs= new HashSet(0);
// omitted all the set and get
}
The mapping B file:
<hibernate-mapping>
<class name="B" table="B_TABLE" schema="S">
<id name="rowId" type="string">
<column name="ROW_ID" length="60" />
<generator class="assigned" />
</id>
<many-to-one name="A" class="A_Class" fetch="select" lazy="false" outer-join="true" foreign-key="QuoteId" property-ref="QuoteId">
<column name="QUOTE_NUM" length="60" not-null="true" />
</many-to-one>
B_Class.java
public class B_Class implements java.io.Serializable
{
private String rowId;
private String quoteNum;
// omitted all the set and get
}
From this query i obtain 2 objects, one of A type and the other of B Type with a lot of correct datas but the set BDocs in the object of A type isn't filled. The goal is to get only the A Object with the variable BDocs filled with the B Objects.
I don't understand if the problem is in the query or in the mapping files. Anyone can help me?

Another NHibernate Mapping Mystery! Getting count in hbm file

I am trying to retrieve the count of items allocated to a container in my hbm file. I've done a bit of digging and managed to get my hbm code this far (below!). I want the count to be retrieved every time a container object is queried. I could use an interceptor but I assume there's a better way. Am I on the right track or should I use a different strategy to get the count loaded up?
Thanks.
P.S. We're using NH v2.2
<?xml version="1.0" encoding="utf-8"?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false" assembly="MyEntities" namespace="Entities.Containers"> <class name="Container" table="[Container]" xmlns="urn:nhibernate-mapping-2.2">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Capacity" column="Capacity">
<column name="Capacity" />
</property>
<property name="Description" column="Description" length="50" type="String">
<column name="Description" />
</property>
<loader query-ref="sqCurrentContainerAllocation"/> </class>
<sql-query name="sqCurrentContainerAllocation">
<return-scalar column="AllocatedItemsCount" type="int"></return-scalar>
SELECT COUNT(*) FROM [ContainerTracking]
WHERE [ContainerId] = :Id </sql-query>
</hibernate-mapping>
If you need to get some calculated property, you can use mapping with formula.
Let's extend your C# class:
public class Container
{
... // ID, Capacity, Description
public virtual int MyCount { get; set; }
And extend your mapping
<class name="Container" table="[Container]"
...
<property name="MyCount" insert="false" update="false" >
<formula>
(
SELECT count(*)
FROM [ContainerTracking] as ct
WHERE ct.[ContainerId] = Id
)
</formula>
</property>
the Id will be replaced with somethink like '_this.Id', the name of the column Id and its alias
This will of course load the count all the time (except projections) so think twice before use it

HQL Query SELECT with NOT IN

I need a HQL Query but I just dont get it.
SELECT * FROM Poll WHERE pid NOT IN (
SELECT pid FROM votes WHERE uid = 'theuserid')
I want all List of PollĀ“s back where the uid not in the table votes exists.
Also helpfull would be the hql query where the uid in the table votes exists, but I guess this is very similar ;-)
These are the 2 classes:
public class Poll {
private int pid;
private String name;
private String description;
private Date deadline;
private Set<Team> teams = new HashSet<Team>(0);
//some getter & setter
}
public class Vote {
private int vid;
private String uid;
private int pid;
private int tid;
private int votes;
//some getter & setter
}
Can smbdy please help me. I guess it is a join with a WHERE and NOT LIKE but I just dont get it.
Merci!
This is btw the hibernate mapping:
<hibernate-mapping>
<class name="package.model.Poll" table="poll">
<id name="pid" column="pid" >
<generator class="increment"/>
</id>
<property name="name" column="name" />
<property name="description" column="description" />
<property name="deadline" type="timestamp" column="deadline" />
<set name="teams" table="pollchoice"
inverse="false" lazy="false" fetch="select" cascade="all" >
<key>
<column name="pid" not-null="true" />
</key>
<many-to-many entity-name="knt.exceedvote.model.Team">
<column name="tid" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="package.model.Vote" table="votes">
<id name="vid" column="vid" >
<generator class="increment"/>
</id>
<property name="pid" column="pid" />
<property name="uid" column="uid" />
<property name="tid" column="tid" />
<property name="votes" column="votes" />
</class>
</hibernate-mapping>
Please keep in mind, Hibernate is designed using the notion of Object Graph, which is a name given to the relational objects.
That core concept is missing in your mapping (Poll and Vote seem to be isolated) hence I doubt you can use HQL in its current state.
In my opinion, you have two options:
Define the relationship between Poll, pid and Vote, uid. Then you should be able to write simple HQL.
Use native SQL through Hibernate session itself.

NHibernate - mapping connection-table & back-references

Hey,
I have to map the following entities:
class Document
{
public int DocumentId { get; set; }
public DocumentList ContainingList { get; set; }
}
class DocumentList
{
public int DocumentListId { get; set; }
public DateTime LastUpdateTime { get; set; }
public IList<Doucment> Documents { get; set; }
}
With the constraint that only one DocumentList can own a specific document (altough a collection-table exists here).
Mapping has to rely on the following tables (which cannot be changed, for the sake of simplicity):
TB_DOC
------
DOC_ID (int, PK)
DOC_CONTENT (blob)
TB_DOC_LIST
-----------
DOC_LIST_ID (int, PK)
DOC_LIST_UPDATE_TIME (datetime)
TB_LIST_AND_DOCS
----------------
DOC_LIST_ID
DOC_ID
So the mapping i tohught of would be like this:
enter code here
<class name="DocumentList" table="TB_DOC_LIST">
<id name="DocumentListId">
<column name="DOC_LIST_ID"/>
<generator class="assigned" />
</id>
<property name="LastUpdateTime" column="DOC_LIST_UPDATE_TIME ">
<set name="Documents" table="TB_LIST_AND_DOCS">
<key column="DOC_ID"></key>
<one-to-many class="Document" />
</set>
</class>
and:
<class name="Document" table="TB_DOC">
<id name="DocumentId">
<column name="DOC_ID"/>
<generator class="assigned" />
</id>
[ ??? ] - property to reference the "owner" document list
</class>
Now, following the known patterns, i can't figure out how should the back-link from Document to the DocumentList be mapped, since i have a "weired| one-to-many relation here, broken by a third table.
I also don't want a Document object to reference an IList to solve this with back-referencing many-to-many, since each Document has only one such "owner" DocumentList.
Any elegant idea? what am i mispercepting here?
cant test it right now, but a join could be used to get the reference id.
<class name="Document" table="TB_DOC">
<id name="DocumentId">
<column name="DOC_ID"/>
<generator class="assigned" />
</id>
<join table="TB_LIST_AND_DOCS">
<key column="DOC_ID"/>
<many-to-one class="DocumentList">
<column name="DOC_LIST_ID" />
</many-to-one>
</join>
</class>

Insert to parent and child tables

Hi
I have parent and child table like below
<?xml version="1.0" encoding="utf-8"?>
------- parent --------------
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="HibernateSample.StudMarks,HibernateSample" table="StudMarks" lazy="false">
<id name="Sno" column="SNO" type="int">
<generator class="assigned"/>
</id>
<many-to-one name="Student" column="ID" not-null="true"/>
<property name="Marks" column="Marks" type="int" not-null="true" />
<property name="Rank" column="Rank" type="int" not-null="true" />
</class>
</hibernate-mapping>
------- child --------------
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="HibernateSample.Student,HibernateSample" table="Student" lazy="false">
<id name="Id" column="ID" type="int">
<generator class="native" />
</id>
<property name="Name" column="Name" type="string" not-null="true" />
<property name="Standard" column="Standard" type="string" not-null="true" />
<bag name="StudMarks" cascade="all" lazy="false">
<key column="ID" not-null="true"/>
<one-to-many class="HibernateSample.StudMarks,HibernateSample" />
</bag>
</class>
</hibernate-mapping>
and in .cs file i have like written code like below to insert into parent and child.
StudMarks sm = new StudMarks();
Student st = new Student();
List<StudMarks> sms = new List<StudMarks>();
st.Id = 9;
st.Name = "stud 999";
st.Standard = "99";
sm.Sno = 9;
sm.Marks = 99;
sm.Rank = 9;
sm.Student = st; ------ **Line 1**
st.StudMarks = sms; ------ **Line 2**
session.Save(sm);
session.Flush();
If i commENT "Line 1" in above code only child is inserting. If i comment "Line 2" foreign key constraint error is throwing.
Tables:
CREATE TABLE [dbo].[Student](
[ID] [int] IDENTITY(1,1) NOT NULL, -- PRIMARY KEY
[Name] [varchar](50) NOT NULL,
[Standard] [varchar](50) NOT NULL,
CREATE TABLE [dbo].[StudMarks](
[SNO] [int] NOT NULL, -- PRIMARY KEY
[ID] [int] NOT NULL, -- FOREIGN KEY
[Marks] [int] NOT NULL,
[Rank] [int] NOT NULL,)
Please let me know how to insert in parent and child at a time in single save operation.
You have a cascade relation from Student->StudentMarks. So you should be saving st...
session.Save(st);
You should also specify inverse="true" on the bag so you don't get double updates of the foreign key and a cascade="all-delete-orphan" is probably appropriate here as you want all StudentMarks to be deleted when the Student is deleted.
I believe you need to specify inverse="true" on the Student mapping of the StudMarks collection property, indicating that the StudMark mapping is the owner of the relationship.