English 中文(简体)
试图指定冬眠中属性的默认值
原标题:Trying to specify default value for property in hibernate

在试图将一行插入表格时,我从休眠处收到以下错误信息:

org.hibernate.exception.ConstraintViolationException: Column priority cannot be null

我知道,我可以在代码中插入一条线来设定值,但还有其他许多程序依赖数据库中的默认值(db是 Mysql) 的例子。

我读到某处, 您可以在 hbm. xml 文件中提供默认值, 但冬眠无法识别它。 这里对应的栏目来自 JobQuue. hbm. xml

    <property name="priority" type="integer">
        <column name="priority" default="0" />
    </property> 

我想另一种选择是修改生成的工作 Quue.java 文件( Im 使用日蚀冬眠工具自动生成冬眠类), 但现在我想尝试让 hbm. xml 配置工作 。

我使用的是冬眠图书馆4.1.3版本和日蚀冬眠工具3.4.0.x。

最佳回答

最后,我修改了JobQuue.java POJO 以设定默认值。为了确保冬眠工具代号不会覆盖此更改,我设置了该代号,以使代号生成在临时文件夹中生成文件,然后将必要的文件复制到永久源位置。

问题回答

default="0" 只与生成数据库 schemata 的 SchemaExport相关。 除了冬眠之外, 其它的冬日完全忽略了此设置 。 您可以尝试为列设置 non- null=" true"

Unless you are not able to recreate the whole database schema, you can set the default value in the variable initialization. In your model set the priority to 0 in the initialization.

在你的班级:

private Integer priority = 0;




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签