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: Null object in array
PostPosted: Thu Jun 19, 2008 10:38 am 
Newbie

Joined: Mon Aug 01, 2005 3:07 am
Posts: 10
Hello,

I have a master and detail tables. There is one record in master and 3 records of its detail.

I'm using arrays. I can't use list - collection types because I'm using these classes in web service, and collection types are not deserializable.

When I select a master record, nhibernate sets 4 record to Details array and first of them is null...

Have you got any idea about it?

Table definitions

Code:
Create table Master(
Id int,
MasterName nvarchar(50)
)

Create table Detail(
Id int,
MasterId int,
DetailExp nvarchar(50)
)


And corresponding classes:

Code:
public class Master
    {
        private Detail[] _Details;
        private int _Id;
        private string _MasterName;
     
        public Detail[] Details
        {
            get
            {
                return _Details;
            }
            set { _Details = value; }
        }
        public int Id
        {
            get { return _Id; }
            set { _Id = value; }
        }
        public string MasterName
        {
            get { return _MasterName; }
            set { _MasterName = value; }
        }
}

public class Detail
    {

        private string _DetailExp;
        private int _Id;       
       
        public string DetailExp
        {
            get { return _DetailExp; }
            set { _DetailExp = value; }
        }
        public  int Id
        {
            get { return _Id; }
            set { _Id = value; }
        }
    }


Mapping Files:

Master.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" default-cascade="save-update" auto-import="true">
<class name="WSTest.Entities.Master, WSTest" lazy="false" table="Master">
<id name="Id" type="int" column="Id" access="field.pascalcase-underscore">
<generator class="native" />
</id>
<array name="Details" cascade="all-delete-orphan" access="field.pascalcase-underscore" >
<key column="MasterId" />
<index column="Id"/>
<one-to-many class="WSTest.Entities.Detail,WSTest" />
</array>
<property name="MasterName" column="MasterName" access="field.pascalcase-underscore" not-null="true" />
</class>
</hibernate-mapping>


Detail.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" default-cascade="save-update" auto-import="true">
<class name="WSTest.Entities.Detail, WSTest" lazy="false" table="Detail">
<id name="Id" type="int" column="Id" access="field.pascalcase-underscore">
<generator class="native" />
</id>
<property name="DetailExp" column="DetailExp" access="field.pascalcase-underscore" not-null="true" />
</class>
</hibernate-mapping>


Data in tables:

select * from Master,Detail where detail.MasterId = Master.Id

Id MasterName Id MasterId DetailExp
1 Master1 1 1 Detail1
1 Master1 2 1 Detail2
1 Master1 3 1 Detail3

Web Service output:

- <Master>
- <Details>
<Detail xsi:nil="true" />
- <Detail>
<DetailExp>Detail1</DetailExp>
<Id>1</Id>
</Detail>
- <Detail>
<DetailExp>Detail2</DetailExp>
<Id>2</Id>
</Detail>
- <Detail>
<DetailExp>Detail3</DetailExp>
<Id>3</Id>
</Detail>
</Details>
<Id>1</Id>
<MasterName>Master1</MasterName>
</Master>
- <Master>
<Details />
<Id>2</Id>
<MasterName>Master2</MasterName>
</Master>

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.