سؤال

I have probably read each Stackoverflow post similar to this problem but just couldn't figure out what is happening.

I have to classes Student and Educational Background, a student can have one or more educational background making this a one-to-many relationship.

Mapping files:

Student.hbm.xml

<class name="Student" table="student">
<id name="Id" column="id">
  <generator class="guid" />
</id>
<property name="Code" column="code" />
<property name="DateTimeCreated" column="datetime_created" />
<property name="FirstName" column="first_name" />
<property name="MiddleName" column="middle_name" />
<property name="LastName" column="last_name" />
<property name="BirthDate" column="birth_date" />
<property name="HomePhone" column="home_phone" />
<property name="Mobile" column="mobile" />
<property name="HomeAddress" column="home_address" />
<property name="CountryCode" column="country_code" />
<property name="ZipCode" column="zip_code" />
<property name="MotherName" column="mother_name"/>
<property name="MotherAddress" column="mother_address" />
<property name="MotherContactNo" column="mother_contact_no" />
<property name="FatherName" column="father_name" />
<property name="FatherAddress" column="father_address" />
<property name="FatherContactNo" column="father_contact_no" />
<property name="CreatedById" column="created_by_id" />
<bag name="EducationalBckgrnd" table="student_educ_bckgrnd" inverse="true" cascade="all">
  <key column="student_id" />
  <one-to-many class="StudentEducBckgrnd" />
</bag>

StudentEducBckgrnd.hbm.xml

<class name="StudentEducBckgrnd" table="student_educ_bckgrnd">
<id name="Id" column="id">
  <generator class="guid" />
</id>
<!--<property name="StudentId" column="student_id" type="System.Guid" insert="false" />-->
<property name="SchoolName" column="school_name" />
<property name="Remarks" column="remarks" />
<property name="From" column="from" />
<property name="To" column="to" />

<many-to-one name="EnrolledStudent" class="Student" column="student_id" />

If I DON'T comment out the StudentId property above, I get Parameter index error.

Student.cs

public class Student
{
    public virtual IList<StudentEducBckgrnd> EducationalBckgrnd { get; set; }
    public virtual Guid Id { get; set; }
    public virtual string Code { get; set; }
    public virtual DateTime DateTimeCreated { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string MiddleName { get; set; }
    public virtual string LastName { get; set; }
    public virtual DateTime BirthDate { get; set; }
    public virtual string HomePhone { get; set; }
    public virtual string Mobile { get; set; }
    public virtual string HomeAddress { get; set; }
    public virtual string CountryCode { get; set; }
    public virtual string ZipCode { get; set; }
    public virtual string MotherName { get; set; }
    public virtual string MotherAddress { get; set; }
    public virtual string MotherContactNo { get; set; }
    public virtual string FatherName { get; set; }
    public virtual string FatherAddress { get; set; }
    public virtual string FatherContactNo { get; set; }
    public virtual Guid CreatedById { get; set; }

    public Student()
    {
        EducationalBckgrnd = new List<StudentEducBckgrnd>();
    }
}

StudentEducBckgrnd.cs

public class StudentEducBckgrnd
{
    public virtual Guid Id { get; set; }
    public virtual Guid StudentId { get; set; }
    public virtual string SchoolName { get; set; }
    public virtual string From { get; set; }
    public virtual string To { get; set; }
    public virtual string Remarks { get; set; }

    public virtual Student EnrolledStudent { get; set; }
}

If I use all of the above code, I get the following error:

NHibernate.Exceptions.GenericADOException was caught

HResult=-2146232832 Message=could not insert: [EnrollmentSystem.Domain.StudentEducBckgrnd#6a0fdb25-18ad-4658-b8b3-e62b8d220649][SQL: INSERT INTO student_educ_bckgrnd (school_name, remarks, from, to, student_id, id) VALUES (?, ?, ?, ?, ?, ?)] Source=NHibernate SqlString=INSERT INTO student_educ_bckgrnd (school_name, remarks, from, to, student_id, id) VALUES (?, ?, ?, ?, ?, ?) StackTrace: at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) at NHibernate.Engine.ActionQueue.ExecuteActions() at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) at NHibernate.Impl.SessionImpl.Flush() at NHibernate.Transaction.AdoTransaction.Commit() at EnrollmentSystem.Business.CStudent.SaveStudent(Student student, List`1 educationList) in d:\Admiral\projects\EnrollmentSystem\EnrollmentSystem\Business\CStudent.cs:line 54 InnerException: MySql.Data.MySqlClient.MySqlException HResult=-2147467259 Message=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, to, student_id, id) VALUES ('fdsaf', 'asdf', '2014-01-28', '2014-01-28', '' at line 1 Source=MySql.Data ErrorCode=-2147467259 Number=1064 StackTrace: at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) InnerException:

I'm using MySQL and I'm building a C# winforms application, the error tells me that I am having an SQL syntax error but I could not figure out why.

Any help would be greatly appreciated.

NEWBIE

هل كانت مفيدة؟

المحلول

The problem here, would be in the keyword. This mapping

<property name="From" column="from" />

says, that the column name is from. But FROM is the key-word (almost for any DB engine I'd bet...). SO what we need to do is to escape that setting with the special sign: `

(I am not absolutely sure about that sign, because on MS SQL Server I do use this style: [from] )

<property name="From" column="`from`" />

Check the exception as well

...for the right syntax to use near 'from, to, student_id,...

By the way, the correct mapping for column used twice is like this

<many-to-one  name="EnrolledStudent" class="Student"  column="student_id" />
<property     name="StudentId"    type="System.Guid"  column="student_id" 
              insert="false" update="false" />

In this case, the INSERT or UPDATE would use the column student_id only once, the reference mapping

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top