首页 \ 问答 \ 玩框架加入表(Play Framework Join Table)

玩框架加入表(Play Framework Join Table)

我想要做的是根据用户ID将表连接到另一个表。

我有一个用户表,以及一个警报历史记录表,其中包含与用户ID相关联的外键。

我使用的是Netbeans 7.4生成的实体。

用户:

    @Entity
@Table(name = "Users", schema = "dbo")
public class User extends Model implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "UserID")
    private Integer userID;
            @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "userId")
            private Collection<AlertHistory> alertHistoryCollection;
     { ... }

警报历史:

    @Entity
@Table(name = "Alert_History", schema = "dbo")
public class AlertHistory extends Model implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    @Basic(optional = false)
    @Column(name = "user_id")
    @ManyToOne(fetch = FetchType.EAGER)
    private int userId;
    @Basic(optional = false)
    @Column(name = "message_id")
    private long messageId;
    @Basic(optional = false)
    @Lob
    @Column(name = "alerts_triggered")
    private String alertsTriggered;
            {...}

我期望的是,当我加载用户时,它将尝试基于userId加入AlertsHistory表结果。 但是,在运行时, AlertsHistory Collection始终为null

更新:已解决

我看错了方法。 为了进行JOIN,AlertHistory期望被传递一个User对象,而不仅仅是userId键。 将User字段添加到AlertHistory模型,并将@OneToMany(mappedBy = "userId")更改为@OneToMany(mappedBy = "user" ,然后添加@JoinColumn(name="user_id", referencedColumnName="userId") private User user;AlertHistory那个User字段让我整理好了!感谢大家的帮助。


What I'm trying to do is to join a table to another table based on a user id.

I have a user table, and an alerts history table that has a foreign key associated with the user's ID.

I am using entities generated by Netbeans 7.4.

User:

    @Entity
@Table(name = "Users", schema = "dbo")
public class User extends Model implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "UserID")
    private Integer userID;
            @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "userId")
            private Collection<AlertHistory> alertHistoryCollection;
     { ... }

Alert History:

    @Entity
@Table(name = "Alert_History", schema = "dbo")
public class AlertHistory extends Model implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    @Basic(optional = false)
    @Column(name = "user_id")
    @ManyToOne(fetch = FetchType.EAGER)
    private int userId;
    @Basic(optional = false)
    @Column(name = "message_id")
    private long messageId;
    @Basic(optional = false)
    @Lob
    @Column(name = "alerts_triggered")
    private String alertsTriggered;
            {...}

What I would expect is that when I load a user, it will attempt to join the AlertsHistory table results based on the userId. However, at runtime, the AlertsHistory Collection is always null

Update: SOLVED

I was looking at this the wrong way. In order to do a JOIN, AlertHistory was expecting to be handed a User object, and not just the userId key. Adding a User field to the AlertHistory model, and changing the @OneToMany(mappedBy = "userId") to @OneToMany(mappedBy = "user", then adding @JoinColumn(name="user_id", referencedColumnName="userId") private User user; to that User field in AlertHistory got me sorted out! Thanks for the help everyone.


原文:https://stackoverflow.com/questions/21266049
更新时间:2023-02-12 08:02

最满意答案

为什么你必须测试内部课堂?

为了确保行为是预期的测试外部类或方法。

只需按照原样使用该类并测试外部类/方法的行为是否是预期的。 你不需要测试内部类来确保它是正确的行为。

来自dimo414的评论的绝佳插件。

你不应该需要测试内部类 。 内部类完全有可能存在过于复杂的行为,此时它应该被拉出到自己的代码块中,并像其他代码一样进行测试


Why you have to test inner class?

To be sure behaviour is the expected test the outter class or method.

Just use the class as is and test if behaviour of the outer class / method is the expected. You don't need to test the inner class to ensure it's correct behaviour.

Great add-on from dimo414's comment.

You shouldn't need to test the inner class. It's entirely possible for an inner class to have overly-complex behavior, at which point it should be pulled out into its own code block and tested like any other code.

相关问答

更多
  • 你需要在课堂上做。 public Test() { new Anonymous1() { void validate() { final Object this1 = this; new Anonymous2() { int calculate() { return Math.abs(this1.getValue()); } ...
  • 为什么它不起作用。 那么你的问题在于你的测试中的TransactionTemplate是一个模拟。 因此它具有与TransactionTemplate相同的接口,但它不知道如何表现。 你负责它的实现 - 这就是嘲笑的重点。 您在代码中明确调用了template.execute() ,这就是您的第一次验证通过的原因。 但是execute()并不是Spring的一个(或者更准确地说,在你的测试中template不是Spring的TransactionTemplate的一个实例,它只是它的一个模拟) - 也就是说 ...
  • 使用父类引用,您只能访问在父类中声明的方法。 当你这样做 Test1 obj1= new Test1(){ public void test1(){ System.out.println("Yes A"); } }; obj1.test1(); // here i am getting error 您正在创建一个class extending Test1的class extending Test1的匿名实例, class extending Test1由父类型引用 ...
  • 来自: http : //www.liensberger.it/web/blog/?p=191 private static bool CheckIfAnonymousType(Type type) { if (type == null) throw new ArgumentNullException("type"); // HACK: The only way to detect anonymous types right now. return Attribut ...
  • 是的,你的第一个类是匿名的,因为它是内联的并且从Thread延伸出来。 它和你的Thread类不一样。 关于MyClass :当然你不能扩展私有方法。 这与匿名类无关,并且是基本的Java继承规则。 让他们公开,你可以将他们扩展到内联。 为了澄清,如果您有一个名称为嵌套类,您可以声明其类型的变量: public class Test { private static class MyThread extends Thread { @Override public v ...
  • 匿名内部类的目的是扩展和实例化现有类,或者一步实现单个接口。 它的局限性可以从上面得出: 只有一个非final类可以扩展或实现一个接口。 只能访问封闭方法的最终局部变量。 (这是由于当内部类的任何方法被调用时,正常的局部变量将超出范围。) 你不能定义一个构造函数。 (班级没有名字。) 如果你需要多个接口,你可以使用一个本地内部类,它就像一个普通的内部类,它有自己的名字,但是在方法中定义。 我不得不承认,我从来没有见过它在实践中使用过,我认为没有人愿意这样做,希望有人会拿出一个例子。 The purpose ...
  • 类是私有的或匿名的这一事实意味着实现细节是私有的。 一般来说,你的测试不应该取决于实现细节,而应该是测试行为 ,而不是具体的实现。 The fact that the class is private or anonymous means that the implementation details are private. In general, your tests should not be depending upon implementation details, but should be t ...
  • 为什么你必须测试内部课堂? 为了确保行为是预期的测试外部类或方法。 只需按照原样使用该类并测试外部类/方法的行为是否是预期的。 你不需要测试内部类来确保它是正确的行为。 来自dimo414的评论的绝佳插件。 你不应该需要测试内部类 。 内部类完全有可能存在过于复杂的行为,此时它应该被拉出到自己的代码块中,并像其他代码一样进行测试 。 Why you have to test inner class? To be sure behaviour is the expected test the outter c ...
  • 这在Java中是不可能的。 函数参数没有像this “命名空间”,您可以用它来表示您的意思。 唯一的解决方案是为外部参数创建一个像outerX的任意前缀,或者为内部创建一个xAsArray 。 This isn't possible in Java. Function parameters have no "namespace" like this which you could use to denote which variable you mean. The only solution is to c ...
  • 我可能会提取一个EventCache组件。 您可以使用实现计算缓存事件或记录任何感兴趣的内容来替换此测试。 我可能不会改变handleEvent的可见性。 您可以实现只从测试用例中引发事件的ServerCommunication。 I would probably extract a EventCache component. You can replace this for your test with an implementation that counts the cached events or ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。