[英]how to define char(2) column in hibernate hbm.xml?
Hi must modify some hibernate legacy code and I should define a SQL char(2) column (foreign key to a char(2) primary key). 您必须修改一些hibernate遗留代码,我应该定义一个SQL char(2)列(char(2)主键的外键)。
My definition file contains: 我的定义文件包含:
<property
name="language"
type="char"
update="true"
insert="true"
column="LANGUAGE"
length="2"
/>
but hibernate generates: 但是hibernate生成:
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
(...)
| LANGUAGE | char(1) | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
The foreign key table is something like: 外键表是这样的:
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| LANGUAGE | char(2) | NO | PRI | NULL | |
(...)
+--------------+--------------+------+-----+---------+-------+
Using: 使用:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.1.GA</version>
</dependency>
The char
type in Hibernate does not map to a String
, it maps to a char
or Character
in Java (see 6.1.1.2 ). Hibernate中的
char
类型不映射到String
,它映射到Java中的char
或Character
(见6.1.1.2 )。 It happens to use char(1)
as the underlying column type in the table to support this for your DBMS; 碰巧使用
char(1)
作为表中的基础列类型来支持DBMS; which by coincidence makes it seem like it's simply ignoring the length (but its reasons are actually unrelated). 巧合的是,它似乎只是忽略了长度(但其原因实际上是无关的)。
You'll have to use string
for the type (so it maps to a Java String
), then you'll have to tell it to use char(2)
for storage. 你必须使用
string
作为类型(因此它映射到Java String
),然后你必须告诉它使用char(2)
存储。 One option is to explicitly specify sql-type
, eg: 一种选择是显式指定
sql-type
,例如:
<property name="language" type="string" update="true" insert="true">
<column name="LANGUAGE" sql-type="char(2)"/>
</property>
Or just manually adjust the table afterwards to change it from varchar(2)
to char(2)
. 或者只是手动调整表格,然后将其从
varchar(2)
更改为char(2)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.