首页 \ 问答 \ Hibernate映射必要?(Hibernate Mapping necessary?)

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
更新时间:2023-07-06 15:07

最满意答案

你不应该调用一个等同于另一个现有类的类,这会导致你的冲突,因此会产生很多问题。

除以下代码外:

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)
    # ...

相关问答

更多
  • 在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__( ...
  • 你想要的任务并不重,它是周期性的,所以使用一个线程是不合适的,对于这个任务我们可以使用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 ...
  • 只需在此处使用QThread.finished信号即可。 如果你完成了你的线程,它将自动执行。 当然,如果需要,您也可以定义自己的自定义信号。 from PyQt5.QtCore import pyqtSignal class Main(QWidget): def __init__(self): super().__init__() def StartButtonEvent(self): self.test = ExecuteThread() ...
  • 阅读您的程序并遵循执行路径。 从window = Buttons()开始,这意味着Buttons.__init__方法运行。 在其中运行messageBox ,它将messageBox插槽连接到button1的clicked信号。 然后打开消息框( self.message.exec_() )。 你的__init__方法最终完成,然后调用app.exec_()实际显示主GUI并启动Qt事件循环,处理按钮点击和重绘事件等事情。 此时,您的程序已被告知显示消息框,并配置为在单击按钮时运行Buttons.mess ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。