Hibernate映射必要?(Hibernate Mapping necessary?)
我是Hibernate的新手,我下载了一个示例Java应用程序并将其修改为更有用,到目前为止,在我的应用程序中,我有两个在我的Java应用程序中定义的db类:Actor.java和City.java每个类都有.hbm.xml文件如下所示:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Jun 17, 2009 11:42:48 AM by Hibernate Tools 3.2.1.GA --> <hibernate-mapping> <class name="sakila.entity.Actor" table="actor" catalog="sakila"> <id name="actorId" type="java.lang.Short"> <column name="actor_id" /> <generator class="identity" /> </id> <property name="firstName" type="string"> <column name="first_name" length="45" not-null="true" /> </property> <property name="lastName" type="string"> <column name="last_name" length="45" not-null="true" /> </property> <property name="lastUpdate" type="timestamp"> <column name="last_update" length="19" not-null="true" /> </property> </class> </hibernate-mapping>
<hibernate-mapping> <class name="sakila.entity.City" table="city" catalog="sakila"> <id name="cityId" type="java.lang.Short"> <column name="city_id" /> <generator class="identity" /> </id> <property name="city" type="string"> <column name="city" length="45" not-null="true" /> </property> <property name="countryId" type="string"> <column name="country_Id" length="45" not-null="true" /> </property> <property name="lastUpdate" type="timestamp"> <column name="last_update" length="19" not-null="true" /> </property> </class> </hibernate-mapping>
该应用程序迄今为止效果很好。 但我的问题是,如果我需要更改我的应用程序,以便它可以处理像数据库中的Actor和City等数百个对象,我需要定义所有映射文件吗? 如果我在编程时不知道用户可能查询哪些数据库对象,我是否需要定义它们,这似乎是不可能的,所以我的问题是:是否需要* .hbm.xml文件? 在这种情况下,我需要编写一个应用程序,让用户查询他需要的任何对象,我甚至可能不知道数据库中的内容,如何编写这样的应用程序? Hibernate可用吗?
I'm new to Hibernate, I downloaded a sample Java app and modified it to be more useful, so far in my app I have two db classes that are defined in my Java app : Actor.java and City.java Each of them has a .hbm.xml file look like the following :
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Jun 17, 2009 11:42:48 AM by Hibernate Tools 3.2.1.GA --> <hibernate-mapping> <class name="sakila.entity.Actor" table="actor" catalog="sakila"> <id name="actorId" type="java.lang.Short"> <column name="actor_id" /> <generator class="identity" /> </id> <property name="firstName" type="string"> <column name="first_name" length="45" not-null="true" /> </property> <property name="lastName" type="string"> <column name="last_name" length="45" not-null="true" /> </property> <property name="lastUpdate" type="timestamp"> <column name="last_update" length="19" not-null="true" /> </property> </class> </hibernate-mapping>
<hibernate-mapping> <class name="sakila.entity.City" table="city" catalog="sakila"> <id name="cityId" type="java.lang.Short"> <column name="city_id" /> <generator class="identity" /> </id> <property name="city" type="string"> <column name="city" length="45" not-null="true" /> </property> <property name="countryId" type="string"> <column name="country_Id" length="45" not-null="true" /> </property> <property name="lastUpdate" type="timestamp"> <column name="last_update" length="19" not-null="true" /> </property> </class> </hibernate-mapping>
The app works great so far. But my question is, what if I need to change my app so that it can handle hundreds of objects like Actor and City in the db, do I need to define all the mapping files ? What if I don't know at the time of programming what db objects user might query, do I need to define all of them, that seems impossible, so my question is : is the *.hbm.xml files necessary ? In the case I need to write an app that lets user query whatever objects he needs, and I may not even know what's in the db, how to write such an app ? Doable with Hibernate ?
原文:https://stackoverflow.com/questions/14428059
最满意答案
你不应该调用一个等同于另一个现有类的类,这会导致你的冲突,因此会产生很多问题。
除以下代码外:
self.userfield = QtWidgets.QTextEdit(self.centralwidget)
你告诉他们使用QtWidgets模块中的QTextEdit,而不是你的类。
解:
... class TextEdit(QtWidgets.QTextEdit): clicked = pyqtSignal() def mouseReleaseEvent(self,event): self.clicked.emit() ... self.userfield = TextEdit(self.centralwidget) self.userfield.setMaximumSize(QtCore.QSize(200, 30)) self.userfield.setObjectName("userfield") self.userfield.clicked.connect(self.txt) ...
You should not call a class equal to another existing class, that can cause you conflicts and therefore many problems.
Apart from that in the following code:
self.userfield = QtWidgets.QTextEdit(self.centralwidget)
You are telling them to use the QTextEdit from QtWidgets module and not your class.
Solution:
# ... class TextEdit(QtWidgets.QTextEdit): clicked = pyqtSignal() def mouseReleaseEvent(self,event): self.clicked.emit() # ... self.userfield = TextEdit(self.centralwidget) self.userfield.setMaximumSize(QtCore.QSize(200, 30)) self.userfield.setObjectName("userfield") self.userfield.clicked.connect(self.txt) # ...
相关问答
更多-
PyQt用dict发出信号(PyQt emit signal with dict)[2023-04-29]
在pyqtSignal定义中使用object而不是dict 。 例如 class Emiterer(QtCore.QThread): f = QtCore.pyqtSignal(object) 这样做的原因是定义为pyqtSignal(dict)信号实际上被pyqtSignal('QVariantMap')解释为与pyqtSignal('QVariantMap')相同,而QVariantMap只能将字符串作为键。 您可以查看(针对您的特定课程) m = Emiterer.staticMetaObj ... -
正如你可以在这里阅读的 , QtCore.SIGNAL在PyQt4之后不再使用,因此不兼容。 本页面介绍了PyQt5的新型信号和插槽。 语法是: PyQt5.QtCore.pyqtSignal(types[, name[, revision=0[, arguments=[]]]]) 您的案例可以翻译成: from PyQt5 import pyqtsignal data_changed = pyqtsignal(QModelindex,QModelIndex) 并发出你的信号: self.data_c ...
-
您的代码有以下错误: 信号不能在类的任何方法中声明,它必须与方法处于同一级别。 如果我在将信号连接到任何插槽之前发送信号,则没有人会收听这些信息,这样数据就会丢失,即传输数据几乎是瞬间的。 在下面的代码中,我已经实施了必要的修改以使其工作: class MyApp(QWidget): signal = pyqtSignal(str, str, int) def __init__(self, parent= None): super(MyApp, self).__init__( ...
-
PyQt5:如何自动滚动QTextEdit中的文本(动画效果)?(PyQt5: How to scroll text in QTextEdit automatically (animational effect)?)[2022-04-17]
你想要的任务并不重,它是周期性的,所以使用一个线程是不合适的,对于这个任务我们可以使用QVariantAnimation 。 另一部分是创建一个移动到某一行文本的方法,我们在findBlockByLineNumber()旁边使用QTextDocument 。 对于最后一个,我们必须开始移动到最后可见的初始位置,我们使用cursorForPosition()方法通过viewport()的大小。 longText = "\n".join(["{}: long text - auto scrolling ".fo ... -
简单。 必须首先发出信号 插槽作为参数。 您必须在创建信号后才能连接 只有在建立连接后才能发射。 这里我们举一个小例子: self.signalOwner.mySignal.connect(self.slotFunction) 在你的例子中,让我们说并考虑视图拥有信号,而pictureDropped是你的插槽函数,所以: self.view.dropped.connect(self.pictureDropped) 请记住,您的信号必须发出某种类型或根本不发送任何类型,并且您的@pyqtSlot函数必须接 ...
-
time.sleep()阻塞Qt主循环,因此无法处理窗口重绘事件。 使用QTimer定期调用发出信号的方法,以便定期将控制权返回给Qt事件循环。 time.sleep() blocks the Qt main loop so it can't process window redraw events. Use a QTimer to periodically call a method which emits your signal so that control is returned to the Qt ...
-
你不应该调用一个等同于另一个现有类的类,这会导致你的冲突,因此会产生很多问题。 除以下代码外: self.userfield = QtWidgets.QTextEdit(self.centralwidget) 你告诉他们使用QtWidgets模块中的QTextEdit,而不是你的类。 解: ... class TextEdit(QtWidgets.QTextEdit): clicked = pyqtSignal() def mouseReleaseEvent(self,event): ...
-
您可以将QTextEdit添加为QMainWindow的中心窗口小部件,如下所示: class App(QMainWindow): def __init__(self): super().__init__() self.title = 'Text Editor' self.left = 10 self.top = 10 self.width = 1080 self.height = 920 ...
-
PyQt5 - 如何通过GUI线程从工作人员发出信号来调用事件(PyQt5 - How to emit signal from worker tread to call event by GUI thread)[2023-11-12]
只需在此处使用QThread.finished信号即可。 如果你完成了你的线程,它将自动执行。 当然,如果需要,您也可以定义自己的自定义信号。 from PyQt5.QtCore import pyqtSignal class Main(QWidget): def __init__(self): super().__init__() def StartButtonEvent(self): self.test = ExecuteThread() ... -
PyQt5:使用信号和插槽通过QPushButton验证LineEdit(PyQt5: Validate LineEdit through QPushButton using signals and slots)[2023-04-23]
阅读您的程序并遵循执行路径。 从window = Buttons()开始,这意味着Buttons.__init__方法运行。 在其中运行messageBox ,它将messageBox插槽连接到button1的clicked信号。 然后打开消息框( self.message.exec_() )。 你的__init__方法最终完成,然后调用app.exec_()实际显示主GUI并启动Qt事件循环,处理按钮点击和重绘事件等事情。 此时,您的程序已被告知显示消息框,并配置为在单击按钮时运行Buttons.mess ...