0

I need to query based on child objects of an ActiveRecord class (FindByFleetAndJob(), below). Normally I would use instances of the objects but in this case I don't have them, and don't want to do two visits to the database just to get objects for a query. I have attempted the following, creating dummy objects for the expressions:

[ActiveRecord("Alerts")]
public class Alert : ActiveRecordBase<Alert>
{
    [PrimaryKey]
    public int Id { get; set; }

    [BelongsTo("AssociatedFleetId")]
    public Fleet AssociatedFleet { get; set; }

    [BelongsTo("AssociatedJobId")]
    public Job AssociatedJob { get; set; }

    ...

    public static Alert[] FindByFleetAndJob(int fleetId, int jobId)
    {
        var criteria = new List<ICriterion>();
        criteria.Add(Expression.Eq("AssociatedFleet", new Fleet { Id = fleetId }));
        criteria.Add(Expression.Eq("AssociatedJob", new Job { Id = jobId }));
        return FindAll(criteria.ToArray());
    }
}

I have successfully done this with other queries but for some reason this one throws:

object references an unsaved transient instance - save the transient instance before flushing. Type: DataObjects.Job, Entity: DataObjects.FCJob#123

So my question is either, is there a way to combat this exception, or is there a way to alter the query to just pass the ids of the child objects, rather than objects themselves? Doing the following, understandably, does not find a property of name AssociatedJobId:

criteria.Add(Expression.Eq("AssociatedJobId", jobId));

although this is basically what I want to achieve.

Many thanks.

Chris Wallis
  • 1,263
  • 8
  • 20

1 Answers1

0

you could try the following code, but I am so not sure if it is correct:

  return FindAll(
      Subqueries.PropertyIn("AssociatedFleet", DetachedCriteria.For<Fleet>().Add(Expression.Eq("Id", fleetId)).SetProjection(Projections.Property("Id")))
    , Subqueries.PropertyIn("AssociatedJob", DetachedCriteria.For<Job>().Add(Expression.Eq("Id", jobId)).SetProjection(Projections.Property("Id")))
    );

Greetings Juy Juka

Juy Juka
  • 189
  • 1
  • 9