有2个实体:Deck和Card。每张纸牌包含100张纸牌中的8张。由于每个Deck都包含多个Card,并且Card也可以是许多不同Decks的一部分,因此我通过创建一个名为deck_cards的新表来加入它们。
@Data
@Entity
@Table(name = "cards")
public class Card {
@Id
private Integer id;
@NotBlank
private String icon;
@NotBlank
private String name;
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},
mappedBy = "cards")
private Set<Deck> decks;
// setters and getters
}
@Data
@Entity
@Table(name="decks")
public class Deck {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(name = "deck_cards",
joinColumns = { @JoinColumn(name = "deck_id",
referencedColumnName = "id")},
inverseJoinColumns = { @JoinColumn(name = "card_id",
referencedColumnName = "id")})
private Set<Card> cards = new HashSet<>();
// setters and getters
}
@Repository
@Transactional(readOnly = true)
public class DeckRepositoryImpl implements DeckRepositoryCustom {
@PersistenceContext
EntityManager entityManager;
@Override
public Deck getDeckContaining(List<String> cardIds) {
String queryStr =
"select deck_id\n" +
"from deck_cards\n" +
// Hard-coding card_ids of interest for now to
// see if the query itself works
"where card_id in (1, 2, 3, 4, 5, 6, 7, 8)\n" +
"group by deck_id\n" +
"having count(*) = 8";
Query query = entityManager.createNativeQuery(queryStr,
Deck.class);
return (Deck) query.getSingleResult();
}
我有一个名为getDeckContaining()的自定义方法,该方法最初旨在接收List cardIds,并查看是否有一个包含存储在列表中的ID的卡片组。现在,我对卡的ID进行了硬编码,以在查询字符串中查找。即使那样,当我调用这样的方法时:
List<String> teamCardIdsInString = new ArrayList<>();
//....
Deck deckOfInterest =
deckRepository.getDeckContaining(teamCardIdsInString);
我收到以下错误消息:
2019-07-29 19:00:06.663 WARN 3409 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: S0022
2019-07-29 19:00:06.664 ERROR 3409 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Column 'id' not found.
2019-07-29 19:00:06.690 ERROR 3409 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select deck_id
from deck_cards
where card_id in (26000060, 28000017, 26000044, 26000039, 26000027, 28000015, 26000033, 28000000)
group by deck_id
having count(*) = 8]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
java.sql.SQLException: Column 'id' not found.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.16.jar:8.0.16]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.16.jar:8.0.16]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.16.jar:8.0.16]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.16.jar:8.0.16]
at com.mysql.cj.jdbc.result.ResultSetImpl.findColumn(ResultSetImpl.java:561) ~[mysql-connector-java-8.0.16.jar:8.0.16]
at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:825) ~[mysql-connector-java-8.0.16.jar:8.0.16]
at com.zaxxer.hikari.pool.HikariProxyResultSet.getInt(HikariProxyResultSet.java) ~[HikariCP-3.2.0.jar:na]
at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:62) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at
我很困惑,因为我以为我使用了实体中定义的字段名称。有什么我可能会错过的东西吗?
答案 0 :(得分:0)
java.sql.SQLException:找不到列“ id”。问题出在列名上。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class GoToDialog(QtWidgets.QDialog):
gotoSignal = QtCore.pyqtSignal(int)
def __init__(self, text_edit, parent=None):
super().__init__(parent)
self.m_text_edit = text_edit
self.spinbox = QtWidgets.QSpinBox(minimum=1)
self.m_text_edit.document().blockCountChanged.connect(self.spinbox.setMaximum)
self.spinbox.setMaximum(self.m_text_edit.document().blockCount())
self.buttonGoto = QtWidgets.QPushButton("Go to...")
self.buttonGoto.clicked.connect(self.onAccepted)
self.buttonCancel = QtWidgets.QPushButton("Cancel")
self.buttonCancel.clicked.connect(self.reject)
vlay = QtWidgets.QVBoxLayout(self)
hlay = QtWidgets.QHBoxLayout()
hlay.addWidget(self.buttonGoto)
hlay.addWidget(self.buttonCancel)
vlay.addWidget(QtWidgets.QLabel("Go to..."))
vlay.addWidget(self.spinbox)
vlay.addLayout(hlay)
self.setFixedSize(300, 100)
def onAccepted(self):
n = self.spinbox.value() - 1
if 0 <= n < self.m_text_edit.document().blockCount():
cursor = QtGui.QTextCursor(
self.m_text_edit.document().findBlockByLineNumber(n)
)
self.m_text_edit.setTextCursor(cursor)
self.reject()
class Writter(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.text_edit = QtWidgets.QTextEdit()
self.setCentralWidget(self.text_edit)
self.goto_dialog = GoToDialog(self.text_edit, self)
menubar = self.menuBar()
editMenu = menubar.addMenu("Edit")
goto_edit = QtWidgets.QAction("Go To...", self)
goto_edit.setShortcut("Ctrl+T")
goto_edit.triggered.connect(self.goto_dialog.show)
editMenu.addAction(goto_edit)
self.resize(600, 500)
self.center()
def center(self):
self.setGeometry(
QtWidgets.QStyle.alignedRect(
QtCore.Qt.LeftToRight,
QtCore.Qt.AlignCenter,
self.size(),
QtWidgets.qApp.desktop().availableGeometry(),
)
)
def main():
app = QtWidgets.QApplication(sys.argv)
w = Writter()
w.show()
return app.exec_()
if __name__ == "__main__":
sys.exit(main())
是这里(下)的可能嫌疑犯
可能的解决方案:
referencedColumnName = "id"
referencedColumnName = "id"
),请确保数据库中的id字段存在