我在网格上进行分层数据绑定,我需要让数据库服务器对我的对象执行排序.我可以轻松地对父集合进行排序,但我似乎无法弄清楚如何对所有子集合进行排序.我有一个模型,其中嵌套了3个子集合,并且所有这些集合都需要进行排序.
这是我想要完成的示例模型:
public class Year
{
public int Id { get; set; }
public string Name { get; set; }
public List<Make> Makes { get; set; }
}
public class Make
{
public int Id { get; set; }
public string Name { get; set; }
public List<Model> Models { get; set; }
}
public class Model
{
public int Id { get; set; }
public string Name { get; set; }
public List<Color> Colors { get; set; }
} …Run Code Online (Sandbox Code Playgroud) 基本上我想要加载属性.我有以下HQL查询:
SELECT u.id AS id, u.name AS text, u AS obj FROM User AS u fetch all properties
Run Code Online (Sandbox Code Playgroud)
我希望这只执行一个查询.相反,我得到了N + 1个查询.
代码如下:
Query q = mySession.createQuery(
"SELECT u.id AS id, u.name AS text, u AS obj FROM User AS u fetch all properties")
.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
for (Iterator i = q.iterate(); i.hasNext();) {
Object line = i.next();
System.out.println(line);
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出(hibernate.show_sql设置为true)是:
Hibernate: select user0_.id as col_0_0_, user0_.name as col_1_0_, user0_.id as col_2_0_ from user user0_
Hibernate: select user0_.id as …Run Code Online (Sandbox Code Playgroud) 我有以下代码不起作用:
$threads = MessageThread::with('last_message', 'thread_visibility')
->where('message_thread_visibility.user_id', Auth::user()->id)->get();
Run Code Online (Sandbox Code Playgroud)
在Laravel中为一个急切加载的多关系雄辩查询添加"where"子句的最佳方法是什么?
我有两个具有单向one to many关系的实体。
@Entity
public class Basket {
@Id
@GeneratedValue
private Long id;
private int capacity;
}
@Entity
public class Item {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
private Basket basket;
}
Run Code Online (Sandbox Code Playgroud)
我保存了两个对象:
Basket basket1 = new Basket(100);
Basket basket2 = new Basket(200);
Basket basket3 = new Basket(300);
basketRepository.save(asList(basket1, basket2, basket3));
Item item1 = new Item("item1", basket1);
Item item11 = new Item("item11", basket1);
Item item2 = new Item("item2", basket2);
Item item22 = new Item("item22", basket2); …Run Code Online (Sandbox Code Playgroud) 我有一个完整的角度应用程序,它使用急切加载。我想将其转换为延迟加载,但是因为我对所有路线都有保护,并且所有路线都是受保护的一条主路线的子路线,所以我不知道是否可以做到这一点并仍然使其发挥作用就像急切加载一样。
这是我在 app-routing.module 中的路由数组:
// Routing array - set routes to each html page
const appRoutes: Routes = [
{ path: 'login/:id', canActivate: [AuthGuard], children: [] },
{ path: '', canActivateChild: [AuthGuard], children: [
{ path: '', redirectTo: '/courses', pathMatch: 'full' },
{ path: 'courses', component: CourseListComponent, pathMatch: 'full'},
{ path: 'courses/:courseId', component: CourseDetailComponent, pathMatch: 'full' },
{ path: 'courses/:courseId/unit/:unitId', component: CoursePlayComponent,
children: [
{ path: '', component: CourseListComponent },
{ path: 'lesson/:lessonId', component: CourseLessonComponent, data:{ type: 'lesson'} },
{ …Run Code Online (Sandbox Code Playgroud)routes lazy-loading eager-loading angular angular-router-guards
我目前正在学习更多关于Linq-To-Entities的内容 - 特别是关于渴望和懒惰加载的那一刻.
proxy.User.Include("Role").First(u => u.UserId == userId)
Run Code Online (Sandbox Code Playgroud)
这应该加载用户以及用户拥有的任何角色.我有一个问题,但我也有一个问题.这只是为了解L2E而创建的一个简单模型
我的印象是,这是为了让事情变得强烈 - 所以我为什么要写"角色"?看来,如果我更改了表的名称,那么这不会产生编译错误......
我的错误是这样的:
The specified type member 'Roles' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Run Code Online (Sandbox Code Playgroud)
下面的解决方案允许我现在编写代码:
proxy.User.Include(u => u.Role).First(u => u.UserId == userId)
Run Code Online (Sandbox Code Playgroud)
哪个好多了!
linq-to-entities entity-framework lazy-loading eager-loading
我在1:n关系中有两个实体:类别和产品.
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public virtual Product { get; set; }
}
public class context : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想通过Eager加载来加载每个类别的产品.
context.Categories.Include(c=>c.Products)
Run Code Online (Sandbox Code Playgroud)
但Include不加载任何导航属性.它只接受一个名为"path"的字符串参数.
我有这个
using (ITransaction transaction = session.BeginTransaction())
{
Task tAlias = null;
CompletedTask cAlias = null;
List<Task> tasks = session.QueryOver<Task>(() => tAlias)
.Where(Restrictions.In(Projections.Property(() => tAlias.Course.Id), courseIds))
.Fetch(pt => pt.PersonalTaskReminders).Eager
.List<Task>().ToList().ConvertToLocalTime(student);
transaction.Commit();
return tasks;
}
PersonalTaskReminders == Collection
Run Code Online (Sandbox Code Playgroud)
因此任务可以有许多personalTaskReminders.我发现如果我设置2个personalTaskReminders(所以PersonalTaskReminders现在将从db的集合中有2行)
它返回两次相同的任务.
所以,如果我有50个personaltaskReminders用于该任务.我会得到50个相同任务的结果.我不明白为什么.
如果我删除了急切的加载.我按照预期从数据库中获取了一个任务.
我正在努力解决上面的错误.我在这里发现了不同的答案(堆栈溢出),但它们都没有解决我与错误相关的问题.
我只是在我的ConnectionString中启用MARS但没有成功.
我有一类产品
public class Product
{
public Product()
{
this.Additives = new HashSet<Additive>();
}
public int Id { get; set; }
public string Name { get; set; } // refrigerante
public string CommercialName { get; set; } // nome popular, ex: fanta laranja
public string Brand { get; set; } // marca, ex: Coca-cola
public string Details { get; set; } // composicao, ingredientes
public HalalState HalalState { get; set; } // estado: halal, haram ou desconhecido
public DateTime? …Run Code Online (Sandbox Code Playgroud) 我正在使用此查询来获取我的数据
user = User.includes(:skills).order(user: :id)
Run Code Online (Sandbox Code Playgroud)
它工作正常。但是当我尝试按字母顺序显示技能时,如下所示
user.skills.order(name: :asc)
Run Code Online (Sandbox Code Playgroud)
它在日志中显示它作为order()一种activerecord方法进入数据库。这似乎eager loading失败了,因为无论如何必须将急切加载放入数据库中,这有什么用呢?
谁能指导我这样做的好方法是什么。
activerecord ruby-on-rails eager-loading ruby-on-rails-4 rails-activerecord
eager-loading ×10
lazy-loading ×4
hibernate ×2
java ×2
activerecord ×1
angular ×1
c# ×1
eloquent ×1
fetch ×1
hql ×1
jpa ×1
laravel ×1
laravel-4 ×1
nhibernate ×1
queryover ×1
routes ×1
sorting ×1
spring-data ×1
sql-server ×1