Re: [fluent-nhib] PersistenceSpecification and CheckList

2010-07-07 Thread Luis Abreu
Hello Paul.

Yes, I found that post :) Now, the thing is that my problem is still
happening, so I'm not sure on what's going on...I'm probably doing
something wrong, but don't know what...

I've improved my code and now I'm using something like this for
testing the collection:

new PersistenceSpecification(session)
.CheckProperty(e => e.Nif, _nif)
.CheckProperty( e =>e.Organismo, organismo)
.CheckProperty( e => e.Descricao, _descricao)
.CheckProperty( e => e.TipoOcorrencia,
TipoOcorrencia.Processo)
.CheckList( e =>e.Intervencoes, intervencoes,
( o, i) => o.ModificaEstado(i))
.VerifyTheMappings();

I expected that passing the action as 3rd parameter would be enough
for making sure that Intervencao has its Ocorrencia property set
(internally, the ModificaEstado method is responsible for adding
Intervencao to the inner collection maintained by the Ocorrencia and
for setting the Intervencao's Ocorrencia property to the Ocorrencia
object over which the call is made).

I've looked at the output and I've noticed that my code is generating
the following SQL statements (I'monly putting there the statements
related with Ocorrencia and Intervencao tables):

INSERT INTO Accoes (DataAccao, Utilizador, Descricao, Aplicacao)
VALUES (@p0, @p1, @p2, @p3); select SCOPE_IDENTITY();@p0 = 07-07-2010
14:16:51, @p1 = 'labreu', @p2 = 'This is just a test', @p3 = 'Test'

INSERT INTO Organismos (Nome) VALUES (@p0); select
SCOPE_IDENTITY();@p0 = 'Organismo teste'

INSERT INTO Intervencoes (Version, Guid, TipoEstado, Observacoes,
IdAccao, IdOcorrencias) VALUES (@p0, @p1, @p2, @p3, @p4, @p5); select
SCOPE_IDENTITY();@p0 = 1, @p1 = 5d7750b5-1ae1-4b80-9f11-a84d00afc45d,
@p2 = 0, @p3 = 'teste de intervenção', @p4 = 3808, @p5 = NULL

And then, I get the exception:

NHibernate.Exceptions.GenericADOException : could not insert:
[Sra.Ocorrencias.Intervencao][SQL: INSERT INTO Intervencoes (Version,
Guid, TipoEstado, Observacoes, IdAccao, IdOcorrencias) VALUES (?, ?,
?, ?, ?, ?); select SCOPE_IDENTITY()]
  > System.Data.SqlClient.SqlException : Cannot insert the value
NULL into column 'IdOcorrencias', table
'SRAEntidades.dbo.Intervencoes'; column does not allow nulls. INSERT
fails.
The statement has been terminated.

This happens because IdOcorrencia is set up as a foreign key: If I
remove the relationship between the 2 tables, then I've noticed that
my test code ends up exeucting the previous instructions and then it
executes another 2:
* insert into Ocorrencias
* performs an Update over the previous inserted Intervencao

Is this the right behavior? In my opinion, Ocorrencias should be the
first item saved into the db and only then should each Intervencao be
saved. Am I wrong?

I've built a small app which creates an Ocorrencia and it works fine
when I try to save it to the database (and I checked and made sure
that I had the relationship between the two tables when I run this
small test). Does this mean that I'm missing something in my tests?

Thanks
Luis

On Wed, Jul 7, 2010 at 12:51 PM, Paul Batum  wrote:
> Not sure if I'm on the right track exactly, but there are some overloads for
> CheckList that might help you.
> See this thread:
> http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/cebc70ff873e4fd2/ce6cc622bc02d9c
>
> On Mon, Jul 5, 2010 at 6:30 PM, Luis Abreu  wrote:
>>
>> Hello guys,
>>
>> I've got the following mappings:
>>
>> public class OcorrenciaMapping: ClassMap {
>>   HasMany(ocorrencia => ocorrencia.Intervencoes)
>>                    .Access.AsCamelCaseField(Prefix.Underscore)
>>                    .AsBag()
>>                    .Cascade
>>                    .All()
>>                    .KeyColumnNames.Add("IdOcorrencias")
>>                    .Not.LazyLoad();
>> }
>>
>> public class IntervencaoMapping: ClassMap {
>>         WithTable("Intervencoes");
>>            Not.LazyLoad();
>>            Id(intervencao => intervencao.Id)
>>                .ColumnName("IdIntervencoes")
>>                .WithUnsavedValue(0)
>>                .SetGeneratorClass("identity");
>>            Map(intervencao => intervencao.Guid, "Guid")
>>                .Not.Nullable();
>>            Version(ent => ent.Version)
>>               .ColumnName("Version");
>>            References(ent => ent.Action, "IdAccao")
>>                .Cascade
>>                .SaveUpdate();
>>            Map(intervencao => intervencao.TipoEstado, "TipoEstado")
>>                .CustomTypeIs(typeof (TipoEstado))
>>                .CustomSqlTypeIs("integer");
>>            Map(intervencao => intervencao.Observacoes,
>> "Observacoes");
>>            References(intervencao => intervencao.Ocorrencia,
>> "IdOcorrencias")
>>                   .Not.LazyLoad();
>> }
>>
>> Now, I'm trying to run the following test:
>>
>> var accao = CreateAction();
>> session.Save(accao);
>>
>> var organismo = CreateOrganismo(

Re: [fluent-nhib] PersistenceSpecification and CheckList

2010-07-07 Thread Paul Batum
Not sure if I'm on the right track exactly, but there are some overloads for
CheckList that might help you.

See this thread:
http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/cebc70ff873e4fd2/ce6cc622bc02d9c

On Mon, Jul 5, 2010 at 6:30 PM, Luis Abreu  wrote:

> Hello guys,
>
> I've got the following mappings:
>
> public class OcorrenciaMapping: ClassMap {
>   HasMany(ocorrencia => ocorrencia.Intervencoes)
>.Access.AsCamelCaseField(Prefix.Underscore)
>.AsBag()
>.Cascade
>.All()
>.KeyColumnNames.Add("IdOcorrencias")
>.Not.LazyLoad();
> }
>
> public class IntervencaoMapping: ClassMap {
> WithTable("Intervencoes");
>Not.LazyLoad();
>Id(intervencao => intervencao.Id)
>.ColumnName("IdIntervencoes")
>.WithUnsavedValue(0)
>.SetGeneratorClass("identity");
>Map(intervencao => intervencao.Guid, "Guid")
>.Not.Nullable();
>Version(ent => ent.Version)
>   .ColumnName("Version");
>References(ent => ent.Action, "IdAccao")
>.Cascade
>.SaveUpdate();
>Map(intervencao => intervencao.TipoEstado, "TipoEstado")
>.CustomTypeIs(typeof (TipoEstado))
>.CustomSqlTypeIs("integer");
>Map(intervencao => intervencao.Observacoes,
> "Observacoes");
>References(intervencao => intervencao.Ocorrencia,
> "IdOcorrencias")
>   .Not.LazyLoad();
> }
>
> Now, I'm trying to run the following test:
>
> var accao = CreateAction();
> session.Save(accao);
>
> var organismo = CreateOrganismo();
> session.Save(organismo);
>
> intervencao = CreateIntervencao();
> ((IHasAssignedDomainAction)intervencao).SetActionTo(accao);
> var intervencoes = new List {intervencao};
>
> new PersistenceSpecification(session)
>.CheckProperty(e => e.Nif, _nif)
>.CheckProperty( e =>e.Organismo, organismo)
>.CheckProperty( e => e.Descricao, _descricao)
>.CheckProperty( e => e.TipoOcorrencia,
> TipoOcorrencia.Processo)
>.CheckList( e => e.Intervencoes, intervencoes)
>.VerifyTheMappings();
>
>
> the problem is that I get an exception on the database saying that
>
> could not insert: [Sra.Ocorrencias.Intervencao][SQL: INSERT INTO
> Intervencoes (Version, Guid, TipoEstado, Observacoes, IdAccao,
> IdOcorrencias) VALUES (?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]
>  > System.Data.SqlClient.SqlException : Cannot insert the value
> NULL into column 'IdOcorrencias', table
> 'SRAEntidades.dbo.Intervencoes'; column does not allow nulls. INSERT
> fails.
> The statement has been terminated.
>
>
> In my domain, Intevencao will only get added to the collection through
> a method which ensures that its Ocorrencia property is set. But I'm
> not sure if there's any way to do that with the CheckList method.
>
> Any clues?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Fluent NHibernate" group.
> To post to this group, send email to fluent-nhibern...@googlegroups.com.
> To unsubscribe from this group, send email to
> fluent-nhibernate+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/fluent-nhibernate?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.



[fluent-nhib] PersistenceSpecification and CheckList

2010-07-05 Thread Luis Abreu
Hello guys,

I've got the following mappings:

public class OcorrenciaMapping: ClassMap {
   HasMany(ocorrencia => ocorrencia.Intervencoes)
.Access.AsCamelCaseField(Prefix.Underscore)
.AsBag()
.Cascade
.All()
.KeyColumnNames.Add("IdOcorrencias")
.Not.LazyLoad();
}

public class IntervencaoMapping: ClassMap {
 WithTable("Intervencoes");
Not.LazyLoad();
Id(intervencao => intervencao.Id)
.ColumnName("IdIntervencoes")
.WithUnsavedValue(0)
.SetGeneratorClass("identity");
Map(intervencao => intervencao.Guid, "Guid")
.Not.Nullable();
Version(ent => ent.Version)
   .ColumnName("Version");
References(ent => ent.Action, "IdAccao")
.Cascade
.SaveUpdate();
Map(intervencao => intervencao.TipoEstado, "TipoEstado")
.CustomTypeIs(typeof (TipoEstado))
.CustomSqlTypeIs("integer");
Map(intervencao => intervencao.Observacoes,
"Observacoes");
References(intervencao => intervencao.Ocorrencia,
"IdOcorrencias")
   .Not.LazyLoad();
}

Now, I'm trying to run the following test:

var accao = CreateAction();
session.Save(accao);

var organismo = CreateOrganismo();
session.Save(organismo);

intervencao = CreateIntervencao();
((IHasAssignedDomainAction)intervencao).SetActionTo(accao);
var intervencoes = new List {intervencao};

new PersistenceSpecification(session)
.CheckProperty(e => e.Nif, _nif)
.CheckProperty( e =>e.Organismo, organismo)
.CheckProperty( e => e.Descricao, _descricao)
.CheckProperty( e => e.TipoOcorrencia,
TipoOcorrencia.Processo)
.CheckList( e => e.Intervencoes, intervencoes)
.VerifyTheMappings();


the problem is that I get an exception on the database saying that

could not insert: [Sra.Ocorrencias.Intervencao][SQL: INSERT INTO
Intervencoes (Version, Guid, TipoEstado, Observacoes, IdAccao,
IdOcorrencias) VALUES (?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]
  > System.Data.SqlClient.SqlException : Cannot insert the value
NULL into column 'IdOcorrencias', table
'SRAEntidades.dbo.Intervencoes'; column does not allow nulls. INSERT
fails.
The statement has been terminated.


In my domain, Intevencao will only get added to the collection through
a method which ensures that its Ocorrencia property is set. But I'm
not sure if there's any way to do that with the CheckList method.

Any clues?

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.