我有2个表说学生和老师,并说学生与老师有多对一的关系,并说,teacherId作为外键.
我怎样才能以某种方式使用spring数据JPA repo方法 - findByTeacherName
如果我想查询下面的内容,
select * from Student S, Teacher T
where T.teacherName = 'SACHIN' and S.teacherId = T.teacherId
Run Code Online (Sandbox Code Playgroud)
注意:这里我只想使用查询StudentRepository
,这是使用与StudentHibernateMapping
类有关系的TeacherHibernateMapping
类创建的
任何帮助将不胜感激.
我的班级映射如下:
@Entity
public class Order {
private String formulatedProp;
@Formula(value="(SELECT ',' + coalesce(a.description,a.code)
FROM another_table a
WHERE a.order_id=id FOR XML PATH(''))")
public String getFormulatedProp() {
return formulatedProp;
}
}
Run Code Online (Sandbox Code Playgroud)
该公式的 SQL 查询在单独执行时效果很好。但是当 Hibernate 生成查询来获取订单时,它会将表的别名附加到FOR
和XML
单词上。所以它尝试执行类似的查询
SELECT ',' + coalesce(a.description,a.code)
FROM another_table a
WHERE a.order_id=order0_.id order0_.FOR order0_.XML PATH('')
Run Code Online (Sandbox Code Playgroud)
这显然是错误的。我怎样才能做到这一点?
环境:Hibernate 3.6、SQL-Server 2008
PS 我知道自定义方言可以是一个解决方案,但我不想朝这个方向发展。
假设我有这些类:
class A {}
class B extends A {}
Run Code Online (Sandbox Code Playgroud)
也
static void call(A a) { System.out.print("A"); }
static void call(B b) { System.out.print("B"); }
public static void main(String[] args) {
A a = new A();
A b = new B();
call(a);
call(b);
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:AA
虽然我在期待:AB
我想知道为什么?