在不同表上具有本机@Query的JpaRepository

时间:2018-11-06 13:22:10

标签: spring spring-boot nativequery

我只是偶然发现了spring数据中的一些意外行为。 为了演示,我使用Initializr(https://start.spring.io/)添加了JPA,Web,H2,设置了一些Spring Boot应用程序。

该应用程序包含两个表和一些数据:

data.sql:

create table table1 (
  id int,
  name varchar(50)
);

create table table2 (
  id int,
  name varchar(50)
);

insert into table1 (id, name) values (1, 'First row from table 1');
insert into table1 (id, name) values (2, 'Second row from table 1');
insert into table1 (id, name) values (3, 'Third row from table 1');
insert into table1 (id, name) values (4, 'Fourth row from table 1');

insert into table2 (id, name) values (1, '** TABLE 2: 1st ROW UPPERCASE **');
insert into table2 (id, name) values (2, '** TABLE 2: 2nd ROW UPPERCASE **');

此表结构只有一个模型,因为两个表的结构相同。我为此表创建了一个JpaRepository

SampleDAO:

@Repository
public interface SampleDAO extends JpaRepository<SampleModel, Integer> {

  @Query(value = "select id, name from table1", nativeQuery = true)
  List<SampleModel> findAllFromTable1();

  @Query(value = "select id, name from table2", nativeQuery = true)
  List<SampleModel> findAllFromTable2();
}

最后我添加了一个控制器(TestController):

@RestController
public class TestController {

  @Autowired
  private final SampleDAO sampleDAO;

  public TestController(SampleDAO sampleDAO) {
    this.sampleDAO = sampleDAO;
  }

  @GetMapping(path = "/")
  @ResponseBody
  public String testNativeQuery() {
    List<SampleModel> list1 = this.sampleDAO.findAllFromTable1();
    List<SampleModel> list2 = this.sampleDAO.findAllFromTable2();

    SampleModel m1 = list1.get(0);
    SampleModel m2 = list2.get(0);
    System.out.println("*****************************************");
    System.out.println("Data from findAllFromTable1():");
    list1.forEach(l -> {
      System.out.println(l.getName());
    });
    System.out.println("*****************************************");
    System.out.println("Data from findAllFromTable2():");
    list2.forEach(l -> {
      System.out.println(l.getName());
    });
    System.out.println("*****************************************");

    return "Done";
  }
}

我希望list1和list2包含两个表的数据,但令人惊讶的是,仅获取了第一个表的结果:

*****************************************
Data from findAllFromTable1():
First row from table 1
Second row from table 1
Third row from table 1
Fourth row from table 1
*****************************************
Data from findAllFromTable2():
First row from table 1
Second row from table 1
*****************************************

您可以在此处找到示例项目:https://github.com/steinmann321/nativequerydemo

这是预期的行为还是我做错了什么?

(请注意:这只是一个示例项目,实际查询要复杂得多,但结果是相同的)

1 个答案:

答案 0 :(得分:3)

您遇到的问题是持久实体之间的身份冲突。您可以看到您从第一个查询获取了四个记录,从第二个查询获取了两个记录,它们与数据库中的记录匹配。但是当entitymanager正在处理第二组中的记录时,它发现已经存在具有id 1和id 2的持久性实体,因此它不会创建新的实体。

如果您希望此方法有效,则需要指示您有一个由ID和名称组成的组合键。这也将与equals方法中的业务逻辑相匹配。

创建一个JPA可嵌入密钥:

@Embeddable
public class MyKey implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "id", nullable = false)
    private String id;

    @Column(name = "name", nullable = false)
    private String name;

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }
      MyKey that = (MyKey) o;
      return Objects.equals(id, that.getId()) && Objects.equals(name, that.getName());
    }

    @Override
    public int hashCode() {
      return Objects.hash(id, name);
    }
}   

然后使用您的SampleModel实体内的嵌入式密钥:

@Entity
public class SampleModel {

  @EmbeddedId
  private MyKey myKey;

  public MyKey getMyKey() {
    return myKey;
  }

  public void setMyKey(MyKey myKey) {
    this.myKey = myKey;
  }
}

您的控制器将稍作更改以从键中提取值。

System.out.println("*****************************************");
System.out.println("Data from findAllFromTable1():");
list1.forEach(l -> {
  System.out.println(l.getMyKey().getName());
});
System.out.println("*****************************************");
System.out.println("Data from findAllFromTable2():");
list2.forEach(l -> {
  System.out.println(l.getMyKey().getName());
});
System.out.println("*****************************************");

然后尝试您的TestController。我确实尝试过,结果如下:

*****************************************
Data from findAllFromTable1():
First row from table 1
Second row from table 1
Third row from table 1
Fourth row from table 1
*****************************************
Data from findAllFromTable2():
** TABLE 2: 1st ROW UPPERCASE **
** TABLE 2: 2nd ROW UPPERCASE **
*****************************************