如何在使用JPA进行映射时增加mysql中String的长度 [英] How to increase length of a String in mysql while mapping using JPA
问题描述
我在JPA遇到了一些麻烦。如果有人能提供解决方案,我会非常感激。和JPA(我正在使用MySQL DB)一样,我有一个映射如下的类:
I'm having some trouble in JPA. I would be really thankful if someone could provide a solution. WIth JPA (I'm using MySQL DB), lets say, I have a class mapped as follows:
@Entity
class Employee{
int id;
String employeeName;
//getters and setters...
}
映射到表,我看到String在Mysql中用 varchar(255)
映射。但是,假设我有一个名字超过255个字符的员工,它会显示数据截断错误。
when mapping to the table, I see that String is mapped with varchar(255)
in Mysql. However, lets say if I have an employee whose name is more than 255 chars, it shows data truncation error.
我知道我们可以通过将length属性添加到Employee列中来解决此问题:
I know we could solve this by adding "length" attribute to the Employee column as:
@column(length=1000)
String employeeName;
这是唯一可行的方法吗?我想,如果我们只是映射到java中的String,数据库将动态分配长度。
Is this the only possible way? I thought, if we just map to String in java, the database would dynamically assign length.
推荐答案
映射到表时,我看到String与varchar(255)映射在一起Mysql
when mapping to the table, I see that String is mapped with varchar(255) in Mysql
这是因为创建的DDL语句中的默认长度 VARCHAR
列大多数JPA提供程序(包括Hibernate和EclipseLink)都是255.为 @Column
注释指定 length
属性有助于覆盖值,以便JPA提供者的模式生成器获取新值。
This is because the default length of VARCHAR
columns in DDL statements created by most JPA providers (including Hibernate and EclipseLink) is 255. Specifying the length
attribute to the @Column
annotation helps override the value, so that the new value is picked up by the schema generator of the JPA provider.
我想,如果我们只是映射到java中的字符串,数据库会动态分配长度。
I thought, if we just map to String in java, the database would dynamically assign length.
这是一个不正确的假设。 JPA提供程序将仅创建一次表,并且在应用程序的生命周期内不会动态更改基础表的长度,并且仅当您将提供程序配置为首先创建/更新表定义时。此外, String
的默认映射是SQL VARCHAR
类型。
This is an incorrect assumption. The JPA provider will create tables only once, and will not dynamically change the length of the underlying table during the lifetime of the application, and only if you configure the provider to create/update the table definitions in the first place. Moreover, the default mapping of String
is the SQL VARCHAR
type.
在初始化过程中,您似乎已将JPA提供程序配置为根据需要(在可能删除它们之后)创建表。如果您正在使用Hibernate,则可以使用 persistence.xml
中指定的 hibernate.hbm2ddl.auto
属性来完成值更新
,创建
或 create-drop
。使用EclipseLink,您将指定属性 eclipselink.ddl-generation
,其值为 create-tables
或 drop-and-create-tables
。
You seem to have configured the JPA provider to create tables as needed (after possibly dropping them), during the initialization process. If you're using Hibernate, this is done using the hibernate.hbm2ddl.auto
property specified in persistence.xml
with a value of update
, create
or create-drop
. With EclipseLink, you would be specifying the property eclipselink.ddl-generation
with a value of create-tables
or drop-and-create-tables
.
建议不要在生产环境中使用上述两个属性。理想的方法是使用DDL脚本来创建表。由于您使用的是 VARCHAR
,因此您应该在列定义中指定合适的长度以适合用户输入的最大长度。此外,由于您使用 VARCHAR
而不是 CHAR
,因此数据库引擎将确保分配的存储空间取决于正在存储的记录的大小。
Both of the above properties are not recommended for use in a production environment. The ideal approach is to have DDL scripts to create the tables. Since, you are using VARCHAR
, you ought to specify a suitable length in the column definition, to fit the maximum length of the user input. Additionally, since you are using VARCHAR
over CHAR
, the database engine will ensure that the storage space allocated will depend on the size of the records being stored.
如果您不需要字符串到默认的 VARCHAR
映射,而是使用另一个有效的映射,那么你必须使用 @Column
注释的 columnDefinition
属性。将 Calendar
映射到 TIMESTAMPTZ
SQL数据类型的示例用法显示在 JPA WikiBook ;你需要修改它以满足你的需要。
If you do not need a String to the default VARCHAR
mapping and instead use another valid mapping, then you must use columnDefinition
attribute of the @Column
annotation. An example usage for mapping Calendar
to the TIMESTAMPTZ
SQL datatype is shown in the JPA WikiBook; you'll need to modify this to suit your need.
这篇关于如何在使用JPA进行映射时增加mysql中String的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!