首页 \ 问答 \ 如何通过ID加入实体(How to join entity by id)

如何通过ID加入实体(How to join entity by id)

你能帮我解决一个小问题吗? 这是关于Spring Data,JPA和Hibernate的项目。

我有两个实体,我需要一个到另一个。 是的,这听起来很简单:)

首先是活动:

public class Activity {
  ...

  @Column(name="STATUS_ID")
  private String statusId;

  ...
}

其次是Vocab值(用户可以将它作为一个简单的值列表):

public class VocabValue {
  ...

  // Type of vocab (in our case - 'ACT_STAT')
  @Column(name="TYPE")
  private String type;

  // Name - code of Vocab value
  @Column(name="NAME")
  private String type;

  // Value
  @Column(name="VAL")
  private String value;

  ...
}

在我的情况下,我想在一个活动中存储一个Vocab值的名称并加入它。

在目标状态下,我的实体应该如下所示:

public class Activity {
  ...

  @Column(name="STATUS_ID")
  private String statusId;

  @... what?
  private String status;

  // Getter returns Vocab Value in accordance with the following join spec (pseudo-sql):
  // select voc.value from VocabValue voc where voc.name = %statusId% and type = 'ACT_STAT'
  public String getStatus() {
  }

  ...
}

我应该如何配置我的活动实体来提供这个? 好的,这可能吗?

我会很感激你的建议!

谢谢!

更新13.01

我试过这个解决方案:

public class Activity {

  ...
  @Column(name="STATUS_ID")
  private String statusId;

  @JoinColumn(name = "STATUS_ID", referencedColumnName = "NAME",   insertable = false, updatable = false)
  @Where(clause = "type = 'ACT_STAT'")
  @ManyToOne(optional = true)
  @NotFound(action = NotFoundAction.IGNORE)
  private VocabValue status;

  ...
}

此外,我必须使VocabValue实现Serializable(VocabValue作为Detached实体应该按照Hibernate规范的Serializable接口的实现)。

public VocabValue implemets Serializable {...}

它可以工作,但注解@Where不起作用:仅使用@JoinColumn规范执行联接,在@Where子句中定义的附加条件不适用。

是否有解决方法?

更新13.01 - 2

它通过以下配置解决:

@JoinFormula(value="SELECT v.ROW_ID FROM VOCAB v WHERE v.NAME=STATUS_ID AND v.TYPE='ACT_STAT'") 
@ManyToOne 
@NotFound(action = NotFoundAction.IGNORE) 
private VocabValue status;

谢谢!)


Could you help me solve a small question? It's about the project with Spring Data, JPA and Hibernate.

I have 2 entities and I need to join one to another. Yes, it sounds simple :)

First is Activity:

public class Activity {
  ...

  @Column(name="STATUS_ID")
  private String statusId;

  ...
}

Second is Vocab Value (Users can administer it as a simple list of values):

public class VocabValue {
  ...

  // Type of vocab (in our case - 'ACT_STAT')
  @Column(name="TYPE")
  private String type;

  // Name - code of Vocab value
  @Column(name="NAME")
  private String type;

  // Value
  @Column(name="VAL")
  private String value;

  ...
}

In my case, I want to store in an Activity a name of Vocab Value and join it.

In the target state, my entity should look like:

public class Activity {
  ...

  @Column(name="STATUS_ID")
  private String statusId;

  @... what?
  private String status;

  // Getter returns Vocab Value in accordance with the following join spec (pseudo-sql):
  // select voc.value from VocabValue voc where voc.name = %statusId% and type = 'ACT_STAT'
  public String getStatus() {
  }

  ...
}

How should I configure my Activity entity to provide this? Ok, Is it possible?

I'll be grateful for your advice!

Thank!

Update 13.01

I've tried this solution:

public class Activity {

  ...
  @Column(name="STATUS_ID")
  private String statusId;

  @JoinColumn(name = "STATUS_ID", referencedColumnName = "NAME",   insertable = false, updatable = false)
  @Where(clause = "type = 'ACT_STAT'")
  @ManyToOne(optional = true)
  @NotFound(action = NotFoundAction.IGNORE)
  private VocabValue status;

  ...
}

Moreover, I had to make VocabValue implementation of Serializable (VocabValue as Detached entity should be implemetation of Serializable interface in accordance with Hibernate spec).

public VocabValue implemets Serializable {...}

It works, but annotation @Where doesn't work: Join performed only with @JoinColumn specification, an additional criteria, defined in @Where clause doesn't apply.

Whether there are workarounds?

UPDATE 13.01 - 2

It was solved with following configuration:

@JoinFormula(value="SELECT v.ROW_ID FROM VOCAB v WHERE v.NAME=STATUS_ID AND v.TYPE='ACT_STAT'") 
@ManyToOne 
@NotFound(action = NotFoundAction.IGNORE) 
private VocabValue status;

Thanks!)


原文:https://stackoverflow.com/questions/41624890
更新时间:2022-05-22 17:05

最满意答案

使用fullPage.js提供的回调,例如afterLoadonSlideLeave

 $('#fullpage').fullpage({
     afterLoad: function (anchorLink, index) {
         var loadedSection = $(this);

         //section 3 finished loading
         if (index == 3) {
             $('#myElement').addClass('demo');

             $('#myElement2').removeClass('demo');
         }

     }
 });

Use the callbacks provided by fullPage.js such as afterLoad or onSlideLeave.

 $('#fullpage').fullpage({
     afterLoad: function (anchorLink, index) {
         var loadedSection = $(this);

         //section 3 finished loading
         if (index == 3) {
             $('#myElement').addClass('demo');

             $('#myElement2').removeClass('demo');
         }

     }
 });

相关问答

更多
  • 您始终可以使用HTML 5历史记录API。 您可以使用onLeave或afterLoad等回调: $('#fullpage').fullpage({ afterLoad: function(anchorLink, index) { history.pushState(null, null, "bar.html"); } }); 考虑到您需要为旧浏览器使用一些polyfill。 或者直接使用history.js 。 请注意,上面的示例可能无法在某些浏览器上的localhost ...
  • 使用fullPage.js提供的回调,例如afterLoad或onSlideLeave 。 $('#fullpage').fullpage({ afterLoad: function (anchorLink, index) { var loadedSection = $(this); //section 3 finished loading if (index == 3) { $('#myElement').add ...
  • 确保您不要多次初始化插件。 (这是使用ajax技术的人们非常常见的错误,例如Rails框架使用的技术) 您在main.js和HTML文件中两次调用fullPage.js。 Make sure you dont initialize the plugin multiple times. (which is a very common error in people using ajax techniques such as the ones used by Rails framework) You are c ...
  • 只需使用fancybox的一个回调,如onStart ,然后调用fullpage方法$.fn.fullpage.setKeyboardScrolling(false); 禁用全页键盘功能。 然后再次激活它,使用相同的方法但值相反的onClosed 。 $(".fancybox").fancybox({ openEffect : 'none', closeEffect : 'none', keys : true, onStart: function() { ...
  • 假设你有4个像这样的块。 你需要在最后一节滚动条确定。 所以我们可以在CSS和JQuery的帮助下完成。 使用CSS 使用JQuery首先使用JQuery库文件。 $(document).ready(function () { $('.sec_4').css({ 'overflow': 'a ...
  • 使用fullpage.js插件提供的选项normalScrollElements : 从文档 : normalScrollElements :(默认为null)如果要在滚动某些元素时避免自动滚动,这是您需要使用的选项。 (对于地图,滚动div等有用)它需要一个带有jQuery选择器的字符串用于这些元素。 (例如:normalScrollElements:'#element1,.element2') Using the option normalScrollElements that fullpage.js ...
  • javascript控制台显示此错误: 未捕获的TypeError:对象#没有方法'easeInQuart' 这意味着您不会导入插件所需的jQuery UI库,如文档中所指定。