0

I have a problem. When I try to insert an object with the type book to the DB, It should add the author also to the authors table if it doesnt exist. Instead I get an error 'unsaved object' and the object has not been saved. whats the problem?

the code:

Book:
            Id(x => x.ID);
            Map(x => x.Title);
            References(x => x.Author)
            .Not.LazyLoad()
            .Column("Author_id");

Author:
            Id(x => x.ID);
            Map(x => x.FullName);
Paul Phillips
  • 6,093
  • 24
  • 34
user1430430
  • 21
  • 1
  • 6
  • What is the mapping? From what u said it seems to be a problem with ID mapping or some cascade – mynkow Jun 17 '12 at 20:52
  • @user1430430 you're getting that error because the author does not exist in the database. Author is really the root entity so you should save the author object first – clyc Jun 18 '12 at 19:32
  • it does exist in the db.if I add the author manually it does work and if i get it from the db it has the author object.what to do? – user1430430 Jun 19 '12 at 14:31

1 Answers1

0
References(x => x.Author)
    .Not.LazyLoad()
    .Cascade.All()         // unsaved Authors should also be saved
    .Column("Author_id");

don't forget you have to use session.Get/session.Load to get existing Authors

Firo
  • 30,626
  • 4
  • 55
  • 94
  • does not work!Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Library.Author#1] what can i do? – user1430430 Jun 21 '12 at 17:01
  • did you use the same session to get the author and to save the book? when not use `session.Lock(author, LockMode.None);` – Firo Jun 22 '12 at 05:46
  • I did it in generic type...so i cannot use it....there is maybe another option?evertything is mapping correctly.thank u !! – user1430430 Jun 22 '12 at 07:05
  • the best option is to load it with the same session you use to save the book since this is the UnitOfWork(session) – Firo Jun 22 '12 at 08:49
  • how can it be possible?and if the program is shut down...i see that the save shuold be auto and i cannot understant y not – user1430430 Jun 22 '12 at 15:01