查询检索的重复对象

时间:2018-06-16 18:10:38

标签: hibernate spring-boot jpa spring-data-jpa

我正在使用带有JPA和Hibernate的Spring Boot 2.

在第一个实体中,我有一对多的关系:

@Entity
public class Factories{

    @Id
    @SequenceGenerator(name = "factories_id_seq", sequenceName = "factories_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "factories_id_seq")
    private Integer id;

    @OneToMany(mappedBy = "factory", cascade = CascadeType.ALL, orphanRemoval = true)
    private final List<Machines> machines = new ArrayList<>();
    ...
}

另一方面,我有多对一的关系:

@Entity 
public class Machines {

    @Id
    @SequenceGenerator(name = "machines_id_seq", sequenceName = "machines_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "machines_id_seq")
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "factories_id")
    private Factories factory;
    ...
}

我有3个工厂,每个工厂有一台机器,除了一台有两台机器。

当我运行此查询时:

@Query(value= "SELECT f from Factories f JOIN FETCH f.machines m JOIN FETCH f.cities")
public List<Factories> findAllWithMachine();

我有4个工厂(正确的机器是孩子)......其中两个是相同的。

我原本只想要获得3分。

我做错了吗?

2 个答案:

答案 0 :(得分:1)

我猜你的查询会检索所有拥有一台机器的工厂,而且由于其中一台机器实际上有两台机器,因此对于这两台机器中的每一台机器都将重新检索工厂。

如果这不是必需的行为,您可以在查询中添加DISTINCT子句,作为您不希望在查询结果中看到重复项的提示。

我还建议您为实体FactoryMachine命名,而不是复数形式,因为List<Factory>已经转化为复数。

答案 1 :(得分:1)

只需在查询中添加“distinct”:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(566, 475)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setWindowTitle("Transactional Data")
        self.tableWidget.setGeometry(QtCore.QRect(60, 30, 461, 381))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(7)
        self.tableWidget.setRowCount(5)
        self.tableWidget.setAlternatingRowColors(True)
        self.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 566, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)

        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == "__main__":
    import sys
    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance()
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

另一种变体是覆盖@Query("select distinct f from Factories f join fetch f.machines") public List<Factories> findAllWithMachine(); 方法并使用findAll注释:

EntityGraph

Spring Data JPA中存在错误,请参阅我的错误报告DATAJPA-1299example repo