这是我的用例
我的JPA Repo界面
@Repository
public interface MyJpaRepository extends JpaRepository<MyEntity, Integer> {
//Example query that illustrates working with multiple tables only. Ignore the details of join conditions
@Query(value="SELECT a.p, a.q,a.r , b.s, b.t, c.u,c.v FROM a, b, c
WHERE a.p=?1,b.s=?2,c.u=5"+
"ORDER BY b.t",nativeQuery = true)
List<MyEntity> findByPAndS(String p, Integer s);
}
在这种情况下,我的实体应该如何?这是我的草稿
@Entity
public class MyEntity {
@Column
private Integer p;
@Column
private Integer q;
@Column
private String r;
@Column
private String s;
@Column
private String t;
@Column
private String u;
@Column
private Double v;
public MyEntity(){
}
}
我的实体声明是否有任何问题? JPA / Spring Data如何推断特定列与哪个表相关联?如果我需要明确定义,我该怎么做?
答案 0 :(得分:0)
正如Alan Hay所说,一个@Entity类通常映射到数据库中的一个表。没有&#34;名称&#34;参数表名由类名引起(类MyFoo - &gt;表&#34; MyFoo&#34;)。如果你想引用另一个表,你可以明确地设置它:
@Entity(name = "MyTableName")
class MyFoo {}
关于您的班级成员:如果成员名称与列名称匹配,则您不一定需要@Column Annotation。
@Entity
class MyFoo {
private String bar;
}
将映射到表&#34; MyFoo&#34;使用列&#34; bar&#34;,直到您明确使用其他名称:
@Column(name = "Mycolumn")
private String bar;
由于您有3个表,因此通常会创建3个@Entity类和3个存储库来访问它们,并且域对象相互引用(请参阅@OneToOne,@ OneToMany等等,...)
如果你肯定想为多个表坚持使用单个实体,你可以使用@SecondaryTable检查这个SO答案:is-possible-map-a-single-entity-with-multiple-tables-using-jpa
答案 1 :(得分:0)
@Entity
@Table
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int eid;
private String ename;
private double salary;
private String deg;
public Employee(int eid, String ename, double salary, String deg) {
super( );
this.eid = eid;
this.ename = ename;
this.salary = salary;
this.deg = deg;
}
public Employee( ) {
super();
}
public int getEid( ) {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname( ) {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSalary( ) {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDeg( ) {
return deg;
}
public void setDeg(String deg) {
this.deg = deg;
}
}
我认为您需要此链接。enter link description here