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.