首页 \ 问答 \ 如何使用IHasManyConvention Fluent NHibernate约定在HasMany映射上设置PropertyRef?(How do I use the IHasManyConvention Fluent NHibernate convention to set the PropertyRef on a HasMany mapping?)

如何使用IHasManyConvention Fluent NHibernate约定在HasMany映射上设置PropertyRef?(How do I use the IHasManyConvention Fluent NHibernate convention to set the PropertyRef on a HasMany mapping?)

目前我正在使用Fluent NHibernate生成我的数据库模式,但我希望HasMany关系中的实体指向不同的列以供参考。 IE,这就是NHibernate在创建DDL时会生成的:

alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (Id);

这就是我想要的:

alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (EntityId);

其中Stable.ID是主键,Stable.EntityId只是我设置的另一列。

我已经有一个类看起来像这样:

public class ForeignKeyReferenceConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Cascade.All();
        //What goes here so that I can change the reference column?
    }
}

我需要做些什么才能让参考栏发生变化?

作为一个例子,这里是IReferenceConvention的代码看起来像做同样的事情:

    public class MyReferenceConvention : IReferenceConvention
    {
        public void Apply(IManyToOneInstance instance)
        {
            instance.PropertyRef("EntityId");
            instance.Cascade.All();
        }
    }

编辑: instance.Key.Column("EntityId")不是解决方案。


Currently I'm using Fluent NHibernate to generate my database schema, but I want the entities in a HasMany relationship to point to a different column for the reference. IE, this is what NHibernate will generate in the creation DDL:

alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (Id);

This is what I want to have:

alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (EntityId);

Where Stable.ID would be the primary key and Stable.EntityId is just another column that I set.

I have a class already that looks like this:

public class ForeignKeyReferenceConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Cascade.All();
        //What goes here so that I can change the reference column?
    }
}

What do I have to do to get the reference column to change?

As an example, here is what the code for IReferenceConvention looks like to do the same thing:

    public class MyReferenceConvention : IReferenceConvention
    {
        public void Apply(IManyToOneInstance instance)
        {
            instance.PropertyRef("EntityId");
            instance.Cascade.All();
        }
    }

EDIT: instance.Key.Column("EntityId") is not the solution.


原文:https://stackoverflow.com/questions/2344475
更新时间:2021-10-26 07:10

最满意答案

您可以使用<c:set>在任意范围内设置属性,并且可以使用${param}来访问请求参数。

<c:set var="foo" value="${param.foo}" scope="session" />

这基本上会做session.setAttribute("foo", request.getParameter("foo")); 在使用此行提交表单的JSP结果页面的响应呈现期间。 在此示例中,隐藏字段应具有名称foo

如果您确实需要将请求/响应转发到JSP 之前进行设置,那么您需要在提交表单的负责控制/后处理servlet类中(间接)执行此操作。

如果您确实需要显示表单期间设置它,那么只需使用<c:set>直接设置值,而不是从隐藏输入传递。 例如

<c:set var="foo" value="theValue" scope="session" />

You can use <c:set> to set an attribute in an arbitrary scope and you can use ${param} to access a request parameter.

<c:set var="foo" value="${param.foo}" scope="session" />

This will basically do session.setAttribute("foo", request.getParameter("foo")); during rendering of the response of the JSP result page with this line where the form is been submitted to. In this example, the hidden field should have the name foo.

If you actually need to set it before forwarding the request/response to the JSP, then you'll need to do this (indirectly) in the responsible controlling/postprocessing servlet class where the form is been submitted to.

If you actually need to set it during displaying the form, then just set the value directly using <c:set> instead of passing from a hidden input. E.g.

<c:set var="foo" value="theValue" scope="session" />

相关问答

更多
  • 如果您设置val标记,您可以从隐藏字段中检索值吗? 例如 $(document).ready(function(){ alert($('#foo').val()); }) Can you retrieve the value from the hidden field if you set the val tag? e.g.
  • 简单: $(document).ready(function() { $("#question_id").val($("#que_id").val()); }); Simple: $(document).ready(function() { $("#question_id").val($("#que_id").val()); });
  • 如果你的隐藏字段被命名为output ,只需用val函数替换text函数,如下所示: var input = $("#mobile-number"), output = $("#output"), input.on("keyup change", function() { var intlNumber = input.intlTelInput("getNumber"); if (intlNumber) { output.val("International: " + in ...
  • 试着用这个 if (nom !== "" && nom !== undefined) 所以,你的代码应该重写如下 function Confirm() { var nom = document.getElementById('hdNomValue').Value; if (nom !== "" && nom !== undefined) { // logic here } } try to use this if (nom !== "" & ...
  • 而不是使用隐藏字段,为什么不在表单中传递challenge_id? 否则,您可以在用户可以在该隐藏字段中输入他们想要的内容。 形成: form_for [@challenge, Idea.new] do |f| 接着: def create @challenge = Challenge.find(params[:challenge_id]) @idea = Idea.new(idea_params) @idea.challenge_id = @challenge.id end I ...
  • 使用$id_action = $_POST["id_action"]在php中执行此操作 检查您是否在此阶段传递了ID: 更改 if (isset($_POST['id_action'])) { $del_id = $_POST['id_action']; $delUser = new User; $delUser->deletarUsuario($del_id); ...
  • 您可以使用在任意范围内设置属性,并且可以使用${param}来访问请求参数。 这基本上会做session.setAttribute("foo", request.getParameter("foo")); 在使用此行提交表单的JSP结果页面的响应呈现期间。 在此示例中,隐藏字段应具有名称foo 。 如果您确实需要在将请求/响应转发到JSP 之前进行设置,那么您需要在提交表单的负责 ...
  • 在“页面B”的控制器中,将ViewBag.MyValue设置为会话变量并将其应用于隐藏的值。 调节器 ViewBag.MyValue = Session["MYVALUE"]; 视图 如果需要从JavaScript获取会话变量,则需要开发一个操作,该操作将返回会话变量并使用JavaScript / jQuery来使用它,如下所示: // Controller code [H ...
  • 你可能会误解jsp和javascript存在于同一个文件中。 是的但是服务器端的JSP部分编译本身就是client 。 中间的代码<% %>在服务器端执行。 你不能用Javascript做到这一点。 您需要为此发出服务器请求(有表单,Ajax,url..etc)。 Here is how I achieved my task successfully. I have created a new servlet and trigger an AJAX call as below. Then I set com ...
  • 给你的textarea命名,它会在你的隐藏输入中显示 所以隐藏的输入名称将是您的名字。 在PHP中你可以把它当成 $_POST['yourname'] 更新 这是工作小提琴 give name to your textarea and it will show it in your hidden input