玩框架加入表(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, theAlertsHistory
Collection is alwaysnull
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 theAlertHistory
model, and changing the@OneToMany(mappedBy = "userId")
to@OneToMany(mappedBy = "user"
, then adding@JoinColumn(name="user_id", referencedColumnName="userId") private User user;
to thatUser
field inAlertHistory
got me sorted out! Thanks for the help everyone.
原文:https://stackoverflow.com/questions/21266049
最满意答案
为什么你必须测试内部课堂?
为了确保行为是预期的测试外部类或方法。
只需按照原样使用该类并测试外部类/方法的行为是否是预期的。 你不需要测试内部类来确保它是正确的行为。
来自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 ...
-
我如何测试匿名类?(how do I test anonymous class?)[2021-11-08]
为什么你必须测试内部课堂? 为了确保行为是预期的测试外部类或方法。 只需按照原样使用该类并测试外部类/方法的行为是否是预期的。 你不需要测试内部类来确保它是正确的行为。 来自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 ...