MySQL using NHibernate unidirectional one to many mapping entity data recording

When performing the test code, the fruiting body and the parent entity can be saved to the database, but the parent entity ID record of the fruiting body corresponding to the data value in the table for all the null, database mysql, excuse me this is normal, whether to need to set up a two-way mapping entity will ID with Bu Baocun?
1) father entity definition:

class father
{
public virtual string name;
public virtual string id;
public virtual IList<string> children;
}

2) child entity definition:

class child
{
public virtual string id;
public virtual string name;
}

3) mapping file for father

<class name ="father" table="dbfather">
<id name="id" column ="id">
<generator class ="assigned"/>
</id>
<property name ="name"/>
<bag name="ichild">
<key column="fatherid" />
<one-to-many class="child" />
</bag>
</class>

4) mapping file for child

<class name ="child" table="dbchild">
<id name="id" column ="id">
<generator class ="assigned"/>
</id>
<property name ="name"/>
</class>

5) test codes

var cfg = new NHibernate.Cfg.Configuration().Configure("hibernate.cfg.xml");
using (ISessionFactory sessionFactory = cfg.BuildSessionFactory())
{
ISession session = sessionFactory.OpenSession();
try
{
Child c = new Child();
c.id= Guid.NewGuid().ToString();
c.name= "test";
Father f = new Father();
f.id = Guid.NewGuid().ToString();
f.name= "ftest";
f.children.Add(child);

session.Save(c);
session.Save(f);
}
finally
{
session.Flush();
}
}

Started by Curitis at November 25, 2016 - 1:17 PM

Print out the SQL yourself

Posted by Frederic at November 28, 2016 - 1:52 PM

Print is respectively insert to the parent entity correspondence table and the child entity two SQL corresponding to the table, but insert to the child entity corresponding table SQL does not include operating the parent entity ID field, then finally also does not have a UPDATE statement, therefore caused a problem, but I want to know is, there are problems whether you need to set the bidirectional mapping can solve the problem or what my own set of mapping.

Posted by Curitis at November 29, 2016 - 2:32 PM

Pure MYSQL can be obtained through the last_insert_id () the last inserted after the self increasing numbers. But in NHibernate if the implementation of recommendations to the board to consult.

Posted by Shelley at December 04, 2016 - 2:54 PM

<bag name="ichild">
<key column="fatherid" />
<one-to-many class="child" />
</bag>
The name property above to one end of the definition and the parent entity favorable is the children field above you.
The column property key and mapping file ID

Posted by Willie at December 16, 2016 - 3:32 PM