All my previous projects the key was assigned by the database, but
this new project I'm implementing today's best techniques (e.g.
GuidComb) and using ORM (e.g. ActiveRecord).
I created the TransactionGroupUser object explicitly. Is this the
right approach of handling multiple collections (e.g. IList) for the
association table with surrogate key? Source code snippet below:
public static void publishRequisition(Models.Requisition
requisition, IList<Models.Group> groups, IList<Models.User> users) {
Models.Transaction obj1 = new Models.Transaction() {
transactionType =
Testsoft.Data.Models.Transaction.enumType.Requisition,
transactionCreator = requisition.requisitionCreator,
transactionRequisition = requisition,
transactionDateCreated = DateTime.Now
};
for(int i = 0;i < groups.Count;i++) {
Models.TransactionGroupUser obj2 = new
Testsoft.Data.Models.TransactionGroupUser() {
entityTransaction = obj1,
entityGroup = groups[i]
};
obj2.Create();
}
for(int j = 0;j < users.Count;j++) {
Models.TransactionGroupUser obj2 = new
Testsoft.Data.Models.TransactionGroupUser() {
entityTransaction = obj1,
entityUser = users[j]
};
obj2.Create();
}
}
By the way, thank you Markus for helping me out and replying to all my
previous emails.
Sincerely,
William Chang
On Aug 3, 4:38 pm, Markus Zywitza <[email protected]> wrote:
> To be more precise, it cannot work with an NHibernate-assigned ID value. You
> can let NHibernate populate the an association table with surrogate key
> automatically, but this works only with a key that can be assigned by the DB
> (Identity, rowid() etc.) when all other attributes are nullable (they will
> be null by default).
>
> -Markus
>
> 2009/8/3 Markus Zywitza <[email protected]>
>
>
>
> > You have to create the TransactionGroupUser object explicitly.
>
> > -Markus
>
> > 2009/8/3 William Chang <[email protected]>
>
> >> I'm having trouble with using many-to-many relation,
> >> "HasAndBelongsToMany" attribute. I read the section titled
> >> "Association table with surrogate key" in the documentation and does
> >> not extend on using with "Create()":
>
> >>http://www.castleproject.org/activerecord/documentation/trunk/usersgu...
>
> >> I do not know if I should create the object from the association table
> >> or not? I tried creating an object from one of the foreign tables, but
> >> I got an error:
>
> >> ----> NHibernate.Exceptions.GenericADOException : could not insert
> >> collection:
>
> >> [Testsoft.Data.Models.Transaction.transactionGroups#43fc1ed4-3919-4d12-989b
> >> -9c590167bb15]
> >> [SQL: INSERT INTO TransactionsGroupsUsers (entityTransactionId,
> >> entityGroupId) VALUES (?p0, ?p1)]
> >> ----> MySql.Data.MySqlClient.MySqlException : Duplicate entry '' for
> >> key 1
>
> >> About my association table, I have three tables connecting to it by
> >> foreign key: Transactions, Groups, and Users. I found that the
> >> surrogate key is not being generating by ActiveRecord when using
> >> "Create()" method. The surrogate key column ends up being blank after
> >> INSERT.
>
> >> Test Code Snippet:
> >> IList<Data.Models.Group> gs1 = new List<Data.Models.Group>();
> >> gs1.Add(g1);
>
> >> IList<Data.Models.User> us1 = new List<Data.Models.User>();
> >> us1.Add(u1);
>
> >> Models.Transaction t1 = new Models.Transaction() {
> >> transactionType =
> >> Testsoft.Data.Models.Transaction.enumType.Requisition,
> >> transactionCreator = requisition.requisitionCreator,
> >> transactionRequisition = requisition,
> >> transactionGroups = gs1,
> >> transactionUsers = us1,
> >> transactionDateCreated = DateTime.Now
> >> };
>
> >> t1.Create();
>
> >> Source Code Snippet:
> >> public class TransactionGroupUser :
> >> ActiveRecordBase<TransactionGroupUser> {
> >> [PrimaryKey(PrimaryKeyType.GuidComb)]
> >> public virtual Guid entityId {get;set;}
> >> [BelongsTo("entityTransactionId")]
> >> public virtual Transaction entityTransaction {get;set;}
> >> [BelongsTo("entityGroupId")]
> >> public virtual Group entityGroup {get;set;}
> >> [BelongsTo("entityUserId")]
> >> public virtual User entityUser {get;set;}
> >> }
>
> >> Specs:
> >> Microsoft ASPNET 3.5
> >> MySQL 5.0.24a
> >> MySQL Connector/Net 6.1.0
> >> NHibernate 2.1.0GA
> >> Castle ActiveRecord 2.0
>
> >> Please help?
>
> >> Thank you,
> >> William Chang
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---