gpt4 book ai didi

c# - hibernate "could not initialize a collection"

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:46 25 4
gpt4 key购买 nike

我的 NHibernate 一对多映射存在以下问题。如果我想通过我的 API 方法获取所有客户,我会收到以下错误:

"$id":"1","Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'text/html; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"$id":"2","Message":"An error has occurred.","ExceptionMessage":"could not initialize a collection: [Designet.Models.Customer.Orders#1][SQL: SELECT orders0_.CustomerId as Custom2_2_1_, orders0_.Id as Id1_2_1_, orders0_.Id as Id1_2_0_, orders0_.CustomerId as Custom2_2_0_, orders0_.Description as Descri3_2_0_, orders0_.Price as Price4_2_0_, orders0_.Created as Create5_2_0_, orders0_.Deadline as Deadli6_2_0_ FROM Order orders0_ WHERE orders0_.CustomerId=?]","ExceptionType":"NHibernate.Exceptions.GenericADOException","StackTrace":" w NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type)\r\n

下面我粘贴了我的代码。我在谷歌和 SO 上寻找我的问题的答案,但没有人提出的帮助。我不明白代码的哪一部分会引发错误。也许你有一些建议,我应该怎么做,来解决这个问题?我很高兴能得到你的帮助。

客户模型:

public class Customer
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }

public virtual IList<Order> Orders { get; set; }

public Customer()
{
Orders = new List<Order>();
}
}

客户映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"
assembly="Designet" namespace="Designet.Models">
<class name="Customer" table="Customer" dynamic-update="true" lazy="true" >
<cache usage="read-write"/>
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>
<property name="Name" />
<bag name="Orders" lazy="true" inverse="true" >
<key column="CustomerId"/>
<one-to-many class="Designet.Models.Order"/>
</bag>
</class>
</hibernate-mapping>

客户数据库:

CREATE TABLE [dbo].[Customer] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

订购型号:

 public class Order
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
public virtual decimal Price { get; set; }
public virtual DateTime Created { get; set; }
public virtual DateTime Deadline { get; set; }

public virtual int CustomerId { get; set; }
public virtual Customer Customer { get; set; }

}

订单映射:

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"
assembly="Designet" namespace="Designet.Models"
<class name="Order" table="Order" dynamic-update="true" lazy="true" >
<cache usage="read-write"/>
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>

<many-to-one name="Customer" column="CustomerId"/>

<property name="Description" />
<property name="Price" />
<property name="Created" />
<property name="Deadline" />
<property name="CustomerId" not-null="true" />

</class>
</hibernate-mapping>

订单数据库:

CREATE TABLE [dbo].[Order] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Description] NVARCHAR (MAX) NULL,
[Price] SMALLMONEY NULL,
[Created] DATETIME NOT NULL,
[Deadline] DATETIME NULL,
[CustomerId] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([Id])
);

最佳答案

您的评论 json 链接写入的内部异常 Incorrect syntax near the keyword 'Order'.您的表名为 Order ,这是一个 SQL 关键字。它必须逃脱。为此使用后倾。

<class name="Order" table="`Order`" ...

映射语义正确性注意事项:
bag应映射为 ICollection<>而不是 IList<> .一个IList<>用于映射 list :一个集合,其中每个元素都有一个索引记录在数据库中,从零到集合大小减一,没有任何空洞。您不太可能希望那样。

此外,对于您的模型,您应该使用 set而不是 bag : 一个 bag允许重复。您的模型不允许它们,一个订单在客户订单集合中可能只出现一次。如果您更改为 set , 使用 ISet<>在代码中。

关于您的问题的注释
您的错误消息已被截断,无法检查造成问题的原因。 GenericADOException应该包含另一个 InnerException ,但您提供的消息并未显示。

请获取完整的异常消息,并尝试以可读的方式发布:“ pretty-print ”您的 JSON,或者更好的是,获取异常 .ToString()输出。

关于c# - hibernate "could not initialize a collection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43218343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com